我試圖弄清楚如何解決此代碼...echo -n "Enter you complaint : ";read complaint;if [[ -z $complaint ]] || [[ $complaint != 'FIRE' ]] || [[ $complaint != 'INTOXICATION' ]] || [[ $complaint != "INJURY" ]] || [[ $complaint != "BREAKAGE" ]] ;then echo "Not a good option"; exit 1;ficase $complaint in FIRE) = $complaint=$(echo "FIRE");; INTOXICATION) = $complaint=$(echo "INTOXICATION");; INJURY) = $complaint=$(echo "INJURY");; BREAKAGE) = $complaint=$(echo "BREAKAGE");;esac是否可能知道:為什么即使我鍵入有效的內(nèi)容也總是輸入?
1 回答

MM們
TA貢獻1886條經(jīng)驗 獲得超2個贊
中的條件if有誤,應(yīng)為:
if [[ -z $complaint ]] || [[ ( $complaint != 'FIRE' && $complaint != 'INTOXICATION' && $complaint != "BRIS" && $complaint != "BOBO" ) ]] ;then
也就是說,您想進入ifif$complaint是null(-z $complaint)或與任何有效選項都不匹配的情況,因此它必須同時與所有選項都不同,因此應(yīng)&&改為||。
或者,您也可以在case...esac塊中使用默認條件:
case $complaint in
FIRE) = $complaint=$(echo "FIRE");;
INTOXICATION) = $complaint=$(echo "INTOXICATION");;
INJURY) = $complaint=$(echo "INJURY");;
BREAKAGE) = $complaint=$(echo "BREAKAGE");;
*) echo "Not a good option"; exit 1;;
esac
添加回答
舉報
0/150
提交
取消