if 语句认为它在函数完成时自行完成。在这个例子中得到了更好的展示,并且脚本不会循环回到 echo 开始。
echo start
why () {
read -p 'Loop now? [y/n]' answer
case $answer in
y | Y | yes | YES ) answer="y";;
*) echo 'Invalid response -- exiting now'
exit;;
esac
}
answer=y
if [ $answer = 'y' ]
then
why
echo "Ok done running the if statement ready to exit the condition now"
fi
所以来回循环一个函数来包装 if 语句,如下所示:
echo start
why () {
read -p 'Loop now? [y/n]' answer
case $answer in
y | Y | yes | YES ) answer="y";;
n | N | no | NO ) answer="n";;
*) echo 'Invalid response -- exiting now'
exit;;
esac
ifFunction
}
ifFunction () {
if [ $answer = 'y' ]
then
why
echo "yes"
elif [ $answer = 'n' ]
then
echo "to bad looping anyway"
why
else
echo "else nothing but another question"
why
fi
}
why
echo never
所以答案是它已经完成了该功能,现在按预期继续执行脚本,在提供的示例中,它会回显 end 作为下一个要完成的任务。