如何在 bash 中一遍又一遍地运行命令直到成功?

发布于 2024-10-21 07:28:35 字数 410 浏览 4 评论 0原文

我有一个脚本,想向用户询问一些信息,但在用户填写此信息之前,脚本无法继续。以下是我尝试将命令放入循环中以实现此目的,但由于某种原因它不起作用:

echo "Please change password"
while passwd
do
    echo "Try again"
done

我尝试了 while 循环的许多变体:

while `passwd`
while [[ "`passwd`" -gt 0 ]]
while [ `passwd` -ne 0 ]]
# ... And much more

但我似乎无法让它工作。

I have a script and want to ask the user for some information, but the script cannot continue until the user fills in this information. The following is my attempt at putting a command into a loop to achieve this but it doesn't work for some reason:

echo "Please change password"
while passwd
do
    echo "Try again"
done

I have tried many variations of the while loop:

while `passwd`
while [[ "`passwd`" -gt 0 ]]
while [ `passwd` -ne 0 ]]
# ... And much more

But I can't seem to get it to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

看轻我的陪伴 2024-10-28 07:28:35
until passwd
do
  echo "Try again"
done

或者

while ! passwd
do
  echo "Try again"
done
until passwd
do
  echo "Try again"
done

or

while ! passwd
do
  echo "Try again"
done
无语# 2024-10-28 07:28:35

为了详细说明@Marc B的答案,

$ passwd
$ while [ $? -ne 0 ]; do !!; done

这是一种很好的方法来完成与命令无关的相同操作。

如果您想将其作为别名来执行此操作(感谢@Cyber​​wiz):

alias rr='while [ $? -ne 0 ]; do eval $(history -p !!); done'

用法:

$ passwd
$ rr

To elaborate on @Marc B's answer,

$ passwd
$ while [ $? -ne 0 ]; do !!; done

Is a nice way of doing the same thing that's not command specific.

If you want to do this as an alias (kudos to @Cyberwiz):

alias rr='while [ $? -ne 0 ]; do eval $(history -p !!); done'

Usage:

$ passwd
$ rr
北斗星光 2024-10-28 07:28:35

您需要改为测试 $?,这是上一个命令的退出状态。如果一切正常,passwd 将以 0 退出;如果 passwd 更改失败(密码错误、密码不匹配等),则以非零退出(密码错误、密码不匹配等)。

passwd
while [ $? -ne 0 ]; do
    passwd
done

使用反引号版本,您将比较 passwd 的输出,其中会像输入密码确认密码之类的东西。

You need to test $? instead, which is the exit status of the previous command. passwd exits with 0 if everything worked ok, and non-zero if the passwd change failed (wrong password, password mismatch, etc...)

passwd
while [ $? -ne 0 ]; do
    passwd
done

With your backtick version, you're comparing passwd's output, which would be stuff like Enter password and confirm password and the like.

断念 2024-10-28 07:28:35

如果有人希望有重试限制:

max_retry=5
counter=0
until <YOUR_COMMAND>
do
   sleep 1
   [[ counter -eq $max_retry ]] && echo "Failed!" && exit 1
   echo "Trying again. Try #$counter"
   ((counter++))
done

If anyone looking to have retry limit:

max_retry=5
counter=0
until <YOUR_COMMAND>
do
   sleep 1
   [[ counter -eq $max_retry ]] && echo "Failed!" && exit 1
   echo "Trying again. Try #$counter"
   ((counter++))
done
帅气尐潴 2024-10-28 07:28:35

您可以使用无限循环来实现此目的:

while true
do
  read -p "Enter password" passwd
  case "$passwd" in
    <some good condition> ) break;;
  esac
done

You can use an infinite loop to achieve this:

while true
do
  read -p "Enter password" passwd
  case "$passwd" in
    <some good condition> ) break;;
  esac
done
枉心 2024-10-28 07:28:35
while [ -n $(passwd) ]; do
        echo "Try again";
done;
while [ -n $(passwd) ]; do
        echo "Try again";
done;
羁〃客ぐ 2024-10-28 07:28:35

如果您想要严格模式(set -e)并且以上都不起作用,那就有点棘手了。

set -euo pipefail

counter=0
max_attempts=3
while ret=0; <your command> || ret=$?; [ $ret -ne 0 ]
do
  [[ $counter -eq $max_attempts ]] && echo "Command failed"; exit 1
  sleep 2
  echo "Trying again"
  counter=$((counter+1))
done

It becomes a little tricky if you want the strict mode (set -e) and none of above worked.

set -euo pipefail

counter=0
max_attempts=3
while ret=0; <your command> || ret=$?; [ $ret -ne 0 ]
do
  [[ $counter -eq $max_attempts ]] && echo "Command failed"; exit 1
  sleep 2
  echo "Trying again"
  counter=$((counter+1))
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文