如果/ then / elseif在bash中工作,如何嵌套?

语法在bash中是如何工作的? 这是我的伪代码为C风格,如果其他语句。 例如:

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" 

我想你的问题是关于许多语法中包含的其他含糊不清的问题。 在bash中没有这样的事情。 if每个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