Shell脚本执行报错:line 4: 1: command not found 求助
问题解决:Shell脚本
command not found错误 你的报错根源是if条件判断的语法错误:Shell中直接写if $gamenum -eq 1时,会把$gamenum的取值当作命令执行,比如用户输入1,系统会尝试运行1 -eq 1,这显然不是合法命令,因此触发报错。
修正后的脚本代码
echo "Select the Game You Want To Play!" echo "[1] Random Number" read gamenum # 用[ ]包裹条件,注意符号前后必须留空格,-eq是整数比较运算符 if [ $gamenum -eq 1 ] then chmod +x Games/RandNum.sh bash Games/RandNum.sh else echo "Incorrect" exit fi
额外优化建议
可以增加输入验证,避免用户输入非数字内容导致脚本异常:
echo "Select the Game You Want To Play!" echo "[1] Random Number" read gamenum # 先校验输入是否为有效整数 if ! [[ $gamenum =~ ^[0-9]+$ ]]; then echo "Please enter a valid number" exit 1 fi if [ $gamenum -eq 1 ] then chmod +x Games/RandNum.sh bash Games/RandNum.sh else echo "Incorrect" exit fi
其他可选的判断写法
使用双括号(( ))做算术判断(语法更简洁,对空格要求宽松):
if (( gamenum == 1 )) then # 执行游戏启动逻辑 fi
内容的提问来源于stack exchange,提问作者haritanshu93
相关产品推荐
相关产品推荐

