猿问

如何确保用户输入有效的开关盒选项?

我试图弄清楚如何解决此代码...


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;

fi



case $complaint in

    FIRE)  = $complaint=$(echo "FIRE");;

    INTOXICATION) = $complaint=$(echo "INTOXICATION");;

    INJURY) = $complaint=$(echo "INJURY");;

    BREAKAGE) = $complaint=$(echo "BREAKAGE");;

esac

是否可能知道:为什么即使我键入有效的内容也总是输入?


芜湖不芜
浏览 240回答 1
1回答

MM们

中的条件if有误,应为:if [[ -z $complaint ]] || [[ ( $complaint != 'FIRE' && $complaint != 'INTOXICATION' && $complaint != "BRIS" && $complaint != "BOBO" ) ]] ;then也就是说,您想进入ifif$complaint是null(-z $complaint)或与任何有效选项都不匹配的情况,因此它必须同时与所有选项都不同,因此应&&改为||。或者,您也可以在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
随时随地看视频慕课网APP
我要回答