使用if -then语句
if -then语句有如下格式。(感觉等价于C语言的if)
if command
then
commands
fi
bash shell的if语句会运行if后面的那个命令。如果该命令的退出状态码是0表示该命令成功运行,位于then部分的命令就会被执行。如果该命令的退出状态码是其他值,then部分的命令就不会被执行。fi表示if-then语句到此结束。
使用if -then-else语句
if -then -else语句有如下格式。(感觉等价于C语言中的if -else)
if command
then
commands
else
commands
fi
当if语句中的命令返回退出状态码0时, then部分中的命令会被执行,这跟普通的if-then语句一样。当if语句中的命令返回非零退出状态码时, bash shell会执行else部分中的命令。