if 是判断语句,if语句的作用跟 [ ] 差不多,一般判断比较多或者执行的语句比较多的话,那么就会使用if
第一种格式
if [ 判断条件 ];then
    内容
else
    内容
fi第二种格式,多重判断
if [ 判断条件 ];then
    内容
elif [ 判断条件 ];then
    内容
else
    内容
fiif 语句后面的 [ ] 两边必须有空格
1.if判断
判断/root/a.txt是否存在,如果存在,echo 0 ,如果不存在,echo1
#!/bin/bash -
if [ -f /root/a.txt ];then
        echo 0
else
        echo 1
fi2.if...elif...else...
判断/root/a.txt 是否 存在,如果存在,则echo a.txt,判断/root/b.txt是否存在,如果存在,则 echo b.txt,如果a.txt 和 b.txt 都不存在,则输出error
#!/bin/bash -
if [ -f /root/a.txt ];then
        echo "a.txt"
elif [ -f /root/b.txt ];then
        echo "b.txt"
else
        echo "error"
fi原文:http://blog.51cto.com/xiaowangzai/2090020