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

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

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

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

相關推薦

  • 系統時間與硬件時間

    1. “系統時間”與“硬件時間”    系統時間: 一般說來就是我們執行 date 命令看到的時間,linux系統下所有的時間調用(除了直接訪問硬件時間的命令)都是使用的這個時間。    硬件時間: 主板上BIOS中的時間,由主板電池供電來維持運行,系統開機時要讀取這個時間,并根…

    Linux干貨 2016-08-05
  • 第一周作業

    1、描述計算機的組成及其功能。
    2、按系列羅列Linux的發行版,并描述不同發行版之間的聯系與區別。
    3、描述Linux的哲學思想,并按照自己的理解對其進行解釋性描述。
    4、說明Linux系統上命令的使用格式;詳細介紹ifconfig、echou、tty、startx、export、pwd、history、shutdown、poweroff、reboot、hwclock、date命令的使用,并配合相應的示例來闡述。
    5、如何在Linux系統上獲取命令的幫助信息,請詳細列出,并描述man文檔的章節是如何劃分的。
    6、請羅列Linux發行版的基礎目錄名稱命名法則及共用規定。

    Linux干貨 2017-12-03
  • grep與正則表達式

    grep與正則表達式 grep與正則表達式 grep 正則表達式 grep 使用語法:grep [-abcEFGhHilLnqrsvVwxy][-C<顯示列數>][-e<范本樣式>][-f<范本文件>][范本樣式][文件或目錄…]  功能說明:查找指定文件或標準輸入里符合條件的字符串 常用選項 -a…

    Linux干貨 2016-11-05
  • OSI和TCP/IP

            我們生活中各方面都離不開網絡,那網絡究竟是什么東西呢?它是怎么做到讓在不同地點的人無視空間距離完成通信的呢?首先我們來了解一下網絡的基本概念。網絡是由節點和連線構成,表示諸多對象及其相互聯系。在數學上,網絡是一種圖,一般認為專指加權圖。網絡除了數學定義外,還有具體…

    2017-09-02
  • linux篩選數據和用戶基本操作

    linux篩選數據的基本操作 列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。   [root@localhost script]# who   chenjianhang tty1      …

    Linux干貨 2016-10-17
  • 94-HAProxy

    一. HAProxy簡介 1. LB CLuster:

    2016-11-18
欧美性久久久久