2016-11-17 125 views
1

我試圖在這裏實現一些非常簡單的事情,但缺少一些東西。Bash:Path with

我想通過bash腳本(.sh)創建一個文件。

當我直接終端上我嘗試:

touch /Users/luco/Downloads/My\ Test\ Folder/test.txt 

它創建.txt文件沒有問題。但是,當我直接嘗試我的腳本時:

#!/bin/sh 
clear 
touch "$1"/test.txt 

它不起作用。給我這個消息:

.../test.txt: No such file or directory 

我打電話這種方式的腳本:

./Script.sh "/Users/luco/Downloads/My\ Test\ Folder/" 
./Script.sh /Users/luco/Downloads/My\ Test\ Folder/ 

無工作。

我在做什麼錯?

在此先感謝。

+0

你是如何調用腳本? – chepner

+0

如果將它作爲'./the_script/Users/luco/Downloads/My \ Test \ Folder'運行會怎麼樣? – Arkku

+1

在您的問題中給出的腳本沒有表現出所述的問題。 –

回答

3

使用在調用時既引號和反斜線會產生同樣的錯誤,使用要麼此:

#!/bin/sh 
touch "$1/test.txt" 

...或這個:

#!/bin/sh 
touch "$1"/test.txt 

問題就如此複製:

$ ./test.sh "/Users/admin/Developmemt/Pippo\ Pelo" 
touch: /Users/admin/Developmemt/Pippo\ Pelo/test.txt: No such file or directory 

我得到的錯誤(由於反斜槓在引號內 - "/" - 這意味着文字反斜槓有望成爲目錄名稱的一部分)。


相反,如果不使用引號,反斜線被解讀爲一個信號外殼,而不是作爲目錄名稱的一部分,所以它的工作原理:

$ ./test.sh /Users/admin/Developmemt/Pippo\ Pelo