我正在編寫一個腳本來刪除目錄中的所有文件以供練習。我使用引號我的變量,但我仍然得到以下錯誤:bash中太多參數錯誤
/usr/local/bin/deleteall: line 6: [: too many arguments
/usr/local/bin/deleteall: line 11: [: too many arguments
這裏是我的代碼:
#!/bin/bash
#Deletes all files in the current directory
read -p "You are about to delete all files in $(pwd). Are you sure you want to do this? y/n" yn
echo $yn
if [ [ "$yn" = "y" ] -o [ "$yn" = "Y" ] ] ; then
for i in `ls`; do
rm $i
done
exit;
elif [ [ "$yn" = "n" ] -o [ "$yn" = "N" ] ] ; then
exit;
else
read -p "Please enter y (yes) or n (no)"
exit;
fi
這是整個輸出:
You are about to delete all files in <my dir>. Are you sure you want to do this? y/nn
n
/usr/local/bin/deleteall: line 6: [: too many arguments
/usr/local/bin/deleteall: line 11: [: too many arguments
Please enter y (yes) or n (no)n
我在做什麼錯?
謝謝!這解決了它。爲什麼我的原始代碼沒有工作? –
@WilliamOliver:因爲你的語法不正確。它應該是:'[「$ yn」='y'-o「$ yn」='Y']' – anubhava
@mrjink不在Bash條件中 –