Bash if then else syntax [duplicate]

橙三吉。 提交于 2019-12-02 12:40:15

Bash if statements require a semi-colon before the then:

if [ condition ] || [ condition ]; then
    # code
elif [ condition ] && [ condition ]; then
    # code
fi

For example.

To help anyone who might look at this for help in the future, I figured I'd answer my own question with all the syntax errors I found from my own testing and with the helpful responses of others. To start the variable assignment:

j = 0

you can't have spaces in between, so it would be:

j=0

Also if statements need a space between if and the bracket and need a semicolon after the last bracket before then. Therefore my incorrect if statement

if[ "$j" -eq 0 ] && [ "$k" -eq 0 ] then

becomes

if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]; then

or instead of a semicolon you can have a new line between the bracket, so it would become

if [ "$j" -eq 0 ] && [ "$k" -eq 0 ]
then
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!