Linux Bash if失败返回到上一个命令

Linux Bash if失败返回到上一个命令,linux,bash,Linux,Bash,我有一个安装windows共享的脚本,我用它来学习bash脚本 我面临的问题是,有一个结果写在zenity中,因为一旦安装了共享,它就会发出OK,当它无法安装时,它就会发出FAIL 现在我想再次询问用户他的密码,如果输出失败,那么如果她/他在密码中有错误,她/他可以重写它 输入 ## define a function that launched the zenity username dialog get_password(){ zenity --entry --width=300 -

我有一个安装windows共享的脚本,我用它来学习bash脚本

我面临的问题是,有一个结果写在zenity中,因为一旦安装了共享,它就会发出OK,当它无法安装时,它就会发出FAIL

现在我想再次询问用户他的密码,如果输出失败,那么如果她/他在密码中有错误,她/他可以重写它

输入

## define a function that launched the zenity username dialog
get_password(){
    zenity --entry --width=300 --title="Mount $MOUNTDIR" --text="Password:" --hide-text
}

# attempt to get the password and exit if cancel was pressed
wPassword=$(get_password) || exit

# if the username is empty or matches only whitespace.
while [ "$(expr match "$wPassword" '.')" -lt "1" ]; do
    zenity --error --title="Error in password!" --text="Please check your password! Password field can not be empty!" || exit
    wPassword=$(get_password) || exit
done
输出:

# show if mounting was OK or failed
if [ $? -eq 0 ]; then
        zenity --info --title="Mounting public share succeeded!" --text="Location Documents/Shares/public!"
else
        zenity --error --title="Mounting public did not succeed!" --text="Please contact system administrator!"
fi

因此,如果输出失败,我需要脚本重新运行输入。我希望你能理解我的需要。

我想你想要的是:

while wPassword=$(get_password)
do
    if mount …options for mount…
    then
        zenity --info --title="Mounting public share succeeded!" \
               --text="Location Documents/Shares/public!"
        break
    else
        zenity --error --title="Mounting public did not succeed!" \
               --text="Please contact system administrator!"
    fi
done
这将运行
get_password
功能并保存输出;如果
get_password
函数返回非零状态,循环将终止。在循环体中,它运行
mount
命令,如果成功,则报告成功并中断循环。如果失败,它会给出一条错误消息(带有一条不再完全合适的消息),然后返回循环读取新密码

…但问题是我在脚本中有保存密码功能,我需要在它给出成功状态之前启动该功能

有多个选项可供选择:

while wPassword=$(get_password)
do
    if ! password_saver "$wPassword"
    then break    # Password saver reported error, presumably
    fi
    if mount …options for mount…
    …
或:


如果密码保护程序只指示错误而不是报告错误,那么您需要在代码中报告问题;这将使第一种选择更好。您可以将
中断
替换为
继续
,以再次循环。如果“密码保存程序错误时停止循环”是正确的,那么第二个更简洁。

您需要
while
循环,并且
成功后中断
,您可以用一个简单的示例进行解释吗?是的,这是可行的,但问题是我在脚本中有保存密码功能,我需要在它给出成功状态之前启动它。例子:虽然。。。。然后在give success else error后保存。因此,在执行
do
之后,运行
save
操作,然后执行
mount
。检查保存状态是否出错取决于您。
while wPassword=$(get_password) &&
      password_saver "$wPassword"
do
    if mount …options for mount…
    …