bash中的语法如何工作?这是我的C style if if语句的伪代码。例如:
If (condition)
then
echo "do this stuff"
elseif (condition)
echo "do this stuff"
elseif (condition)
echo "do this stuff"
if(condition)
then
echo "this is nested inside"
else
echo "this is nested inside"
else
echo "not nested"
答案 0 :(得分:34)
我猜你的问题是关于许多语法中包含的悬殊的歧义;在bash中没有这样的事情。每个if
都必须由标记if块结尾的伴随fi
分隔。
鉴于此事实(除了其他语法错误),您会注意到您的示例不是有效的bash脚本。试图解决一些你可能得到的错误
if condition
then
echo "do this stuff"
elif condition
then
echo "do this stuff"
elif condition
then
echo "do this stuff"
if condition
then
echo "this is nested inside"
# this else _without_ any ambiguity binds to the if directly above as there was
# no fi colosing the inner block
else
echo "this is nested inside"
# else
# echo "not nested"
# as given in your example is syntactically not correct !
# We have to close the last if block first as there's only one else allowed in any block.
fi
# now we can add your else ..
else
echo "not nested"
# ... which should be followed by another fi
fi