shell编程中的条件判断
条件
if-then
case
if-then
单条件
if command
then
commands
fi
当command返回码为0时 条件成立
if.sh
#! /bin/bash if date then echo "command exec" fi if date123 then echo "command exec1" fi echo "out if" [[email protected] sh]# ./if.sh 2016年 10月 29日 星期六 10:39:18 EDT command exec ./if.sh: line 8: date123: command not found out if
全覆盖
if command
then
commands
else
commands
fi
if.sh
#! /bin/bash if date1 then echo "command" echo "ok" else echo "commond1" echo "fail" fi [[email protected] sh]# ./if.sh ./if.sh: line 2: date1: command not found commond1 fail
条件嵌套
if command1
then
commands
if command2
then
commands
fi
commands
fi
多条件
if command1
then
commands
elif command2
then
commands
else
commands
fi
test命令:
第一种形式
if test condition
then
commands
fi
第二种形式
中括号 []:
if [ condition ]
then
commands
fi
复合条件判断
[ condition1 ] && [ condition2 ]
[ condition1 ] || [ condition2 ]
三类条件
数值比较
字符串比较
文件比较
if.sh
echo plese input a num read num if [ $num -gt 10 ] then echo "the num>10" fi if [ $num -lt 10 ] && [ $num -gt 5 ] then echo "the 5<num<10" fi [[email protected] sh]# ./if.sh plese input a num 11 the num>10
待续
时间: 2024-10-13 00:55:22