我必須編寫一個腳本來生成並執行包含文件名的可變數量參數的命令。這些文件名可能包含空格,並且所有內容都必須正確使用或不使用空格。在shell腳本中使用空格執行動態創建的命令
例如,這產生的命令可能是這樣的:
curl --data-urlencode "[email protected]/tmp/some folder/data.txt" http://www.someurl.com
如果我使用硬編碼的命令並執行它,它都運行良好,有和沒有空格。但是,如果我在字符串變量中創建命令文本並執行字符串內容,則似乎該命令不考慮引號,僅使用文件的第一部分:
腳本(簡化,只是想象命令字符串使用複雜的規則)發佈:
#!/bin/sh
#prepare
command="curl --data-urlencode \"[email protected]/tmp/some folder/data.txt\" www.someurl.com"
#execute
$command
結果:
$ ./test.sh
Warning: Couldn't read data from file ""[email protected]/tmp/some", this makes an
Warning: empty POST.
curl: (6) Couldn't resolve host 'folder'
我嘗試不同的東西,交換報價的風格,用的東西象exec,但我could'nt得到這個工作。
任何幫助,將不勝感激
感謝
注:我要補充所有這些測試是在Cygwin完成。關於路徑語法可能很重要。
嘗試使用替代空間 「\」 – spicavigo
@spicavigo試過已經沒有結果:/ – Shtong
你可以換出一些dbl-quotes for single quotes?,ie command =「curl --data-urlencode'param_value @/tmp/some folder/data.txt'www.someurl.com」...祝你好運! – shellter