小白易患錯誤之絕對路徑和相對路徑的操作錯誤

小白易患錯誤之絕對路徑和相對路徑的操作錯誤

作爲一個不安穩的小白,一天都在那路亂折騰,恰巧,老師課程題目中有一題將/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。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章