if commands; then
commands
[elif commands; then
commands...]
[else
commands...]
[else
commands]
fi
这里if后面的是否为True,和C语言中非零就是True正好相反,它代表测试command是否执行成功。而一般command执行成功的返回值是0。举个例子,有如下bash脚本try.sh
#! /bin/bash
check() {
return 1
}
if check; then
echo haha
else
echo sb
fi
运行这个脚本
tianyxu@love_server1:~$ ./try.sh
sb