如何从bash的if循环中“突破"? [英] how to 'break' out of an if loop in bash?

查看:53
本文介绍了如何从bash的if循环中“突破"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在bash的if循环中使用 break 时,它告诉我它对bash无效,我该怎么用呢?

When I use break in an if loop in bash it tells me its not valid for bash, what can I use instead?

用例是,向用户询问一个问题,如果他回答否",则脚本应跳至下一部分.

The use case is, the user is asked a question and if he answers 'no', the script should skip to the next section.

if [[ $ans1_1 = "y" ]]; then
    fedoraDeps
elif [[ $ans1_1 = "n" ]]; then
    break
else
    echo "Answer 'y' or 'n' "
fi

推荐答案

if 语句不是循环",因此中断它们是没有意义的.如果您希望其中一个块为无操作块,则可以使用内置的:命令,该命令什么都不做:

if statements are not "loops", so it doesn't make sense to break out of them. If you want one of your blocks to be a no-op, you can use the built-in : command, which simply does nothing:

if [[ $ans1_1 = y ]]; then
    fedoraDeps
elif [[ $ans1_1 = n ]]; then
    :
else
    echo "Answer 'y' or 'n'"
fi

这篇关于如何从bash的if循环中“突破"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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