shell脚本、if判断、case判断
发布时间:2020-12-15 19:56:47 所属栏目:安全 来源:网络整理
导读:20.5 shell脚本中的逻辑判断 if 判断 #if 表示中文的 如果;格式1:if条件; then 语句; fi[root@qingyun-01 ~]# a=5;if [ $a -gt 3 ];then echo ok; fiok;参考文本格式如下:#!/bin/basha=5if [ $a -gt 3 ] #-gt 表示 = then echo okfi;格式2:if条件; then
20.5 shell脚本中的逻辑判断
#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
#另一种写法: [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
20.6 文件目录属性判断
[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
[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
[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
[root@qingyun-01 shell]# f="/tmp/taoyuan";if [ -r $f ];then echo $f read;else echo file reservation;fi /tmp/taoyuan read
[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
[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特殊用法
[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.
[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
[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 不显示过滤的结果
20.8-20.9 case判断
[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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |