自学内容网 自学内容网

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

.条件判断方法

shell 程序中,用户可以使用测试语句来测试指定的条件表达式的条件的真或假
[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.文件判断表达式

a )判断文件时否存在
[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
b )判断文件类型
[root@haha shells]# [ -d /mnt ] && echo /mnt is directory
/mnt is directory
[root@haha shells]# 
c )判定文件是否存在并不为空
[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
d )判定文件权限
[root@haha shells]# [ -x file ] || echo file is not execute
file is not execute
e )判定文件新旧
[root@haha shells]# touch file1
[root@haha shells]# [ file -ot file1 ] && echo yes
yes

2.字符串测试表达式

1. 检测变量是否未空
[root@haha shells]# b=hello
[root@haha shells]# [ -z "$b" ] && echo yes
[root@haha shells]# [ -n "$b" ] && echo yes
yes
[root@haha shells]# 
2. 比较字符
[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

.判定综合训练

1. 请用判定的方式书写一个 1 10 秒的倒计时,确保倒计时器符合日常显示规则
2. 请书写一个猜数游戏,要求如下
[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)!