我正在尝试使用bash进行仿真,但没有用。代码如下
# an emulation of the do-until loop
do_stuff() {
echo "Enter password"
read password
if [[ $password != "theflow" ]]
then
echo " Sorry, try again."
fi
}
do_stuff
until (( $password == "theflow" ))
do
do_stuff
done
而不是在两个不同的地方比较$password
,我认为使用函数的返回码指示检查是否成功是更有意义的:
check_password () {
echo "Enter password"
read password
if [[ $password != 'theflow' ]]; then
echo ' Sorry, try again.'
return 1
fi
return 0
}
然后您的until
循环可以是:
until check_password; do
:
done
[它将继续调用check_password
,直到返回0
(成功)。