一、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]

本文详细介绍了Linux shell中的if条件判断、case语句、for循环、while循环和until循环的语法和使用示例,包括逻辑运算符、用户输入交互、并发执行与等待等关键概念。通过实例解析,帮助读者掌握shell脚本编程中的循环控制结构。
最低0.47元/天 解锁文章
3264

被折叠的 条评论
为什么被折叠?



