shell 测试和判断

退出状态

退出状态的概念

1、linux系统中,当命令执行完后,系统会返回一个退出状态

2、退出状态用数值表示

3、作用:用于判断命令运行是否正确

退出状态的值

0 :命令运行成功

0 :运行失败

$? $? 是内置变量,最后一次执行的命令退出状态值被保存在 $? 中。

测试

作用

测试表达式的真假

测试执行结果:

真:返回0

假:返回非0

测试命令和结构

测试命令

test 

[]

 

测试结构(2种)

test expression

expression :数字、字符串、文本、文件属性比较,运算

[ expression ]

[: :是启动测试的命令 ] 不可少

空格 :“[”后和“]”前的空格是必不可少的。

整数运算符

1) test "num1" numeric_operator  "num2

2) [ "num1" numeric _operator "num2" ]

 

整数比较运算符

描述

num1 -eq num2

如果 num1等于num2,测试结果为0

num1 -ge num2

如果 num1大于或等于num2,测试结果为0

num1 -gt num2

如果 num1大于num2,测试结果为0

num1 -le num2

如果 num1小于或等于num2,测试结果为0

num1 -lt num2

如果 num1小于num2,测试结果为0

num1 -ne num2

如果 num1不等于num2,测试结果为0

 

例:

eq b

如果满足a等于b,则测试的结果为真(测试条件用0表示)

如果a不等于b,则测试结果为假(测试条件用非0表示)

 

code

[root@se ~]# num1=15

[root@se ~]# [ "$num1" -eq 15 ]

[root@se ~]# echo $?

0

[root@se ~]# 

[root@se ~]# test $num1 -eq 20

[root@se ~]# echo $?

1

[root@se ~]# 

 

字符串运算符

字符串运算符用于测试字符串是否为空、两个字符串是否相等或者是否不相等

 

字符串运算符

   描述

string

测试字符串string是否不为空

-n string

测试字符串string是否不为空

-z string

测试字符串string是否为空

string1 = string2

测试字符串string1是否与字符串string2相等

string1 != string2

测试字符串string1是否与字符串string2 不相等

 

string 和 -n string 都可以用来测试字符串是否为null

但是string 只能用 test命令 -n stirng 两种测试命令都可以

[root@se ~]# str1=""

[root@se ~]# test "$str1"

[root@se ~]# echo $?

1

[root@se ~]# test -n "$str1"

[root@se ~]# echo $?

1

[root@se ~]# [ -n "$str1" ]

[root@se ~]# echo $?

1

[root@se ~]# str2="dd"

[root@se ~]# [ -n "$str2" ]

[root@se ~]# echo $?

0

[root@se ~]# 

文件操作符

文件运算符

描述

-d file 

测试file是否为目录

-e file

测试file是否存在

-f file

测试file是否为普通文件

-r file

测试file是否是进程可读文件

-s file

测试file的长度是否不为0

-w file

测试file是否是进程可写文件

-x file

测试file是否是进程可执行文件

-L file

测试file是否符号化链接

 

[root@se ~]# [ -r post-install ]

[root@se ~]# echo $?

0

[root@se ~]# 

逻辑运算符

逻辑运算符主要包括逻辑非、逻辑与、逻辑或运算符

逻辑操作符

描述

expression 

 如果expression为假,则测试结果为真

expression1 –a expression2

如果expression1expression同时为真,则测试结果为真

expression1 –o expression2

如果expression1expression2中有一个为真,则测试条件为真

 

[root@se ~]#  [ -e post-install ]

[root@se ~]# echo $?

0

[root@se ~]#  [ ! -e post-install ]

[root@se ~]# echo $?

1

 

[root@se ~]# ll ls.sh 

-rwxr--r--. 1 root root 6 Nov 28 00:15 ls.sh

[root@se ~]# [ -e post-install -a -x ls.sh ]

[root@se ~]# echo $?

0

[root@se ~]# 

 

判断

简单if结构

if  expression

then

command

command

fi

 

code

 

#if_exam2.sh: first creates a file,and  test the file is existence,

# then test the file's authority.

#!/bin/sh

touch if_file1

if [ -e if_file1 ]

then echo "The file if_file1 is created successfully!"

fi

if [ -r if_file1 ]

then echo "The file can read."

fi

if [ -w if_file1 ]

then echo "The file can write."

fi

if [ -x if_file ]

then echo "The file can execute."

fi

 

 

关于分号

1、在使用这种简单if结构时,要特别注意测试条件后如果没有“;”,则then语句要换行,否则会产生不必要的错误。

2、如果ifthen可以处于同一行,则必须用“;”。

 

if else结构

       if expression1

       then 

              command

              …

              command

        else 

              command

              …

              command

        Fi

 

 

if/elif/else结构

 

if expression1

    then    

        command                                               

        command

        …

elif expression2

    then

        command

        command

        …

elif expressionN

then

    command

    …

    command

else 

    command

    …

    command

fi

 

 

case结构

case Variable  in

value1)

     command

 …

 command;;

value2)

     command

     …

     command;;

valueN)

     command

     …

     command;;

*)

     command

     …

     command;;

Esac

 

运算符

算术运算符

Linux Shell中,算术运算符包括:+(加运算)、-(减运算)、*(乘运算)、/(除运算)、%(取余运算)、**(幂运算),这些算术运算符的举例及其结果如下表所示:

 

运算符

举例

结果

+(加运算)

3+5

8

-(减运算)

5-3

2

*(乘运算)

5*3

15

/(除运算)

8/3

2

%(取余运算)

15%4

3

**(幂运算)

5**3

125

位运算符

位运算符在Shell编程中很少使用,通常用于整数间的运算,位运算符是针对整数在内存中存储的二进制数据流中的位进行的操作。

运算符

举例

解释和value

<<(左移)

value=4<<2

4左移2位,value值为16

>>(右移)

value=8>>2

8右移2位,value值为2

&(按位与)

value=8&4

8按位与4value值为0

|(按位或)

value=8|4

8按位或4value值为12

~(按位非)

value=~8

按位非8value值为-9

^(按位异或)

value=10^3

10按位异或3value值为9

 

自增自减运算符

自增自减操作符主要包括

前置自增(++variable) 

前置自减(--variable

后置自增(variable++

后置自减(variable--

 

前置操作

首先改变变量的值(++用于给变量加1--用于给变量减1),然后在将改变的变量值交给表达式使用

后置变量

则是在表达式使用后再改变变量的值

 

注意

要特别注意自增自减操作符的操作元只能是变量,不能是常数或表达式,且该变量值必须为整数型,例如:++1、(num+2++都是不合法的。

 

数字常量

1Linux Shell脚本或命令默认将数字以十进制的方式进行处理,

2、如果要使用其他进制的方式进行处理,则需对这个数字进行特定的标记或加前缀。

 

八进制 当使用0作为前缀时表示八进制

十进制 当使用0x进行标记时表示十六进制

num# 同时还可使演示用num#这种形式进行标记进制数

 

 

你可能感兴趣的:(shell 测试和判断)