shell条件测试
一.命令执行结果判定
ls /mnt/file &> /dev/null && {
echo yes
echo /mnt/file is exist
} || {
echo no
echo /mnt/file is not exist
}
[root@haha shells]# sh test.sh
no
/mnt/file is not exist
二.条件判断方法
[root@haha shells]# a=1
[root@haha shells]# b=1
[root@haha shells]# test $a = $b && echo yes || echo no
yes
[root@haha shells]# a=1
[root@haha shells]# b=2
[root@haha shells]# [ "$a" = "$b" ] && echo yes || echo no
no
[root@haha shells]# a=2
[root@haha shells]# [[ $a =~ 2|10 ]] && echo yes || echo no
yes
[root@haha shells]# a=3
[root@haha shells]# (( $a>0,$a<10 )) && echo yes
yes
三.判断表达式
1.文件判断表达式
[root@haha shells]# [ -a "file" ] && echo yes || echo no
no
[root@haha shells]# touch file
[root@haha shells]# [ -a "file" ] && echo yes || echo no
yes
[root@haha shells]# [ -d /mnt ] && echo /mnt is directory
/mnt is directory
[root@haha shells]#
[root@haha shells]# [ -s file ] || echo file is not exist or file is empty file
file is not exist or file is empty file
[root@haha shells]# ll file
-rw-r--r--. 1 root root 0 Dec 8 15:24 file
[root@haha shells]# [ -x file ] || echo file is not execute
file is not execute
[root@haha shells]# touch file1
[root@haha shells]# [ file -ot file1 ] && echo yes
yes
2.字符串测试表达式
[root@haha shells]# b=hello
[root@haha shells]# [ -z "$b" ] && echo yes
[root@haha shells]# [ -n "$b" ] && echo yes
yes
[root@haha shells]#
[root@haha shells]# a=world
[root@haha shells]# [ "$a" = "b" ] && echo yes || echo no
no
四.练习脚本
要求:
[root@haha ~]# sh checkfile.sh
please input filename: 当未输入任何字符时回车
Error: no checkfilename,Please input filename for check !! 报错
please input filename: /mnt/leefile 当输入/mnt/leeifle时
文件不存在时
/mnt/file is not exist
当文件存在时输入文件类型
/mnt/file is commom file 注意文件是什么类型就输出文件类型的相关输出
结果如下:
[root@haha shells]# sh check_file.sh
please input filename:
Error: no checkfilename,Please input filename for check !!
[root@haha shells]# sh check_file.sh
please input filename:file
file is commom file
[root@haha shells]# sh check_file.sh
please input filename:/mnt/file
/mnt/file is not exist
[root@haha shells]#
五.整数测试表达式
[root@haha shells]# a=1
[root@haha shells]# b=2
[root@haha shells]# [ $a -eq $b ] && echo yes || echo no
no
[root@haha shells]# [ $a -ne $b ] && echo yes || echo no
yes
[root@haha shells]# [ $a -ge $b ] && echo yes || echo no
no
[root@haha shells]# [ $a -gt $b ] && echo yes || echo no
no
[root@haha shells]# [ $a -le $b ] && echo yes || echo no
yes
[root@haha shells]# [ $a -lt $b ] && echo yes || echo no
yes
[root@haha shells]#
六.逻辑操作符
[root@haha ~]# ls -l leefile easylee
-rw-r--r-- 1 root root 0 Apr 7 23:59 easylee
-rw-r--r-- 1 root root 0 Apr 7 23:59 leefile
test 示例
[root@haha ~]# test -f leefile -a -f easylee && echo yes || echo no
yes
[root@haha ~]# test -f leefile1 -a -f easylee && echo yes || echo no
no
[root@haha ~]# test -f leefile1 -o -f easylee && echo yes || echo no
yes
[root@haha ~]# test ! -f leefile1 && echo yes || echo no
yes
[root@haha ~]# test -f leefile1 && echo yes || echo no
no
[]示例
[root@haha ~]# [ -f "leefile" -a -f "easylee" ] && echo yes || echo no
yes
[root@haha ~]# [ -f "leefile1" -a -f "easylee" ] && echo yes || echo no
no
[root@haha ~]# [ -f "leefile1" -o -f "easylee" ] && echo yes || echo no
yes
[root@haha ~]# [ ! -f "leefile1" ] && echo yes || echo no
yes
[[]]示例
[root@haha ~]# [[ -f "leefile" && -f "easylee" ]] && echo yes || echo no
yes
[root@haha ~]# [[ -f "leefile1" && -f "easylee" ]] && echo yes || echo no
no
[root@haha ~]# [[ -f "leefile1" || -f "easylee" ]] && echo yes || echo no
yes
[root@haha ~]# [[ ! -f "leefile1" ]] && echo yes || echo no
yes
(())演示
[root@haha ~]# A=3
[root@haha ~]# (( $A >=0 && $A<5 )) && echo yes || echo no
yes
七.判定综合训练
[root@haha ~]# guest_number.sh
please input a number between 0 ~ 9: 此处没有输出字符报错并从新开启输入提示符
Error:Please input a number between 0 - 9
please input a number between 0 ~ 9:
[root@haha ~]# guest_number.sh
please input a number between 0 ~ 9: 11 此处没有输出字符报错并从新开启输入提示符
Error:11 is not between 0 ~ 9,Please input a number between 0 - 9
please input a number between 0 ~ 9:
[root@haha ~]# guest_number.sh
please input a number between 0 ~ 9: 5 脚本开始判定是不是幸运数字,假设幸运数字为2
5 is too big
please input a number between 0 ~ 9: 1
1 is too small
please input a number between 0 ~ 9: exit
bye
please input a number between 0 ~ 9: 2
HAHAHA!!! YES You are right !!
Please again?(y/n): y重复执行上述动作,n退出
原文地址:https://blog.csdn.net/2201_76045651/article/details/144326254
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!