bash shell中测试命令,bashshell测试命令


bash shell中测试命令

    test命令提供了if-than语句中测试不同条件的途径。如果test命令中列出的条件成立,test命令就会退出并返回退出状态吗0 。这样if-than语句就与其他编程语言中的if-than语句类似的方式工作了。如果条件不成立,test命令就会退出并返回非零的退出状态码,使得if-than语句不会被执行。

1    test 命令的基本格式

    test        condition

        conditiontest命令要测试的一系列参数和值。

2用在if-than语句中,格式是这样的:

        if     test     condition

        then

            commands

        fi

    如果不写condition部分,它会以非零的退出状态码退出。then 语句块不会被执行,并执行else语句块。

[root@local data]# cat test6.sh
#!/bin/bash
#
if    test
then
    echo "No expressing returns a Ture"
else
    echo "No expressing returens a False"
fi

[root@local data]# bash test6.sh

    "No expressing returens a False"

3 bash shell 的另一种常用的测试条件

    if    [    condition        ]

    then

        commands

    fi

    方括号定义了测试条件。

    test命令可以判断三类条件:

    (1)数值比较

    2)字符串比较

    3)文件比较

注意:方括号之后和第二个方括号之前必须加上一个空格,否则会报错。

4    数值比较

    下表列出了测试两个值时可用的条件参数

比较

描述

n1 -eq n2

检查n1是否等于n2

n1 -ge n2

检查n1是否大于或等于n2

n1 -gt n2

检查n1是否大于n2

n1 -le n2

检查是否小于等于n2

n1 -lt n2

检查n1是否小于n2

n1 -ne n2

检查n1是否不等于n2

注意:测试命令中只能处理整数

[root@local data]# cat number_test.sh
#!/bin/bash
value1=10 #定义变量value1、value2
value2=11
if [ $value1 -gt 5 ]    #测试value1的值是否大于5
then
    echo "The test value $value1 is greater than 5"
fi
if [ $value1 -eq $value2 ]        #测试value1的值是否和变量value2的值相等。
then
    echo "The values are equal"
else
    echo "The values are different"
fi

[root@local data]# bash number_test.sh

The test value 10 is greater than 5

The values are different

5    字符串比较

    条件测试运行字符串比较,下表列出了字符串附加测试

比较

描述

str1 = str2

检查str1是否和str2 相同

str1 != str2

检查str1是否和str2不同

str1 < str2

检查str1 是否比str2

str1 > str2

检查str1 是否比str2

-n str1

检查str1的长度是否非0

-z str1

检查str1的长度是否为0

    要测试一个字符串是否比另一个字符串大是一个麻烦的问题,主要有一下两个问题:

1)大于号和小于号必须转义,否则SHELL会把它们当做重定向符号,把字符串值当作文件名;

2)大于和小于顺序和sort命令所采用的不同。

在字符串比较测试中,大写字母被认为是大于小写字母的;比较测试中使用的是标准的ASCII顺序,根据每个字符的ASCII数值来决定排序结果。

[root@local data]# cat str_comparison.sh
#!/bin/bash
var1=baseball
var2=hockey
if [ $var1 \> $var2 ]
then
    echo "$var1 is greater than $var2"
else
    echo "$var1 is lees than $var2"
fi

[root@local data]# bash str_comparison.sh

baseball is lees than hockey

6    文件比较

    测试Linux上文件系统上文件和目录的状态。

比较

描述

-d file

检查file 是否存在并是一个目录

-e file

检查file是否存在

-f file

检查file是否存在并是一个文件

-r file

检查file是否存在并可读

-s file

检查file是否存在并非空

-w file

检查file是否存在并可写

-x file

检查file是否存在并可执行

-O file

检查file是否并属当前用户所有

-G file

检查file 是否存在且默认组与当前用户相同

file1 –nt file2

检查file1是否比file2

file1 –ot file2

检查file1是否比file2

[root@localdata]# cattestfile_exist.sh
#!/bin/bash
item_name=$HOME
echo
echo "The item being checked:$item_name"
echo
if [ -e $item_name ]
then #item_name does exist
    echo "The item,$item_name,does exist"
    echo "But is it a file?"
    echo
if [ -f $item_name ]
then #item_name is a file
    echo "Yes,$item_name is a file"
else #item_name is not a file
    echo "No,$item_name is not a file"
fi
else #item_name does not exist
    echo "The item,$item_name, does not exist"
    echo "Nothing to update"
fi

[root@local data]# bash testfile_exist.sh

The item being checked:/root

The item,/root,does exist

But is it a file?

No,/root is not a file

7    复合条件测试

if-than语句中可以使用布尔逻辑来组合测试

1 [ condition1 ] && [ condition2 ]

2 [ conditon1 ] || [ conditon2 ]

    第一种布尔运算使用AND布尔运算符来组合两个条件。要让then部分的命令执行,两个条件必须都满足。

    第二种布尔运算使用OR布尔运算符来组合两个条件。如果任意条件为TRUEthen部分的命令就会执行。

[root@localdata]# catcompund_comparison.sh

#!/bin/bash

#

if [ -d $HOME ] && [ -w $HOME/testing ]

then

    echo "The file exists and you can write to it"

else

    echo "I can not write to the file"

fi

[root@local data]# bash compund_comparison.sh

I can not write to the file

    第一个比较会检查用户的$HOME目录是否存在。第二个比较会检查在用户的$HOME目录下是否有个叫做testing的文件,以及用户是否有该文件的写入权限。如果两个比较中有一个失败了,if语句就会失败,shell就会执行else的部分。如果两个条件都通过了,则if语句通过,shell会执行then部分的命令。

相关内容