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

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

作為一個不安穩的小白,一天都在那路亂折騰,恰巧,老師課程題目中有一題將/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文檔一一翻譯了一遍,均沒結果。 隨即想起“一支煙,解千愁,去萬恨?!惫唬貋淼臅r候翻著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.www58058.com/27583

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

相關推薦

  • 08.04 筆記總結

    1.ps axo  user,ruser,group,rgroup,cmd  //  ruser  表示程序發起的用戶,一般是指系統當前的用戶  //  user  表示程序的所有者。在大數情況下,兩個用戶都是系統當前的用戶, 之后當程序被賦予suid的時候,可能user會發生變化。 2.…

    Linux干貨 2016-08-08
  • shell腳本總結

    shell腳本簡要總結 腳本調試 bash -n /path/to/some_script檢測腳本中的語法錯誤 bash -x /path/to/some_script調試執行 變量 環境變量 變量聲明、賦值:export name=VALUEdeclare -x name=VALUE 變量引用:$name, ${name} 顯示所有環境變量: export…

    Linux干貨 2016-08-21
  • 第八周作業(網絡、bash腳本)

    1-3,計算機網絡基礎博客已總結,簡要補充 協議的分層 為了使那些比較復雜的網絡協議更加簡單化。ISO根據各通信協議的功能將網絡體系分成七層。 在這個分層中,下層為上一層提供服務。上下層之間進行交互時所遵循的約定叫做“接口”。同層之間的交互所遵循的約定叫做“協議” 說的更形象一點,兩個中國人打電話這個通信。就可以分為兩層,同層之間的叫協議,我說漢語,你也說漢…

    Linux干貨 2016-12-18
  • 計算機及Linux基礎介紹

    Linux及計算機基礎介紹 計算機的組成及其功能 What:計算機的組成是什么? 計算機的主要部件: CPU:可分為運算器、控制器、寄存器、緩存等 存儲器:比如內存。執行程序時,會將程序及相關數據加載進內存,供CPU讀取 輸入設備:比如:鍵盤。用于下達指令及輸入數據。 輸出設備:比如顯示器。用戶顯示和輸出指令執行的結果等 馮諾依曼體系中提出的五大部件為:運算…

    Linux干貨 2016-09-19
  • Linux入門命令(一)

    已經一年沒有在接觸過Linux了,經過一個星期的Linux學習之后,又重新對她產生了興趣,本周學習了一些Linux入門級的命令: (1)     (1)tty 這個命令是查看當前所處于的中終端信息的,同樣也可以使用 who am i ,who以及w來查看終端信息. 終端也還有物理終端、虛擬終端以及偽終端之分,在虛擬機下…

    2017-07-16
  • Python內置數據結構

    Python內置數據結構 數值型 Int,float,complex,bool 序列對象 字符串:str ?列表:list ??數組:tuple 鍵值對 集合:set ???字典:dict 數值型 Int,float,complex,bool都是class,1,5.0,2+3j都是對象即示例 Int:python3的int就是長整形,且沒有大小限制,受限于內…

    Linux干貨 2018-03-26
欧美性久久久久