我寫了這段代碼將一個目錄移動到另一個。 我想要的代碼如下:如何將默認值傳遞給shell腳本中的變量?
- 用戶將給出一個文件名或目錄名。
- 用戶可以給一個目標文件夾。如果用戶不需要目的地文件夾,他/她只需按下輸入鍵。
- 然後源目錄將被複制到目的地目錄(如果給定)或默認的目錄(如果沒有給定目的地)
下面是代碼。
$source_folder
$destination_folder
$destination
read -r directory_source
read -r destination_folder
if [ "$destination_folder" = ""]
then
$destination = "/var/www/html/"
else
$destination = "$destination_folder"
fi
cp -a "$source_folder" "$destination"
這裏是輸入到該程序:
Myfiles.sh (called the shell script in terminal)
Max Sum (This is the source directory)
(Pressed enter i.e. No destination given)
它提供了以下錯誤:
/usr/local/myfiles/copyDirToHTML.sh: line 6: [: : unary operator expected
/usr/local/myfiles/copyDirToHTML.sh: line 10: =: command not found
cp: cannot stat ‘’: No such file or directory
是什麼問題? – Prashant 2015-02-06 08:58:22
需要交互式輸入的腳本不太有用,因爲它們不能作爲構建塊併入較大的腳本中,並且會放棄shell等有用的交互功能,例如製表符擴展等。一個明智的'cpd'(用於默認複製)將是簡單爲'cpd(){cp「$ 1」「$ {2-/path/to/default}」; }' – tripleee 2015-02-07 07:37:34
謝謝,我只是在學習。將嘗試你的解決方案。 @tripleee – InsomniacSabbir 2015-02-07 13:17:31