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

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

作为一个不安稳的小白,一天都在那路乱折腾,恰巧,老师课程题目中有一题将/etc/skel 这个目录的文件除了..和. 复制到/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。

原创文章,作者:fighter,如若转载,请注明出处:http://www.178linux.com/27583

(0)
fighterfighter
上一篇 2016-08-05 16:20
下一篇 2016-08-05 16:20

相关推荐

  • 玩转Nginx之一:基础概念

    Nginx web服务器:http协议 http协议:html,MIME(多用途互联网邮件拓展) major/minor text/html,text/plain,image/jpeg web资源:URL(scheme://server:port/path/to/source) 方法:GET,HEAD(相应首部)读取资源 POST提交表单 PUT上传数据 D…

    Linux干货 2016-10-29
  • 文本编辑器sed和vim的用法集锦

    8月8号,主要学习内容为: 一、文本处理工具sed 二、vim编辑器 一、文本处理工具sed 1)简介    sed是一种流编辑器,它一次处理一行内容。处理时,把 当前处理的行存储在临时缓冲区中,称为“模式空间”( pattern space),接着用sed命令处理缓冲区中的内容 ,处理完成后,把缓冲区的内容送往屏幕。接着处理下…

    Linux干货 2016-08-12
  • N26—第三周

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 [root@localhost ~]# who | cut -d ' ' -f 1 |sort -u l_cong root (unknown)   2、取出最后登录到当前系统的用户的相关信息。 [l_cong@localhost ~]$…

    Linux干货 2017-02-15
  • HAProxy七种调度方法的简单示意图

    看了三个月,中间因出差和其他事没有看估计也有十几天,刚把35天的视频看完。很多内容都记不住,待第一次看完后再回头看吧。 现在想,对内容进行简单的画图,把基本的体现出来,对记忆和回顾应该有帮助。

    Linux干货 2016-07-26
  • 马哥教育网络班21期+第四周博客作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 2、编辑/etc/group文件,添加组hadoop。 3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的id号;其家目录为/home/hadoop。 4、复制/etc/sk…

    Linux干货 2016-07-07
  • 第十三周作业

    “1、建立samba共享,共享目录为/data,要求:(描述完整的过程)   1)共享名为shared,工作组为magedu;   2)添加组develop,添加用户gentoo,centos和ubuntu,其中gentoo和centos以develop为附加组,ubuntu不属于develop组;密码均为用户名; …

    Linux干货 2017-08-13