小白易患错误之绝对路径和相对路径的操作错误

发布时间:2020-08-17 18:50:16 作者:hello大钊
来源:网络 阅读:889

小白易患错误之绝对路径和相对路径的操作错误

作为一个不安稳的小白,一天都在那路乱折腾,恰巧,老师课程题目中有一题将/etc/skles 这个目录的文件除了..和. 复制到/home/USRNAEM 的家目录下。然后自以为是不按照老师的方法,自己折腾用了这样一条命令

[root@local skel]# ls -A
.bash_logout  .bash_profile  .bashrc
[root@local skel]# cp $(ls -A) /home/mao1/
[root@local skel]# cd /home/mao1
[root@local mao1]# ls -A
.bash_logout  .bash_profile  .bashrc

然后我又在这个skel目录下建立一个目录,确认目录是否能这样被引用。

[root@local skel]# mkdir .test.dir
[root@local skel]# mkdir test1.dir
[root@local skel]# ls -A
.bash_logout  .bash_profile  .bashrc  test1.dir  .test.dir
[root@local skel]# cp -r $(ls -A) /home/mao1
cp: overwrite `/home/mao1/.bash_logout'? y
cp: overwrite `/home/mao1/.bash_profile'? y
cp: overwrite `/home/mao1/.bashrc'? y
[root@local skel]# ls -a /home/mao1/
.  ..  .bash_logout  .bash_profile  .bashrc  test1.dir  .test.dir

也能成功,然后我又折腾了一下,到一个下一个用户的家目录,mao2(前面是mao1,请注意)。又执行这条命令

[root@local ~]# cd /home/mao2/
[root@local mao2]# ls -A
[root@local mao2]# cp -r $(ls -A /etc/skel/) /home/mao2/
cp: cannot stat `.bash_logout': No such file or directory
cp: cannot stat `.bash_profile': No such file or directory
cp: cannot stat `.bashrc': No such file or directory
cp: cannot stat `test1.dir': No such file or directory
cp: cannot stat `.test.dir': No such file or directory

居然给我反馈找不到文件和目录,我瞬间懵逼了。why? 一模一样的操作,居然前面能执行就换个目录就出错。瞬间感觉没爱了。然后隔壁老王让我排错,天生作为一个trouble maker。走上一条不属我的排错路,各种百度,google都无能给我解决。然后又将cp、ls的man文档一一翻译了一遍,均没结果。 随即想起“一支烟,解千愁,去万恨。”果然,回来的时候翻着ppt看,发现绝对路径和相对路径。仔细一看果然虽然我ls -A /etc/skel 显示的是skel目录下的文件,但是我cp的时候不在skel目录下,系统就会在当前路径下寻找这几个文件,恰巧当前目录没有这几个,所以就出错了。各位看官,到此处应该看出来,我的错误了。接下来验证我的想法是否正确。

[root@localhost tmp]# ll -A
total 0
[root@localhost tmp]# cd /etc/skel/
[root@localhost skel]# ls -A
.bash_logout  .bash_profile  .bashrc  .mozilla
[root@localhost skel]# cp -r $(ls -A /etc/skel/) /tmp 
[root@localhost skel]# cd /tmp/
[root@localhost tmp]# ll -A
total 12
-rw-r--r--. 1 root root  18 Aug  2 08:44 .bash_logout
-rw-r--r--. 1 root root 193 Aug  2 08:44 .bash_profile
-rw-r--r--. 1 root root 231 Aug  2 08:44 .bashrc
drwxr-xr-x. 4 root root  37 Aug  2 08:44 .mozilla
[root@localhost tmp]# cd /home/mao2
[root@localhost mao2]# ll -A
total 0
[root@localhost mao2]# cd -
/tmp
[root@localhost tmp]# cp -r $(ls -A /etc/skel/) /home/mao2/ 
[root@localhost tmp]# cd -
/home/mao2
[root@localhost mao2]# ls -A
.bash_logout  .bash_profile  .bashrc  .mozilla
[root@localhost mao2]#

上面可以看出只要在有这几个文件的目录下能成功,那我们再去没有这几个文件的目录下面执行以下这条命令。

[root@localhost mao2]# cd /usr/tmp/
[root@localhost tmp]# ls -A
[root@localhost tmp]# cp -r $(ls -A /etc/skel/)  /home/mao2/ 
cp: cannot stat ‘.bash_logout’: No such file or directory
cp: cannot stat ‘.bash_profile’: No such file or directory
cp: cannot stat ‘.bashrc’: No such file or directory
cp: cannot stat ‘.mozilla’: No such file or directory

诚不欺我,果然不能成功。作为一个小白继续折腾,没有路径,我给你添加。

[root@localhost tmp]# cp -r /etc/skel/$(ls -A /etc/skel/)  /home/mao2/ 
cp: overwrite ‘/home/mao2/.bash_logout’? y
cp: cannot stat ‘.bash_profile’: No such file or directory
cp: cannot stat ‘.bashrc’: No such file or directory
cp: cannot stat ‘.mozilla’: No such file or directory

oh,shit。又只能拷贝一个,一看原来中间有空格,系统把每个空格后面有当成相对路径。不怕,我们继续,反正作为一个trouble maker。我就是不断创造麻烦的。那就继续,既然这样我尝试用xargs将结果输出,然后再用sed在每个文件前面加个路径。

尝试1:
[root@localhost tmp]# cp -r /etc/skel/{$(ls -A /etc/skel/ |xargs|sed -r "s#[[:space:]]#\,#g")} /tmp
cp: cannot stat ‘/etc/skel/{.bash_logout,.bash_profile,.bashrc,.mozilla}’: No such file or directory
[root@localhost tmp]# cp /etc/skel/{.bash_logout,.bash_profile,.bashrc,.mozilla}  /usr/tmp/
cp: omitting directory ‘/etc/skel/.mozilla’
[root@localhost tmp]# cp -r /etc/skel/{.bash_logout,.bash_profile,.bashrc,.mozilla}  /usr/tmp/
cp: overwrite ‘/usr/tmp/.bash_logout’? y
cp: overwrite ‘/usr/tmp/.bash_profile’? y
cp: overwrite ‘/usr/tmp/.bashrc’? y

这个问题的原因在于命令引用后的bash的{}特性不再被识别,/etc/skel/{.bashlogout,.bashprofile,.bashrc,.mozilla}被当做一个整体的文件了。因此路不通。

尝试2:
[root@localhost ~]# ls -A /etc/skel/ |xargs|sed -r "s#(\..*[[:alpha:]]\b)#/etc/skel/\1#g" 
/etc/skel/.bash_logout .bash_profile .bashrc .mozilla

这个尝试最为纠结,尝试了各种不同办法,我在下面简单写一下部分尝试。

[root@localhost tmp]# ls -A /etc/skel/ >1.txt
[root@localhost tmp]# cat 1.txt 
.bash_logout
.bash_profile
.bashrc
.mozilla
[root@localhost tmp]# sed -r "s#(\..*[[:alpha:]]\>)#/etc/skel/\1#g" 1.txt 
/etc/skel/.bash_logout
/etc/skel/.bash_profile
/etc/skel/.bashrc
/etc/skel/.mozilla

由此证明正则表达式没错。go on;

oh,no, 又出错了。

[root@localhost tmp]# ls -A /etc/skel/ |xargs > 1.txt 
[root@localhost tmp]# sed -r "s#(\..*[[:alpha:]]\>)#/etc/skel/\1#g" 1.txt 
/etc/skel/.bash_logout .bash_profile .bashrc .mozilla

于是我怀疑是xargs命令的问题,我用加法运算做了一个尝试

[root@localhost tmp]# seq 1 10 |xargs|sed -r "s# #+#g" |bc
55
[root@localhost tmp]# seq 1 10|xargs 
1 2 3 4 5 6 7 8 9 10
[root@localhost tmp]# seq 1 10 |xargs|sed -r "s# #+#g" 
1+2+3+4+5+6+7+8+9+10
[root@localhost tmp]# seq 1 10 |xargs|sed -r "s# #+#g" |bc
55

xargs也没有问题,同样的命令,同样的正则表达式居然就在上面行不通了。若各位看官能解决此问题,望通知小弟。

尝试3:
[root@localhost home]# for i in `ls -A /etc/skel/` ;do cp -r /etc/skel/$i  /tmp ;done
cp: overwrite ‘/tmp/.bash_logout’? y
cp: overwrite ‘/tmp/.bash_profile’? y     
cp: overwrite ‘/tmp/.bashrc’? y

用for循环来实现这个功能,这个思路来自于teacher Rex。

尝试4:
cp -r /etc/skel/.  /home/mao

隔壁老王的方法,这个方法是最完美,最简便的方法。teacher wang 不亏是老司机。

总结:一,理解为什么排错最痛苦。这次不完全排错我用了一天功夫,原因有1.作为一个小白很多东西没有接触到,这个是个吃经验的活。2.自己把简单问题复杂化了。3.对于相对路径和绝对路径的理解不够深透。

二,来源teacher Rex,既然不知道怎么出错,就把每一个命令一一追加之新的文件,在对这个被追加的文件来执行命令。

三,踏踏实实做作业,不要猎奇。好奇心害死猫。

四,有问题找隔壁老王,万能的teacher wang。


推荐阅读:
  1. Oracle RAC错误之--oifcfg错误案例
  2. php小白容易出现的 strpos 逻辑错误

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

错误 绝对路径 相对路径

上一篇:【Flashback】Flashback Database闪回数据库功能实践

下一篇:ora2pg的使用(一、centos下的安装)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》