bash中的do-until仿真不起作用

问题描述 投票:0回答:1

我正在尝试使用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
linux bash shell control-flow until-loop
1个回答
0
投票

而不是在两个不同的地方比较$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(成功)。

© www.soinside.com 2019 - 2024. All rights reserved.