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

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

作为一个不安稳的小白,一天都在那路乱折腾,恰巧,老师课程题目中有一题将/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

相关推荐

  • iptables练习

    iptables练习 一、COMMAND 1、列出所有链的规则:iptables -L ,显示某条链的规则就是iptables -L INPUT 详细信息:iptables -vnL 2、清楚所有链的规则 :iptables -F 3、设置默认规则策略:iptables -P INPUT DROP,iptables -P OUTPUT DROP , ipta…

    Linux干货 2016-12-20
  • 初识Linux

    本文对计算机组成及其功能、Linux的发行版、以及Linux的哲学思想进行了简单的介绍;同时对Linux系统中常用的基础命令以及如何获取帮助信息进行了详细的说明。

    2018-01-14
  • 决不放弃

    坚持到底!

    Linux干货 2017-11-17
  • python 常用记录

    all() >>> a = [11,22,33] >>> all(a) >>> ture 对序列进行判断,如果全为真,则返回Ture >>> a = [11,22,0] >>> a…

    Linux干货 2016-09-19
  • lnmap实战之负载均衡架构(无高可用)

    lnmap实战之负载均衡架构(无高可用) 架构图如下: 此次实战软件,全部yum安装 1.准备好机器,同步好时间 192.168.42.150 node1 [负载均衡器]192.168.42.152 node3 [web2]192.168.42.153 node4 [web1]192.168.42.151 node2 [memcached session存储…

    Linux干货 2017-06-22
  • 创建及管理KVM工具之virsh

    一、什么是虚拟化 虚拟化是一个广义的术语,在计算机方面通常是指计算元件在虚拟的基础上而不是真实的基础上运行。虚拟化技术可以扩大硬件的容量,简化软件的重新配置过程。 二、虚拟化能做什么 CPU的虚拟化技术可以单CPU模拟多CPU并行,允许一个平台同时运行多个操作系统,并且应用程序都可以在相互独立的空间内运行而互不影响,从而显著提高计算机的工作效率。 三、虚拟化…

    Linux干货 2017-01-19