一、shell脚本中的逻辑判断

1、格式1:if条件,then语句,fi    (if 和fi 相对应 )

#!/bin/bash
a=5
if [ $a -gt 3 ]
then
echo "ok"
fi
2、格式2:if条件,then语句;else语句; fi

#!/bin/bash
a=2
if [ $a -gt 3 ]
then
echo "ok"
else
echo "fail"
fi

3、格式3:if...;then....;elif....;else....;fi

#!/bin/bash
a=4
if [ $a -gt 6 ]
then 
echo "ok"
elif [ $a -eq 5 ]
then
echo "good"
else 
echo "fail"
fi 

4、[ $a -gt $b ]  等同于  (($a>$b))   ge (大于等于) le (小于等于) ne(不等于)

5、 && 和 || 结合使用

 if [ $a -gt 5 ] && [ $a -lt 10 ] ;then ...;fi

 if [ $a -gt 5 ] || [ $a -lt 2 ];then ....;fi 

二、文件目录属性判断

1、[ -f file ],判断是否为普通文件且存在

#!/bin/bash
f=/tmp/aminglinux                   
if [ -f $f ]
then
echo "$f exist"
else
touch $f
fi

[root@localhost ~]# sh -x 2.sh
+ f=/tmp/aminglinux
+ '[' -f /tmp/aminglinux ']'
+ echo '/tmp/aminglinux exist'
/tmp/aminglinux exist
[root@localhost ~]# sh -x 2.sh
+ f=/tmp/aminglinux
+ '[' -f /tmp/aminglinux ']'
+ echo '/tmp/aminglinux exist'
/tmp/aminglinux exist

2、[ -d file ],判断是否为目录且存在

#!/bin/bash
f=/tmp/aming
if [ -d $f ]
then
echo "$f exist"
else
mkdir $f
fi

[root@localhost ~]# sh -x 3.sh
+ f=/tmp/aming
+ '[' -d /tmp/aming ']'
+ echo '/tmp/aming exist'
/tmp/aming exist

3、[ -e file ],判断文件或目录是否存在

#!/bin/bash
f=/tmp/aming
if [ -e $f ]
then
echo "$f exist"
else
touch $f
fi

[root@localhost ~]# sh -x 3.sh
+ f=/tmp/aming
+ '[' -e /tmp/aming ']'
+ echo '/tmp/aming exist'
/tmp/aming exist

4、[ -r file ],判断文件是否可读

#!/bin/bash
f=/tmp/aminglinux
if [ -r $f ]
then
echo "$f readable"
fi

[root@localhost ~]# sh -x 3.sh
+ f=/tmp/aminglinux
+ '[' -r /tmp/aminglinux ']'
+ echo '/tmp/aminglinux readable'
/tmp/aminglinux readable
[root@localhost ~]# ls -l /tmp/aminglinux
-rw-r--r--. 1 root root 0 4月 18 21:40 /tmp/aminglinux

5、[ -w file ],判断文件是否可写

#!/bin/bash
f=/tmp/aminglinux
if [ -w $f ]
then
echo "$f writeable"
fi

[root@localhost ~]# sh -x 3.sh
+ f=/tmp/aminglinux
+ '[' -w /tmp/aminglinux ']'
+ echo '/tmp/aminglinux writeable'
/tmp/aminglinux writeable

1、[ -x file ],判断文件是否可执行 

#!/bin/bash
f=/tmp/aminglinux
if [ -x $f ]
then
echo "$f writeable"
else
echo "$f fail"
fi

[root@localhost ~]# sh -x 3.sh
+ f=/tmp/aminglinux
+ '[' -x /tmp/aminglinux ']'
+ echo '/tmp/aminglinux fail'
/tmp/aminglinux fail

6、

[ -f  $f ] && rm -f $f 等同于     (&& 前面的命令执行成功,后面的命令才会执行)

if [ -f $f ]
then
rm -f $f
fi

7、

[ -f $f ] || touch $f 等同于     (|| 前面的命令执行不成功,后面的才会执行)

if [ !-f $f ]
then
touch $f
fi

三、if 的特殊用法

1、if [ -z "$a" ] 这个表示当变量a为空时会怎样

#!/bin/bash
n=`wc -l /tmp/lsss`
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo $n
fi

[root@localhost ~]# sh -x 4.sh
++ wc -l /tmp/lsss
wc: /tmp/lsss: 没有那个文件或目录
+ n=
+ '[' -z '' ']'
+ echo error
error
+ exit

1、if [ -e "$a" ] 这个表示当变量a不为空时会怎样

#!/bin/bash
n=`wc -l /tmp/lsss`
if [ -n "$n" ]
then
echo $n
else
echo " $n is null "
fi

[root@localhost ~]# sh -x 4.sh
++ wc -l /tmp/lsss
wc: /tmp/lsss: 没有那个文件或目录
+ n=
+ '[' -n '' ']'
+ echo ' is null '
is null

3、if grep  -wq  "user1" /etc/passwd;then....;fi  #表示如果/etc/passwd 存在halt用户,然后会怎样。

[root@localhost ~]# grep -w "halt" /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
[root@localhost ~]# if grep -wq "halt" /etc/passwd; then echo "halt exist";fi
halt exist

4、if [ ! -f file ]; 表示 不存在文件会怎样

5、[] 中不能使用< 、>、==、!=、>=、<= 这样的符号

四、case判断

1、格式: case 变量名 in

                        value1)

                       command

                       ;;

                       value2)

                       command

                       ;;

                       value*)

                       command

                       ;;

                    esac

在 case 程序中可以使用|,表示或

编辑case判断脚本

#!/bin/bash
read -p "please input a number:" n     #输入一个数字
if [ -z "$n" ]                                          #判断n 是否为空,如果为空,请重新输入
then
read -p "please again input a number:" n
fi
n1=`echo $n|sed 's/[0-9]//g'`              # 判断输入的是否为一个纯数字,如果不是,重新输入
if [ ! -z $n1 ]
then
read -p "please input again input a number:" n
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 "不及格"
;;
2)
echo "及格"
;;
3)
echo "良"
;;
4)
echo "优"
;;
*)
echo "the number is 0-100."
;;
esac

[root@localhost ~]# sh -x 5.sh
+ read -p 'please input a number:' n
please input a number:90
+ '[' -z 90 ']'
++ echo 90
++ sed 's/[0-9]//g'
+ n1=
+ '[' '!' -z ']'
+ '[' 90 -lt 60 ']'
+ '[' 90 -ge 60 ']'
+ '[' 90 -lt 80 ']'
+ '[' 90 -ge 80 ']'
+ '[' 90 -lt 90 ']'
+ '[' 90 -ge 90 ']'
+ '[' 90 -le 100 ']'
+ tag=4
+ case $tag in
+ echo $'\344\274\230'

 

posted on 2018-04-18 23:15  天梭  阅读(133)  评论(0编辑  收藏  举报