一、if条件判断
1. 语法格式:
if
then
else
fi
2. 示例:
如果上条命令执行正确,返回commend execute successful
如果上条命令执行错误,返回commend execute failed
[root@localhost script]# vim if.sh
[root@localhost script]# chmod +x if.sh
[root@localhost script]# cat if.sh
#!/bin/bash
ls &>> /dev/null
if [ "$?"==0 ]
then
echo "commend execute successful"
else
echo "commend execute failed"
fi
[root@localhost script]# ./if.sh
commend execute successful
#同上,只是把 $? 的引号去掉了:
[root@localhost script]# cat if.sh
#!/bin/bash
ls &>> /dev/null
if [ $? == 0 ]
then
echo "commend execute successful"
else
echo "commend execute failed"
fi
[root@localhost script]# ./if.sh
commend execute successful
[root@localhost script]# cat ifwrong.sh
#!/bin/bash
Ls &>> /dev/null
if [ "$?" == 0 ]
then
echo "commend execute successful"
else
echo "commend execute failed"
fi
[root@localhost script]