码迷,mamicode.com
首页 > 系统相关 > 详细

20.5 shell脚本中的逻辑判断 20.6 文件目录属性判断 20.7 if特殊用法 20.8/20.9 case判断

时间:2017-11-22 22:06:17      阅读:350      评论:0      收藏:0      [点我收藏+]

标签:脚本   shell   文件目录   逻辑判断   

- 20.5 shell脚本中的逻辑判断
- 20.6 文件目录属性判断
- 20.7 if特殊用法
- 20.8/20.9 case判断
# 20.5 Shell脚本中的逻辑判断
- 很多脚本可以直接用命令执行,比如之前的那个
```
[root@aming-01 ~]# for i in `seq 1 5`;do echo $i;done
1
2
3
4
5
[root@aming-01 ~]# for i in `seq 1 5`
> do
> echo $i
> done
1
2
3
4
5
[root@aming-01 ~]# 
```
- 格式1:if 条件 ; then 语句; fi
- 下面来写个脚本
- a=5 ,如果a 大于3 就 打印 ok
- 如果写成一行就 下面这样
```
[root@aming-01 ~]# a=5
[root@aming-01 ~]# if [ $a -gt 3 ]
> then
> echo ok
> fi
ok
[root@aming-01 ~]# 

[root@aming-01 ~]# if [ $a -gt 3 ];then echo ok; fi
ok
[root@aming-01 ~]# 

```
- 用脚本的形式编辑下,就是
```
[root@aming-01 shell]# vi if1.sh

#!/bin/bash
a=5
if [ $a -gt 3 ]
then 
    echo ok 
fi
~                                                                                         
~                                                                                         
~                                                                                         
                                                                                       
~                                                                                         
:wq

[root@aming-01 shell]# sh if1.sh
ok
[root@aming-01 shell]# 

```
- 格式2:if 条件; then 语句; else 语句; fi
- 如果满足条件怎么样,不满足又会怎么样
```
[root@aming-01 shell]# cp if1.sh if2.sh
[root@aming-01 shell]# vi if2.sh

#!/bin/bash
a=1
if [ $a -gt 3 ]
then
    echo ok
else
    echo not ok
fi
                                                                                        
~                                                                                         
~                                                                                         
~                                                                                         
:wq

[root@aming-01 shell]# sh -x if2.sh
+ a=1
+ ‘[‘ 1 -gt 3 ‘]‘
+ echo not ok
not ok
[root@aming-01 shell]# 

```
- 格式3:if …; then … ;elif …; then …; else …; fi
- -gt 指的是大于  -lt指的是小于
```
[root@aming-01 shell]# cp if2.sh if3.sh
[root@aming-01 shell]# vi if3.sh

#!/bin/bash
a=5
if [ $a -gt 1 ]
then
    echo ">1"
elif [$a -lt 6 ]
then 
    echo "<6 && >1" 
else
    echo not ok
fi
~                                                                                         
                                                                                       
~                                                                                         
~                                                                                         
:wq
[root@aming-01 shell]# vi if3.sh

[root@aming-01 shell]# cat if3.sh
#!/bin/bash
a=5
if [ $a -gt 1 ]
then
    echo ">1"
elif [$a -lt 6 ]
then 
    echo "<6 && >1" 
else
    echo not ok
fi
[root@aming-01 shell]# 

[root@aming-01 shell]# sh -x if3.sh
+ a=5
+ ‘[‘ 5 -gt 1 ‘]‘
+ echo ‘>1‘
>1
[root@aming-01 shell]# 
```
- 改下
```
[root@aming-01 shell]# vi if3.sh

#!/bin/bash
a=5
if [ $a -gt 4 ]
then
    echo ">1"
elif [ $a -lt 6 ]
then
    echo "<6 && >1"
else
    echo not ok
fi
~                                                                                         
                                                                                      
~                                                                                         
:wq
[root@aming-01 shell]# sh -x if3.sh
+ a=5
+ ‘[‘ 5 -gt 4 ‘]‘
+ echo ‘>1‘
>1
[root@aming-01 shell]# 
```
- if 判断的三种格式
```
[root@aming-01 shell]# if ...;then ...; fi^C
[root@aming-01 shell]# if ...;then ...;else ...;fi^C
[root@aming-01 shell]# if ...;thn ...;elif ...then ...;else ...;fi^C
[root@aming-01 shell]# 
```
- 逻辑判断表达式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格

-    gt 大于        lt 小于         eq 等于
-   -ge 大于等于   -le小于等于     -ne 不等于
```
[root@aming-01 shell]# gt lt eq   -ne -ge -le ^C
```
- 可以使用 && || 结合多个条件         &&并且   ||或者
-  if [ $a -gt 5 ] && [ $a -lt 10 ]; then
-  if [ $b -gt 5 ] || [ $b -lt 3 ]; then










# 20.6 文件目录属性判断
- shell中 我们通常和文件、目录打交道,所以对文件目录的判断很重要
- if 判断文件、目录属性
- [ -f file ]判断是否是普通文件,且存在
- 举例
```
Last login: Tue Nov 21 19:49:55 2017
[root@aming-01 ~]# vi file1.sh

#!/bin/bash
f="/tmp/aminglinux"
if [ -f $f ]
then
    echo $f exist
else 
    touch $f
fi
~                                                                                         
~                                                                                         
                                                                                        
~                                                                                         
:wq
[root@aming-01 ~]# vi file1.sh
[root@aming-01 ~]# sh -x file1.sh
+ f=/tmp/aminglinux
+ ‘[‘ -f /tmp/aminglinux ‘]‘
+ touch /tmp/aminglinux
[root@aming-01 ~]# 

```
- 解释:首先f回去判断这个文件到底存在与否,很明显不存在,于是就创建了一个/tmp/aminglinux 文件
- 那我们再次执行这个脚本,很明显这个文件已经存在了,所以echo  /tmp/aminglinux  exist
```
[root@aming-01 ~]# sh -x file1.sh
+ f=/tmp/aminglinux
+ ‘[‘ -f /tmp/aminglinux ‘]‘
+ echo /tmp/aminglinux exist
/tmp/aminglinux exist
[root@aming-01 ~]# 
```
- 这里把这个脚本移到shell/目录下面来,以为脚本文件我们统一放在/shell 目录里
```
[root@aming-01 ~]# ls
aming.txt  anaconda-ks.cfg  file1.sh  shell  zabbix-release-3.2-1.el7.noarch.rpm
[root@aming-01 ~]# cd shell/
[root@aming-01 shell]# mv /root/file1.sh .
[root@aming-01 shell]# ls
01.sh  file1.sh  if1.sh  if2.sh  if3.sh
```
-  [ -d file ] 判断是否是目录,且存在
```
[root@aming-01 shell]# cp file1.sh file2.sh
[root@aming-01 shell]# vi file2.sh

#!/bin/bash
f="/tmp/aminglinux"
if [ -d $f ]
then
    echo $f exist
else
    touch $f
fi
~                                                                                         
~                                                                                         
                                                                                       
~                                                                                         
:wq
[root@aming-01 shell]# vi file2.sh
[root@aming-01 shell]# sh -x file2.sh
+ f=/tmp/aminglinux
+ ‘[‘ -d /tmp/aminglinux ‘]‘
+ touch /tmp/aminglinux
[root@aming-01 shell]#
```
- 很明显他不适目录,不适目录就touch 一个目录


- 还有一种情况,我不管它是文件还是目录,我仅仅是想知道这个文件或者目录到底存在不存在
- [ -e file ] 判断文件或目录,是否存在
```
[root@aming-01 shell]# vi file2.sh

#!/bin/bash
f="/tmp/aminglinux"
if [ -e $f ]
then
    echo $f exist
else
    touch $f
fi
~                                                                                         
~                                                                                         
                                                                                       
:wq
[root@aming-01 shell]# vi file2.sh
[root@aming-01 shell]# sh -x file2.sh
+ f=/tmp/aminglinux
+ ‘[‘ -e /tmp/aminglinux ‘]‘
+ echo /tmp/aminglinux exist
/tmp/aminglinux exist
[root@aming-01 shell]# 
```
- 判断这个目录或者文件是否存在,不存在就创建一个目录或者文件
- 如果这个文件存在touch会是摸一下 文件或者目录,改变文件和目录的 三个time值 Atime  Ctime  Mtime

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

```
[root@aming-01 shell]# vi file2.sh

#!/bin/bash
f="/tmp/aminglinux"
if [ -r $f ]
then
    echo $f readable
fi
~                                                                                         
                                                                                         
~                                                                                         
:wq
[root@aming-01 shell]# vi file2.sh

[root@aming-01 shell]# sh file2.sh
/tmp/aminglinux readable
[root@aming-01 shell]# 
```
- 显示这个文件是可读的
 
- [ -w file ] 判断文件是否可写
```
[root@aming-01 shell]# vi file2.sh

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

[root@aming-01 shell]# vi file2.sh
[root@aming-01 shell]# !sh
sh file2.sh
/tmp/aminglinux writeable
[root@aming-01 shell]# 

```
- 文件可写
- 可读可写可执行是针对当前用户root 来说的
```
[root@aming-01 shell]# ls -l /tmp/aminglinux
-rw-r--r-- 1 root root 0 11月 21 20:09 /tmp/aminglinux
[root@aming-01 shell]# 
```
-  [ -x file ] 判断文件是否可执行
```
[root@aming-01 shell]# vi file2.sh

#!/bin/bash
f="/tmp/aminglinux"
if [ -x $f ]
then
    echo $f exeable
fi
~                                                                                         
                                                                                        
~                                                                                         
:wq
[root@aming-01 shell]# vi file2.sh
[root@aming-01 shell]# sh file2.sh
[root@aming-01 shell]# 
```
- 因为它不可执行,所以没有任何的输出
- 因为我们根本就没有定义else
```
[root@aming-01 shell]# cat file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -x $f ]
then
    echo $f exeable
fi
[root@aming-01 shell]# 
```
- 这样用的比较多
- 常用案例
并且
```
f="/tmp/aminglinux"
[ -f $f ] && rm -f $f     //前一条命令执行成功才会继续执行之后的命令

等同于下面的表达方式
if [ -f $f ]     
then
      rm -rf $f
fi
```

- 或者
```
f="/tmp/aminglinux"
[ -f $f ] || touch $f    //前面命令不成功时,执行后面的命令
if [ ! -f $f ]        //  “!”表示了如果这条命令不成功,就往下执行 (!表示取反)
then
      touch $f
fi
```








# 20.7 if特殊用法
- if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
```
[root@aming-01 shell]# vi if4.sh

#!/bin/bash
n=`wc -l /tmp/lalal`
if [ $n -gt 100 ]
then
    echo alsdflljk
fi
                                                                                       
~                                                                                         
:wq
[root@aming-01 shell]# vi if4.sh
[root@aming-01 shell]# sh -x if4.sh
++ wc -l /tmp/lalal
wc: /tmp/lalal: 没有那个文件或目录
+ n=
+ ‘[‘ -gt 100 ‘]‘
if4.sh: 第 3 行:[: -gt: 期待一元表达式
[root@aming-01 shell]#
```
- 当变量没有值的时候 会报错,为了让这个脚本更加严谨,应该做一些判断
- if [ -z "$a" ] 这里表示当变量a的值为空时 就echo error 并且 exit
```
[root@aming-01 shell]# vi if4.sh

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


[root@aming-01 shell]# vi if4.sh
[root@aming-01 shell]# sh -x if4.sh
++ wc -l /tmp/lalal
wc: /tmp/lalal: 没有那个文件或目录
+ n=
+ ‘[‘ -z ‘‘ ‘]‘
+ echo error
error
+ exit
[root@aming-01 shell]# 

```
- 再改进一下,这样它就不再报错了
```
[root@aming-01 shell]# !vi
vi if4.sh

#!/bin/bash
if [ ! -f /tmp/lalal ]
then
   echo "/tmp/lalal not exist."
   exit
fi
n=`wc -l /tmp/lalal`
if [ -z "$n" ]
then
    echo error
    exit
elif  [ $n -gt 100 ]
then
    echo alsdflljk
fi
~                                                                                         
                                                                                        
~                                                                                         
:wq

[root@aming-01 shell]# sh if4.sh
/tmp/lalal not exist.
[root@aming-01 shell]# 

```
-  if [ -n "$a" ] 表示当变量a的值不为空
-  用变量的时候 要用双引号引起来,如果是一个文件,那就不用了
-  - 这个 [ -n "$a" ]  是可以判断文件的,判断一个文件的内容是否不为空,条件成立
```
[root@aming-01 shell]# ls
01.sh  file1.sh  file2.sh  if1.sh  if2.sh  if3.sh  if4.sh
[root@aming-01 shell]# if [ -n 01.sh ]; then echo ok;fi
ok
[root@aming-01 shell]# 
```
- 同样可以判断变量
- 解释:当变量b 不为空,那么echo $b, 否则 当变量b为空,那么echo b is null,这里变量b 是为空的
- -n 和 -z 用法,如果不是变量,不需要双引号“”;因为这两个是针对文件执行的;
```
[root@aming-01 shell]# echo $b

[root@aming-01 shell]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi
b is null
[root@aming-01 shell]# 
```
- 比如判断系统的用户里面是否有user1这个用户
```
[root@aming-01 ~]# useradd user1 
[root@aming-01 ~]# cd shell
[root@aming-01 shell]# grep -w ‘user1‘ /etc/passwd
user1:x:1011:1011::/home/user1:/bin/bash
[root@aming-01 shell]# 
```
- 表示如果/etc/passwd中含有‘user1‘的字符 时会怎么样
-  -w 更加精准的匹配,只匹配‘’内的单词
-  如果/etc/passwd 里包含user1 ,那么就说 user1 exist  user1 存在
```
[root@aming-01 shell]# if grep -w ‘user1‘ /etc/passwd;then echo "user1 exist";fi
user1:x:1011:1011::/home/user1:/bin/bash
user1 exist
[root@aming-01 shell]# 
```
- 只不过这个判断的过程 是会输出 过滤的语句出来,把这个结果给输出出来,grep -q 仅仅是做一个过滤,但是不会吧过滤的内容显示出来
```
[root@aming-01 shell]# if grep -wq ‘user1‘ /etc/passwd;then echo "user1 exist";fi
user1 exist
[root@aming-01 shell]# 
```
- 反过来怎么表示呢,如果这个user1不存在
- 如果user1 不存在,加个! 表示取反的意思,那么useradd user1
- 只不过这个操作过程不成立的,因为user1本身就存在了 后面这个命令不生效的then "useradd user1"
```
[root@aming-01 shell]# if ! grep -wq ‘user1‘ /etc/passwd;then "useradd user1";fi
[root@aming-01 shell]# 
```
-  if [ ! -e file ]; then 表示文件不存在时会怎么样
- if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…

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







# 20.8 case判断(上)
```
- 格式 case  变量名 in 
                     value1)
                          command
                          ;;
                     value2)
                          command
                          ;;
                      *)
                        commond
                            ;;
                      esac
``` 
- 在case程序中,可以在条件中使用|,表示或的意思, 比如 
```
2|3) 
    command
    ;;
```
- shell脚本案例:
```
[root@aming-01 shell]# vi case.sh


                                                                                       
~                                                                                         
"case.sh" [New File]
```
- 加入以下脚本
```
[root@aming-01 shell]# vi case.sh
#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]

then
    echo "Please input a number."
    exit 1
fi

n1=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$n1" ]
then
 echo "Please input a number."
 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 "not ok"
        ;;
    2)
        echo "ok"
        ;;
    3)
        echo "ook"
        ;;
    4)
        echo "oook"
        ;;
    *)
        echo "The number range is 0-100."
        ;;
esac

```
- 脚本命令解释
- read -p "Please input a number: " n
if [ -z "$n" ]   
- read -p 的作用,n作为你要捕获一个变量的名字,用户输入什么数值,最终这个n 这个变量就会赋值什么东西
```
[root@aming-01 shell]# read -p "Please input a number: " n
Please input a number: 
Please input a number; kdkdk
[root@aming-01 shell]# echo $n
kdkdk
[root@aming-01 shell]# 
[root@aming-01 shell]# echo $n
123
[root@aming-01 shell]# 
```
- if [ -z "$n" ] 表示当用户没有输入东西的时候  为空的时候  if [ -z "$n" ] 表示为空的时候,说明用户没有输入,那奥告诉他 echo "Please input a number." 请输入一个数字
- 这里的 exit 1  ,这个1 表示 当一个命令一个语句运行完之后 都会有一个返回的值,echo $? 输出结果为0 表示命令 语句是正确的,如果是1 那就是错误 这里的exit 1 就是那个echo $?的输出的值

-  n1=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$n1" ] 
- 这里的变量判断的时你输入的字符串 是不是纯数字,万一输入的时1a1 或者 纯字母aaa呢,都需要去判断下,如果不是那就要做一个操作,把里面的数字做一个清空
- 当你的变量不为空时,if [ -n "$n1" ] 表示$n1不为空时,继续提示让你输入一个纯数字
```
n1=`echo $n|sed ‘s/[0-9]//g‘`
if [ -n "$n1" ]
then
 echo "Please input a number."
 exit 1
```









# 20.9 case判断(下)
```
fi

if [ $n -lt 60 ] && [ $n -ge 0 ]
then
    tag=1
```
- 当你输入的时数字的时候,数字满足[ $n -ge 0 ] 大于等于0  并且 [ $n -lt 60 ]小于60,那么作为标记1  tag1 是个变量

- 如果不是上述情况
```
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
    tag=2
```
- [ $n -ge 60 ]是大于等于60 并且 [ $n -lt 80 ]小于80 那么作为标签2 
- 同理
```
elif [ $n -ge 80 ]  && [ $n -lt 90 ]
then
    tag=3
```
- 如果n满足[ $n -ge 80 ] 大于等于80 并且  [ $n -lt 90 ] 小于90 为标签3

- 同理
```
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
    tag=4
```
- 如果n [ $n -ge 90 ]大于等于90 并且  [ $n -le 100 ]小于等于100 ,为标签4
- 同理

```
else 
    tag=0
```
- 除此之外 标记 标签0
- case格式
```
case $tag in
    1)
	echo "not ok"
        ;;
    2)
        echo "ok"
        ;;
    3)
        echo "ook"
        ;;
    4)
        echo "oook"
        ;;
    *)
        echo "The number range is 0-100."
        ;; 
```
- 满足标签1 输出 not ok
- 满足标签2 输出 ok
- 满足标签3 输出 ook
- 满足标签4 输出 oook
- 其他不再这个范围内 输出  "The number range is 0-100." 数字范围为0-100
- 下面来测试下脚本 分别输入不同的数字 或者 其他来测试下
- 比如数字101,提示数字范围在0-100
```
[root@aming-01 shell]# sh case.sh
Please input a number: 101
The number range is 0-100.
[root@aming-01 shell]# 
```
- 来看下它的执行过程
```
[root@aming-01 shell]# sh -x case.sh
+ read -p ‘Please input a number: ‘ n
Please input a number: 101
+ ‘[‘ -z 101 ‘]‘
++ echo 101
++ sed ‘s/[0-9]//g‘
+ n1=
+ ‘[‘ -n ‘‘ ‘]‘
+ ‘[‘ 101 -lt 60 ‘]‘ 
+ ‘[‘ 101 -ge 60 ‘]‘
+ ‘[‘ 101 -lt 80 ‘]‘
+ ‘[‘ 101 -ge 80 ‘]‘
+ ‘[‘ 101 -lt 90 ‘]‘
+ ‘[‘ 101 -ge 90 ‘]‘
+ ‘[‘ 101 -le 100 ‘]‘
+ tag=0
+ case $tag in
+ echo ‘The number range is 0-100.‘
The number range is 0-100.
[root@aming-01 shell]# 
```
- 首先判断101这个变量存在不存在,存在继续往下走
- 之后做一个判断,存在把数字清空,最后n1为空 
- 判断它是否不为空,不为空那就正常,正常的话那就往下走,做一个判断
- 101是否小于60 
- 101是否大于等于60
- 101是否小于80
- 101是否大于等于80
- 101是否小于90
- 101是否大于等于90
- 101是否小于等于100
- 这些条件都不满足,所以tag= 0 所以提示  ‘The number range is 0-100.‘数字范围在0-100

- 如果输入78 输出ok
```
[root@aming-01 shell]# sh -x case.sh
+ read -p ‘Please input a number: ‘ n
Please input a number: 78
+ ‘[‘ -z 78 ‘]‘
++ echo 78
++ sed ‘s/[0-9]//g‘
+ n1=
+ ‘[‘ -n ‘‘ ‘]‘
+ ‘[‘ 78 -lt 60 ‘]‘
+ ‘[‘ 78 -ge 60 ‘]‘
+ ‘[‘ 78 -lt 80 ‘]‘
+ tag=2
+ case $tag in
+ echo ok
ok
[root@aming-01 shell]# 
```
- 如果输入88 ,输出ook
```
[root@aming-01 shell]# sh -x case.sh
+ read -p ‘Please input a number: ‘ n
Please input a number: 88
+ ‘[‘ -z 88 ‘]‘
++ echo 88
++ sed ‘s/[0-9]//g‘
+ n1=
+ ‘[‘ -n ‘‘ ‘]‘
+ ‘[‘ 88 -lt 60 ‘]‘
+ ‘[‘ 88 -ge 60 ‘]‘
+ ‘[‘ 88 -lt 80 ‘]‘
+ ‘[‘ 88 -ge 80 ‘]‘
+ ‘[‘ 88 -lt 90 ‘]‘
+ tag=3
+ case $tag in
+ echo ook
ook
[root@aming-01 shell]# 
```
- 如果输入一个不是数字的,提示输入一个数字
```
[root@aming-01 shell]# sh -x case.sh
+ read -p ‘Please input a number: ‘ n
Please input a number: dkkddkdk112
+ ‘[‘ -z dkkddkdk112 ‘]‘
++ echo dkkddkdk112
++ sed ‘s/[0-9]//g‘
+ n1=dkkddkdk
+ ‘[‘ -n dkkddkdk ‘]‘
+ echo ‘Please input a number.‘
Please input a number.
+ exit 1
[root@aming-01 shell]# 

```


20.5 shell脚本中的逻辑判断 20.6 文件目录属性判断 20.7 if特殊用法 20.8/20.9 case判断

标签:脚本   shell   文件目录   逻辑判断   

原文地址:http://ch71smas.blog.51cto.com/13090095/1984188

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!