如何在 bash 中一遍又一遍地运行命令直到成功? [英] How can you run a command in bash over and over until success?

查看:23
本文介绍了如何在 bash 中一遍又一遍地运行命令直到成功?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

推荐答案

until passwd
do
  echo "Try again"
done

while ! passwd
do
  echo "Try again"
done

这篇关于如何在 bash 中一遍又一遍地运行命令直到成功?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆