Shell脚本条件测试
语法 | 说明 |
test | 利用test命令进行条件测试 |
[] | 通过[]进行条件测试 |
[[]] | 通过[[]]进行条件测试 |
(()) | 通过(())进行条件测试 |
test语法
test -f file && echo true || echo false #存在输出true,不存在输出false
[]语法
[ -f /tmp/123.txt ] && echo 1 || echo 0 #存在输出1,不存在输出0
[[]]语法
[[ -f /tmp/123.txt ]] &&echo 1 || echo 0 #存在输出1,不存在输出0
文件测试表达式
文件测试操作符 | 说明 |
-d 文件 | 文件存在且为目录为真,及测试表达式成立 |
-f 文件 | 文件存在且为普通文件为真,及测试表达式成立 |
-e 文件 | 文件存在即为真,则表达式成立,不区别文件和目录 |
-r 文件 | 文件存在且可读为真,及测试表达式成立 |
-w 文件 | 文件存在且可写为真,及测试表达式成立 |
-x 文件 | 文件存在且可执行为真,及测试表达式成立 |
-s 文件 | 文件存在且文件大小不为0为真,及测试表达式成立 |
-L 文件 | 文件存在且为链接文件为真,及测试表达式成立 |
f1 -nt f2 | 文件f1比文件f2新则为真,即表达式成立,根据文件的修改时间来计算 |
f1 -ot f2 | 文件f1比文件f2旧则为真,即表达式成立,根据文件的修改时间来计算 |
字符串测试表达式
字符串测试符 | 说明 |
-n "字符串" | 若字符串不为“0”,则为真,即表达式成立 |
-z "字符串" | 若字符串为“0”,则为真,即表达式成立 |
"串1" = "串2" | 若字符串1等于字符串2,则为真,即表达式成立 |
"串1" != "串2" | 若字符串1不等于字符串2,则为真,即表达式成立 |
整数二次元比较符
在[]以及test中使用的比较符 | 在(())和[[]]中使用的比较符 | 说明 |
-eq | ==或者= | 相等 |
-ne | != | 不相等 |
-gt | > | 大于 |
-ge | >= | 大于等于 |
-lt | < | 小于 |
-le | <= | 小于等于 |
root@cs:/server/scripts# a1=98;a2=64 | |
root@cs:/server/scripts# [ "$a1" -eq "$a2" ] && echo 1 || echo 00 | |
root@cs:/server/scripts# [ "$a1" -ne "$a2" ] && echo 1 || echo 01 | |
root@cs:/server/scripts# [ "$a1" -gt "$a2" ] && echo 1 || echo 01 | |
root@cs:/server/scripts# [ "$a1" -lt "$a2" ] && echo 1 || echo 00 |
逻辑操作符
在[]和test中使用的操作符 | 在[[]]和(())中使用的操作符 | 说明 |
-a | && | and,与,两端为真,则结果为真 |
-o | 双竖线 | or,或,两端一个为真,则结果为真 |
! | ! | not,非,两端相反,则结果为真 |
使用-a和&&时
当左边为真,右边为假时,结果为假。 当左边为假,右边为真时,结果为假。 当左边为真,右边为真时,结果为真。 当左边为假,右边为假时,结果为假。
root@cs:/server/scripts# [ 5 -eq 6 -a 7 -ne 5 ] && echo 1 || echo 0 #5等于7?假 7不等于5? 真 结果为假 输出0 | |
0 | |
root@cs:/server/scripts# [ 5 -lt 6 -a 7 -ne 5 ] && echo 1 || echo 0 #5<7?真 7不等于5? 真 结果为真 输出1 | |
1 |
使用-o 或 双竖线时
当左边为真,右边为假时,结果为真。 当左边为假,右边为真时,结果为真。 当左边为真,右边为真时,结果为真。 当左边为假,右边为假时,结果为假。
root@cs:/server/scripts# [ 5 -lt 6 -o 7 -ne 5 ] && echo 1 || echo 0 | |
1 | |
root@cs:/server/scripts# [ 5 -eq 6 -o 7 -ne 5 ] && echo 1 || echo 0 | |
1 | |
root@cs:/server/scripts# [ 5 -gt 6 -o 7 -lt 5 ] && echo 1 || echo 0 | |
0 | |
root@cs:/server/scripts# f1=/etc/rc.local;f2=/etc/services | |
root@cs:/server/scripts# echo -ne "$f1 $f2\n" | |
/etc/rc.local /etc/services | |
root@cs:/server/scripts# [ -f "$f1" -a -f "$f2" ] && echo 1 || echo 0 | |
1 | |
root@cs:/server/scripts# [ -f "$f1" -a -f "$f222" ] && echo 1 || echo 0 | |
0 | |
root@cs:/server/scripts# [ -f "$f1" -o -f "$f222" ] && echo 1 || echo 0 | |
1 | |
root@cs:/server/scripts# [ -f "$f111" -o -f "$f222" ] && echo 1 || echo 0 | |
0 | |
root@cs:/server/scripts# [ -f "$f1" ] && [ -f "$f2" ] && echo 1 || echo 0 | |
1 | |
root@cs:/server/scripts# a="test";b="echo" | |
root@cs:/server/scripts# echo -eq "$a $b\n" | |
-eq test echo\n | |
root@cs:/server/scripts# echo -ne "$a $b\n" | |
test echo | |
root@cs:/server/scripts# [[ ! -n "$a" && "$a" = "$b" ]] && echo 0 || echo 1 | |
1 | |
root@cs:/server/scripts# [[ -z "$a" && "$a" != "$b" ]] && echo 0 || echo 1 | |
1 | |
root@cs:/server/scripts# [[ -z "$a" || "$a" != "$b" ]] && echo 0 || echo 1 | |
0 | |
root@cs:/server/scripts# [[ -z "$a" -o "$a" != "$b" ]] && echo 0 || echo 1 | |
-bash: syntax error in conditional expression | |
-bash: syntax error near `-o' | |
root@cs:/server/scripts# m=21;n=38 | |
root@cs:/server/scripts# [ $m -gt 20 -a $n -lt 30 ] && echo 1 || echo 0 | |
0 | |
root@cs:/server/scripts# [ $m -gt 20 ] && [ $n -lt 30 ] && echo 1 || echo 0 | |
0 | |
root@cs:/server/scripts# [ $m -gt 20 ] || [ $n -lt 30 ] && echo 1 || echo 0 | |
1 | |
逻辑操作符
输入或通过命令行输入一个数字,如果传入的数字等于1,就打印1,输入2,就打印2,输入其他数字就退出程序
root@cs:/server/scripts# cat ljczf.sh | |
#!/bin/bash | |
read -p "please input num:" num | |
[ "$num" = "1" ] && { | |
echo 1 | |
exit 0 | |
} | |
[ "$num" = "2" ] && { | |
echo 2 | |
exit 0 | |
} | |
[ "$num" != "1" -a "$num" != "2" ] && { | |
echo error | |
exit 1 | |
} | |
传参方式 | |
root@cs:/server/scripts# cat ljczf1.sh | |
#!/bin/bash | |
echo "plase input num" | |
num=$1 | |
[ "$num" = "1" ] && { | |
echo 1 | |
exit 0 | |
} | |
[ "$num" = "2" ] && { | |
echo 2 | |
exit 0 | |
} | |
[ "$num" != "1" -a "$num" != "2" ] && | |
{ | |
echo error | |
exit 1 | |
} |
比较两个整数的大小
root@cs:/server/scripts# cat int.sh | |
#!/bin/bash | |
read -p "pls input two num:" num1 num2 | |
[ -z "$num1" -a -z "$num2" ] && { | |
echo error | |
exit 1 | |
} | |
expr $num1 + 1 &>/dev/null | |
RETVAL_n1=$? | |
expr $num2 + 1 &>/dev/null | |
RETVAL_n2=$? | |
[ $RETVAL_n1 -eq 0 -a $RETVAL_n2 -eq 0 ] || { | |
echo "pls input two num again" | |
exit 2 | |
} | |
[ $num1 -lt $num2 ] && { | |
echo "$num1 < $num2" | |
exit 0 | |
} | |
[ $num1 -eq $num2 ] && { | |
echo "$num1 = $num2" | |
exit 0 | |
} | |
[ $num1 -gt $num2 ] && { | |
echo "$num1 > $num2" | |
} | |
root@cs:/server/scripts# sh int.sh | |
pls input two num:6 3 | |
6 > 3 | |
root@cs:/server/scripts# sh int.sh | |
pls input two num:3 6 | |
3 < 6 | |
root@cs:/server/scripts# sh int.sh | |
pls input two num:6 6 | |
6 = 6 | |
root@cs:/server/scripts# sh int.sh | |
pls input two num:dd ff | |
pls input two num again | |
root@cs:/server/scripts# cat int1.sh | |
#!/bin/bash | |
num1=$1 | |
num2=$2 | |
[ $# -ne 2 ] && { | |
echo "USAGE:$0 num1 num2" | |
exit 1 | |
} | |
expr $num1 + 1 &>/dev/null | |
RETVAL_n1=$? | |
expr $num2 + 1 &>/dev/null | |
RETVAL_n2=$? | |
[ $RETVAL_n1 -eq 0 -a $RETVAL_n2 -eq 0 ] || { | |
echo "pls input two num again" | |
exit 2 | |
} | |
[ $num1 -lt $num2 ] && { | |
echo "$num1 < $num2" | |
exit 0 | |
} | |
[ $num1 -eq $num2 ] && { | |
echo "$num1 = $num2" | |
exit 0 | |
} | |
[ $num1 -gt $num2 ] && { | |
echo "$num1 > $num2" | |
} | |
root@cs:/server/scripts# sh int1.sh 6 3 | |
6 > 3 | |
root@cs:/server/scripts# sh int1.sh 3 6 | |
3 < 6 | |
root@cs:/server/scripts# sh int1.sh 3 3 | |
3 = 3 | |
root@cs:/server/scripts# sh int1.sh dd ff | |
pls input two num again |