You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 04:59:53