shell脚本、if判断、case判断

20.5 shell脚本中的逻辑判断

  • if 判断
#if 表示中文的 如果
;格式1:if条件; then 语句; fi
[root@qingyun-01 ~]# a=5;if [ $a -gt 3 ];then echo ok; fi
ok

;参考文本格式如下:
#!/bin/bash
a=5
if [ $a -gt 3 ] #-gt 表示 >= 
then
    echo ok
fi

;格式2:if条件; then 语句; else 语句; fi
[root@qingyun-01 shell]# a=5;if [ $a -gt 6 ];then echo ok;else echo no ok;fi
no ok

;参考文本格式如下:
#!/bin/bash
a=5
if [ $a -gt 6 ]
then
    echo ok
else
    echo no ok
fi

;格式3:if...; then ...; elif ...; then ...; else ...; fi
[root@qingyun-01 shell]# a=5;if [ $a -gt 10 ];then echo 0k;elif [ $a -gt 6 ];then echo $a\>6;else echo $a\<6;fi
5<6

;参考文本格式如下:
[root@qingyun-01 shell]# sh -x if3.sh
+ a=5
+ '[' 5 -gt 10 ']'
+ '[' 5 -gt 6 ']'
+ echo '5<6'
5<6
[root@qingyun-01 shell]# cat if3.sh 
#!/bin/bash
a=5
if [ $a -gt 10 ]
then
    echo ">10"
elif [ $a -gt 6 ]
then
    echo $a">6"
else
    echo $a"<6"
fi

逻辑判断吧表达式:
if [ $a -gt $b ];
if [ $a -lt 5 ];
if [ $b -eq 10 ] 等
-gt(>); -lt(<); -ge(>=);
-le(<=); -eq(==); -ne(!=)
注意到处都是空格

#另一种写法:
[root@qingyun-01 shell]# a=5;if (($a>10));then echo OK;else echo NO;fi
NO
[root@qingyun-01 shell]# a=5;if (($a>=5));then echo OK;else echo NO;fi
OK
[root@qingyun-01 shell]# a=5;if (($a>10));then echo OK;elif (($a>6));then echo $a">6";else echo $a"<6";fi
5<6
  • 可以使用&& || 结合多个条件

if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [[ $b -lt 3 ]; then

20.6 文件目录属性判断

  • [ -f file ]判断是否是普通文件,且存在
[root@qingyun-01 shell]# cat file.sh 
#!/bin/bash
f="/tmp/taoyuan"
if [ -f $f ]
then
    echo $f exist
else
    touch $f
fi

;没有文件,创建文件
[root@qingyun-01 shell]# sh -x file.sh 
+ f=/tmp/taoyuan
+ '[' -f /tmp/taoyuan ']'
+ touch /tmp/taoyuan

;文件已经存在
[root@qingyun-01 shell]# sh -x file.sh 
+ f=/tmp/taoyuan
+ '[' -f /tmp/taoyuan ']'
+ echo /tmp/taoyuan exist
/tmp/taoyuan exist
  • [ -d file ]判断是否是目录,且存在
[root@qingyun-01 shell]# cat file1.sh 
#!/bin/bash
f="/tmp/taoyuan"
if [ -d $f ]
then
    echo $f exist
else
    mkdir $f
fi

[root@qingyun-01 shell]# sh -x file1.sh 
+ f=/tmp/taoyuan
+ '[' -d /tmp/taoyuan ']'
+ mkdir /tmp/taoyuan
  • [ -e file ]判断文件或者目录是否存在
[root@qingyun-01 shell]# cat file2.sh 
#!/bin/bash
f="/tmp/taoyuan"
if [ -e $f ]
then
    echo $f exist        
else
    mkdir $f
fi
[root@qingyun-01 shell]# sh -x file2.sh
+ f=/tmp/taoyuan
+ '[' -e /tmp/taoyuan ']'
+ echo /tmp/taoyuan exist
/tmp/taoyuan exist
  • [ -r file ]判断文件是否可读
[root@qingyun-01 shell]# f="/tmp/taoyuan";if [ -r $f ];then echo $f read;else echo file reservation;fi
/tmp/taoyuan read
  • [ -w file ]判断文件是否可写
[root@qingyun-01 shell]# f="/tmp/taoyuan";if [ -w $f ];then echo $f write;else echo $f File unable to write;fi
/tmp/taoyuan write
  • [ -x file ]判断文件是否可执行
[root@qingyun-01 shell]# f="/tmp/taoyuan";if [ -x $f ];then echo $f x file;else echo $f File no execution;fi
/tmp/taoyuan x file

20.7 if特殊用法

  • if [ -z "$a" ] 这个表示当量a的值为空时会怎么样
[root@qingyun-01 shell]# cat if.sh 
#!/bin/bash
f="/tmp/lalal"
if [ ! -f $f ]
then
    echo "$f not exist."
    exit
fi
n=`wc -l $f`
if [ -z "$a" ] #变量需要双引号
then
    echo error
    exit
elif [ $n -gt 100 ]
then
    echo OK
fi
[root@qingyun-01 shell]# sh if.sh 
/tmp/lalal not exist.
  • if [ -n "$a" ] 表示当变量a的值不为空
[root@qingyun-01 shell]# if [ -n if.sh ]; then echo ok;fi
ok
#可以判断一个文件是否为空

[root@qingyun-01 shell]# if [ -n "$b" ]; then echo $b;else echo "b is null";fi
b is null
  • if grep -q '123' 1.txt;then 表示如果1.txt中含有'123'的行时会怎么样
[root@qingyun-01 shell]# if grep -w 'user' /etc/passwd; then echo "user exist"; fi
user:x:1000:1000:user:/home/user:/bin/bash
user exist
[root@qingyun-01 shell]# if grep -wq 'user' /etc/passwd; then echo "user exist"; fi
user exist
#grep -q 不显示过滤的结果
  • if [ ! -e file ];then 表示文件不存在时会怎么样
  • [] 中不能使用<,>,==,!=,>=,<=这样的符号

20.8-20.9 case判断

  • shellcase判断脚本案例
[root@qingyun-01 shell]# cat case.sh 
#!/bin/bash
read -p "Please enter the score:" n
if [ -z "$n" ]
then
    echo "For the input score."
    exit 1
fi

n1=`echo $n|sed 's/[0-9]//g'`
if [ ! -z "$n1" ]
then
    echo "Non-numeric input."
    exit 1
fi

if [$n -lt 60 ] && [ $n -ge 0 ]
then
    tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
    tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]
then
    tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
    tag=4
else
tag=0
fi

case $tag in
    1)
       echo "C"
    ;;
    2)
       echo "B"
    ;;
    3)
       echo "A"
    ;;
    4)
       echo "A+"
    ;;
    *)
      echo "Please enter a scale of 0-100."
    ;;
esac

转载于:https://blog.51cto.com/3622288/2069596

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值