嗨,我有一个简单的问题。如何打破 if 语句?例如
if [condition]
then **Iwant to to end here**
else
fi
我不知道该使用什么,因为 break 不起作用。请帮忙。谢谢你。
嗨,我有一个简单的问题。如何打破 if 语句?例如
if [condition]
then **Iwant to to end here**
else
fi
我不知道该使用什么,因为 break 不起作用。请帮忙。谢谢你。
您需要在命令名称周围[
和最后一个参数之前使用空格]
。
如果您有!
运算符(如 中bash
),则可以反转条件:
if ! [ condition ]
then : contents of original else clause
fi
或者:
if [ ! condition ]
then : contents of original else clause
fi
或者你可以写出条件的倒数:
if [ inverse-condition ]
then : contents of original else clause
fi
then
或者你可以在子句中什么都不做:
if [ condition ]
then : Do nothing
else : contents of original else clause
fi
你误会了。if 语句仅在条件满足时执行,如果不满足,则不会执行任何操作。但是如果你想跳到别的地方,你可以使用 GOTO
喜欢
转到测试
测试:
是的,break
只能在循环结构内工作,while ... do ; ... done
或者for ... ; do ... done
如果您的代码位于函数内部,则可以使用return
从任何位置(包括if .. ; then ... fi
构造内部)离开函数。
或者,如果您不在函数中,则必须使用exit
.
假设您使用bash|ksh|zsh
的是没有goto :label
. 这将是使用[t]csh
家庭的少数原因之一。
IHTH