And*_*say 21 linux bash if-statement nested
语法如何在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"
Run Code Online (Sandbox Code Playgroud)
mik*_*yra 43
我想你的问题是关于许多语法中包含的悬浮的其他歧义; 在bash中没有这样的事情.每个if都必须由fi标记if块结尾的伴侣分隔.
鉴于此事实(除了其他语法错误),您会注意到您的示例不是有效的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
Run Code Online (Sandbox Code Playgroud)