自学内容网 自学内容网

shell 条件测试

一、命令执行结果判定

&& : 在命令执行后如果没有任何报错时会执行符号后面的动作

|| : 在命令执行后有报错执行符号后的动作

[root@long ~]# a=10
[root@long ~]# echo $a
10
[root@long ~]# [ $a -gt "5" ] && echo yes || echo no
yes
[root@long ~]# a=3
[root@long ~]# echo $a
3
[root@long ~]# [ $a -gt "5" ] && echo yes || echo no
no
[root@long ~]#
​

二、 条件判断方法

使用测试语句来测试指定的条件表达式的条件的真或假

[root@long ~]# a=4 b=6
[root@long ~]# echo -e "$a\t$b"
4       6
[root@long ~]# test $a = $b && echo yes || echo no
no
[root@long ~]# [ $a = $b ] && echo yes || echo no
no
[root@long ~]# [[ $a =~ 2|10 ]] && echo yes || echo no
no
[root@long ~]# [[ $a =~ 4|10 ]] && echo yes || echo no
yes
[root@long ~]# (($a > 0 | $b < 10)) && echo yes || echo no
yes

三、判断表达式

1、文件判断表达式

# 判断文件是否存在
[root@long ~]# [ -a create_user.sh ] && echo yes || echo no
yes
# 判断文件类型
[root@long ~]# [ -a /mnt  ] && echo directory || echo no
directory
# 判断文件是否存在并不为空
[root@long ~]# [ -s file ] || echo file is not exist or file is empty file
file is not exist or file is empty file
# 判断文件权限
[root@long ~]# [ -x file ] || echo file is not execute
file is not execute
# 判断文件的新旧
[root@long ~]# cd /mnt
[root@long mnt]# ls
file  hgfs  longfile
[root@long mnt]# [ file -ot longfile ] && echo yes
yes
[root@long mnt]#

2、字符串测试表达式

# 检测变量是否为空
[root@long ~]# word=qwer
[root@long ~]# [ -z "$word" ] && echo yes || echo no
no
[root@long ~]# [ -n "$word" ] && echo yes || echo no
yes
# 比较字符
[root@long ~]# word1=poi
[root@long ~]# [ "$word1" =  "$word" ] && echo yes || echo no
no
[root@long ~]# [ "$word1" !=  "$word" ] && echo yes || echo no
yes
[root@long ~]#

四、脚本练习

要求:当输出空格时报错,文件存在时输出,反之。

[root@long ~]# vim w.sh
#!/bin/bash
read -p "please input filename: " f
if [ -n "$f" ]; then
    if [ -a "$f" ]; then
        echo "File exists"
​
    else
        echo "File does not exist."
    fi
else
    echo "Filename is empty."
fi
​
[root@long ~]# sh w.sh
please input filename:
Filename is empty.
[root@long ~]# sh w.sh
please input filename: /mnt/file
File exists
[root@long ~]# sh w.sh
please input filename: /mnt/lee
File does not exist.

五、整数测试表达式

[root@long ~]# a=6 b=6
[root@long ~]# echo -e "$a\t$b"
6       6
[root@long ~]# [ "$a" -eq "$b" ] && echo yes || echo no
yes                                                       # 相等或不等
[root@long ~]# [ "$a" -ne "$b" ] && echo yes || echo no
no
[root@long ~]# c=8
[root@long ~]# [ "$a" -ne "$c" ] && echo yes || echo no
yes
[root@long ~]# [ "$a" -gt "$c" ] && echo yes || echo no 
no                                                          # 大小比较
[root@long ~]# [ "$a" -ge "$c" ] && echo yes || echo no
no
[root@long ~]# [ "$a" -lt "$c" ] && echo yes || echo no
yes
[root@long ~]# [ "$a" -le "$c" ] && echo yes || echo no
yes

六、逻辑操作符

# a b c 上述变量
[root@long ~]# [ $a -eq $b -a $a -lt $c ]  && echo yes || echo no
yes
[root@long ~]# [ $a -eq $b -a $a -gt $c ]  && echo yes || echo no
no
[root@long ~]# [ $a -eq $b -0 $a -gt $c ]  && echo yes || echo no
-bash: [: syntax error: `-0' unexpected
no
[root@long ~]# [ $a -eq $b -o $a -gt $c ]  && echo yes || echo no
yes
[root@long ~]#  [ ! $a -eq $b ]  && echo yes || echo no
no

七、编程练习

1、用判断的方式写一个一分十秒的倒计时

# a b c 上述变量
[root@long ~]# [ $a -eq $b -a $a -lt $c ]  && echo yes || echo no
yes
[root@long ~]# [ $a -eq $b -a $a -gt $c ]  && echo yes || echo no
no
[root@long ~]# [ $a -eq $b -0 $a -gt $c ]  && echo yes || echo no
-bash: [: syntax error: `-0' unexpected
no
[root@long ~]# [ $a -eq $b -o $a -gt $c ]  && echo yes || echo no
yes
[root@long ~]#  [ ! $a -eq $b ]  && echo yes || echo no
no

2、猜数字游戏

要求: 输入空格和超过范围的值,对应不同的报错;生成一个随机数,判断输入的值与随机数的大小,进而猜测循环,输入exit退出。

[root@long ~]#vim guest.sh
​
#!/bin/bash
a=$((RANDOM%10))
guest(){
  read -p "please input a number between 0~9:" num
  [ -z "$num" ] && { echo "Error: !!!!"
  guest
​
  }
   [ "$num" = "exit" ] && {
   exit
  }
  [ "$num" -ge "0" -a "$num" -le "9" ] || {  echo "Error:0~9!!"
   guest
  }
  [ "$num" -lt "$a" ] && {
​
     echo "too small"
     guest
  }
  [ "$num" -gt "$a" ] &&  {
​
      echo "too big "
      guest
  }
  echo yes
  read -p "please again ?  (y/n):"  want
  [ "$want"="y" ] && {
       a=$((RANDOM%10))
       guest
​
  } || {
       echo bye
       exit
​
  }
}
guest
[root@long ~]# sh guest.sh
please input a number between 0~9:
Error: !!!!
please input a number between 0~9:18
Error:0~9!!
please input a number between 0~9:3
too big
please input a number between 0~9:2
too big
please input a number between 0~9:1
too big
please input a number between 0~9:0
yes
please again ?  (y/n):y
please input a number between 0~9:4
too small
please input a number between 0~9:6
too small
please input a number between 0~9:7
too small
please input a number between 0~9:8
too small
please input a number between 0~9:9
yes
Error: !!!!
please input a number between 0~9:exit
[root@long ~]#


原文地址:https://blog.csdn.net/m0_64469733/article/details/144325847

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!