十七周一次课

十七周一次课linux

20.5shell逻辑判断shell

20.6文件属性判断vim

20.7if特殊用法bash

20.8-20.9case判断spa

20.5shell逻辑判断文档

逻辑表达式

在[ ]中括号中:字符串

  • -lt:=little than 小于
  • -le:=little && equal 小于等于
  • -eq:=equal 等于
  • -ne:=no equal 不等于
  • -gt:=greater than 大于
  • -ge:=greater && equal 大于等于

在(( ))小括号中:
<,<=,==,!=,>,>=
get

注意: 使用双括号括起来input

[root@tianqi-01 ~]# for i in `seq 1 5`; do echo $i; done
1
2
3
4
5
[root@tianqi-01 ~]# 
it

有时候不用写脚本,1条命令足矣。

[root@tianqi-01 ~]# for i in `seq 1 5`
> do
> echo $i
> done
1
2
3
4
5
[root@tianqi-01 ~]# 

这条命令也能够不用分割,效果相同。

格式1

if 条件 ;then commond;fi

[root@tianqi-01 ~]# a=5
[root@tianqi-01 ~]# if [ $a -gt 3 ]

> then
> echo ok
> fi
ok

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

[root@tianqi-01 shell]# vim if1.sh 

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

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

格式2

if 条件1;then commond1;else commond2;fi

[root@tianqi-01 shell]# vim if2.sh 

#!/bin/bash

a=5

if [ $a -gt 3 ]

#注意[]内的空格

then

    echo "ok"

else

    echo "fault"

fi

[root@tianqi-01 shell]# sh if2.sh 
ok

[root@tianqi-01 shell]# sh -x if2.sh
+ a=5
+ '[' 5 -gt 3 ']'
+ echo ok
ok
[root@tianqi-01 shell]# 

格式3

if 条件1;then commond1;elif 条件2;then commond2;else commond3;fi

[root@tianqi-01 shell]# vim if3.sh

a=5
if [ $a -lt 3 ]
then
        echo "a<3"
elif [ $a -gt 6 ]
then
        echo "a>6"
else
        echo "Out of the zone"
fi

[root@tianqi-01 shell]# sh if3.sh 
Out of the zone

[root@tianqi-01 shell]# sh -x if3.sh
+ a=5
+ '[' 5 -lt 3 ']'
+ '[' 5 -gt 6 ']'
+ echo 'Out of the zone'
Out of the zone
[root@tianqi-01 shell]# 

关系

各个条件之间的关系可使用逻辑链接符:

条件A&&条件B:而且
条件A||条件B:或者

20.6文件属性判断

shell脚本中if常常用于判断文档的属性,好比判断是普通文件仍是目录文件,判断文件是否有读、写、执行权限等。if经常使用的选项有如下几个:

-e:判断文件或目录是否存在

-d:判断是否是目录文件以及是否存在

-f:判断是否是普通文件以及是否存在

-r:判断是否有读权限

-w:判断是否有写权限

-x:判断是否有执行权限

格式

若是某文件存在:

if [ -e filename ]

then

     commond

fi

以上命令可简化为:

[ -e filename ] && commond

&&先后的命都执行

或:

[ -e filename ] || commond

当 || 前面的执行不成功时再执行后面的命令

若是某文件不存在:

if [ ! -e filename ]

then

    commond

fi

“!”等同于取反。

[root@localhost shell]# vim file1.sh

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

[root@localhost tmp]# sh -x file1.sh 
+ a=/tmp/aminglinux
+ '[' -f /tmp/aminglinux ']'
+ touch /tmp/aminglinux

20.7if特殊用法

if [ -z "$a" ]:表示当变量a的值为空时会怎样

if [ -n "$a" ]:表示当变量a的值不为空时会怎样

-z和-n为相反的两个反条件。

[root@tianqi-01 shell]# vim if.sh

#!/bin/bash

n=`wc -l /tmp/test.txt`

if [ $n -gt 20 ]

then

    echo 1

else

    echo 0

fi

在该脚本中无语法错误,只是咱们预设/tmp/test.txt是存在的,若是该文件不存在,该脚本执行的时候就会报错:

[root@tianqi-01 shell]# sh if.sh
wc: /tmp/test.txt: No such file or directory
if.sh: line 5: [: -gt: unary operator expected
if.sh: line 13: $'\302\240\302\240\302\240\302\240echo': command not found
[root@tianqi-01 shell]# 

因此,为了不这种错误的发生,须要将脚本写的更加严谨,须要在执行“if [ $n -gt 20 ]”以前先确认文件“/tmp/test.txt”是否存在:

[root@tianqi-01 shell]# vim exit.sh

#!/bin/bash
n=`wc -l /tmp/test.txt`
if [ -z $n ]
then
        echo "error"
        exit
elif [ $n -lt 20 ]
then
        echo 1
else
        echo 0
fi

即,若是变量n为空则显示error并退出该脚本。

[root@tianqi-01 shell]# sh exit.sh
wc: /tmp/test.txt: No such file or directory
error

即,当该文件不存在的时候就会退出执行,不会提示存在语法错误。(该脚本存在逻辑错误,只作效果演示用)

注意: 在该表达式中引用变量时要用双引号引发来。

判断某参数存在:

[root@tianqi-01 shell]# vim exit1.sh

#!/bin/bash
if grep -wq 'user1' /etc/passwd
then
        echo "user1 exit"
fi

[root@tianqi-01 shell]# sh exit1.sh
user1 exit

判断某参数不存在

[root@tianqi-01 shell]# vim exit1.sh

#!/bin/bash
if  !  grep -wq 'user1' /etc/passwd
then
        echo "user1 exit"
fi

[root@tianqi-01 shell]# sh exit1.sh

说明: grep中-w选项=Word,表示过滤一个单词;-q,表示不打印过滤的结果。判断某参数不存在时使用!表示取反。

20.8-20.9case判断

格式:

case 变量名 in

     value1)

         commond1

         ;;

        value2)

            commod2

            ;;

        value3)

            commod3

            ;;

        *)  

            commod3

            ;;

esac

在case中,能够在条件中使用“|”,表示或的意思,如:

2|3)

    command

    ;;

[root@tianqi-01 shell]# vim case1.sh

#!/bin/bash

#判断是否输入有数值,空直接结束整个文本
read -p "Please input a number:" n

#read 让用户输出一些字符串,赋值给最后一个变量,这里的赋值是“n”
if [ -z "$n" ]                                                    //变量n为空
then
        echo "Please input a number."
        exit 1

#“exit 1”表示执行该部分命令后的返回值 ,即,命令执行完后使用echo $?的值
fi

n1=`echo $n|sed 's/[0-9]//g'`

#判断用户输入的字符是否为纯数字 ,若是是数字,则将其替换为空,赋值给$n1
if [ -n "$n1" ]
then
        echo "Please input a number."
        exit 1

#判断$n1不为空时(即$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

#tag的做用是为判断条件设定标签,方便后面引用

case $tag in
        1)
                echo "not ok"
                ;;
        2)
                echo "ok"
                ;;
        3)
                echo "ook"
                ;;
        4)
                echo "oook"
                ;;
        *)
                echo "The number rang is 0-100."
                ;;
esac

说明: 该脚本的做用是输入考试成绩,判断考试成绩的等级。

[root@tianqi-01 shell]# sh case1.sh
Please input a number:95
oook
[root@tianqi-01 shell]# sh case1.sh
Please input a number:82
ook
[root@tianqi-01 shell]# sh case1.sh
Please input a number:63
ok
[root@tianqi-01 shell]# sh case1.sh
Please input a number:35
not ok

知识点 1

  • shell 中 exit0 exit1 的区别:
    • exit(0):正常运行程序并退出程序;
    • exit(1):非正常运行致使退出程序;
    • exit 0 能够告知你的程序的使用者:你的程序是正常结束的。若是 exit 非 0 值,那么你的程序的使用者一般会认为你的程序产生了一个错误。
    • 在 shell 中调用完你的程序以后,用 echo $? 命令就能够看到你的程序的 exit 值。在 shell 脚本中,一般会根据上一个命令的 $? 值来进行一些流程控制。

 

友情连接:阿铭linux

相关文章
相关标签/搜索