Isa*_*bel 0 linux bash if-statement conditional-statements
我试图接受用户对问题的输入是或否,并根据答案读回我的变量值。我永远无法让附加到变量的命令起作用,或者我的 if 语句接受是或否。它只是继续“不是一个有效的答案”。请让我知道如何真正让它们在 bash 脚本中工作。我一直在寻找不同的东西来尝试,但似乎没有任何效果。这是我现在所拥有的:
yesdebug='echo "Will run in debug mode"'
nodebug='echo "Will not run in debug mode"'
echo "Would you like to run script in debug mode? (yes or no)"
read yesorno
if [$yesorno == 'yes']; then
$yesdebug
elif [$yesorno == 'no']; then
$nodebug
else
echo "Not a valid answer."
exit 1
fi
Run Code Online (Sandbox Code Playgroud)
您的代码有几个问题:
[(这是命令名称)和](这是 的强制性但无功能的参数])周围放置空格。yesdebug () { echo "Will run in debug mode"; }
nodebug () { echo "Will not run in debug mode"; }
echo "Would you like to run script in debug mode? (yes or no)"
read yesorno
if [ "$yesorno" = yes ]; then
yesdebug
elif [ "$yesorno" = no ]; then
nodebug
else
echo "Not a valid answer."
exit 1
fi
Run Code Online (Sandbox Code Playgroud)