shell中的if语句

if语句语法如下:

if commands;then
	commands
[elif commands;then
	commands...]
[else
	commands]
fi

退出状态

当命令执行完毕后,命令(包括我们编写的脚本和shell函数)会给系统发送一个值,叫做退出状态。这个值是一个0到255之间的整数,说明命令执行成功或是失败。一个零值说明成功,其他所有值说明失败。shell提供了一个参数,我们可以用它检查退出状态。

ls -d /usr/bin
/usr/bin
echo $?
0
ls -d /bin/usr
ls: cannot access /bin/usr: No such file or directory
echo $?
2

if用法

if false; true; then echo "It's true."; fi
It's true.

测试
经常以if一块使用的命令是test,这个test命令执行各种各样的检查与比较。它有两种等价模式:

test expression

比较流行的格式是:

[expression]

这里的expression是一个表达式,其执行结果是true或者是false。当表达式为真时,这个test命令返回一个零退出状态,当表达式为假时,test命令退出状态为1.
文件表达式

以下表达式被用来计算文件状态

表达式 如果下列条件为真则返回True
file1 -ef file2 file1和file2拥有相同的索引号(通过硬链接两个文件名指向相同的文件)
file1 -nt file2 file1新于file2
file1 -ot file2 file1早于file2
-b file file存在并且是一个块(设备)文件
-c file file存在并且是一个字符(设备)文件
-d file file存在并且是一个目录
-e file存在
-f file file存在并且是一个普通文件
-g file file存在并且设置了组ID
-G file file存在并且由有效组ID拥有
-k file file存在并且设置了它的"sticky bit"
-L file file存在并且是一个符号链接
-O file file存在并且由有效用户ID拥有
-p file file存在并且是一个命名管道
-r file file存在并且可读(有效用户有可读权限)
-s file file存在且长度大于零
-S file file存在且是一个网络socket
-t fd fd是一个定向到终端/从终端定向的文件描述符。这可以被用来决定是否重定向了标准输入/输出错误
-u file file存在并且设置了setuid位
-w file file存在并且可写(有效用户可写权限)
-x file file存在并且可执行(有效用户有执行/搜索权限)
#!/bin/bash
# test-file: Evaluate the status of a file
FILE=~/.bashrc
if [ -e "$FILE" ]; then
    if [ -f "$FILE" ]; then
        echo "$FILE is a regular file."
    fi
    if [ -d "$FILE" ]; then
        echo "$FILE is a directory."
    fi
    if [ -r "$FILE" ]; then
        echo "$FILE is readable."
    fi
    if [ -w "$FILE" ]; then
        echo "$FILE is writable."
    fi
    if [ -x "$FILE" ]; then
        echo "$FILE is executable/searchable."
    fi
else
    echo "$FILE does not exist"
    exit 1
fi
exit

脚本末尾的exit命令,接受一个单独的,可选的参数,其成为脚本的退出状态。当不传递参数时,退出状态默认为0;以这种方式使用exit命令,如果$FILE展开成一个不存在的文件名,则此脚本提示失败。这个exit命令出现在脚本中的最后一行,是一个当一个当一个脚本“运行到最后”(到达文件末尾),不管怎样,默认情况下它以退出状态0终止。
类似地,通过带有一个整数参数的return命令,shell函数可以返回一个退出状态。如果我们打算把上面的脚本转变为一个shell函数,为了在更大的程序中包含此函数,我们用return语句代替exit命令,则得到期望的行为:

test_file () {
    # test-file: Evaluate the status of a file
    FILE=~/.bashrc
    if [ -e "$FILE" ]; then
        if [ -f "$FILE" ]; then
            echo "$FILE is a regular file."
        fi
        if [ -d "$FILE" ]; then
            echo "$FILE is a directory."
        fi
        if [ -r "$FILE" ]; then
            echo "$FILE is readable."
        fi
        if [ -w "$FILE" ]; then
            echo "$FILE is writable."
        fi
        if [ -x "$FILE" ]; then
            echo "$FILE is executable/searchable."
        fi
    else
        echo "$FILE does not exist"
        return 1
    fi
}

字符串表达式
测试字符串表达式

表达式 如果下列条件为真则返回True
string string不为null
-n string 字符串string的长度大于0
-z string 字符串string的长度为0
string1=string2 string1和string2相同。单或双等号都可以,不过双等号更受欢迎
string1!=string2 string1和string2不相同
string1>string2 string1排列在string2后面
string1<string2 string1排列在string2之前

注意:当与test一块使用的时候,>和<表达式操作符必须用引号引起来(或者用反斜杠转义),如果不这样,它们会被shell解释为重定向操作符,造成潜在的破坏结果。

整式表达式

表达式 如果为真
integer1 -eq integer2 integer1 等于 integer2
integer1 -ne integer2 integer1不等于 integer2
integer1 -le integer2 integer1小于或等于integer2
integer1 -lt integer2 integer1小于integer2
integer1 -ge integer2 integer1大于或等于integer2
integer1 -gt integer2 integer1 大于integer2

目前的bash版本包括一个复合命令,作为加强的test命令代替物。它使用以下语法:
[[expression]]
这里,类似于test,expression是一个表达式,其计算结果为真或假。这个[[]]命令非常相似于test命令(它支持所有的表达式),但是增加了一个重要的新的字符串表达式:
string1=~regex
其返回值为真,如果string1匹配扩展的正则表达式regex。这就为执行比如数据验证等任务提供了许多可能性。在我们前面的整数表达式例子中,如果常量INT包含除了整数之外的任何数据,脚本就会运行失败。这个脚本需要一种方式来证明此常量包含一个整数。
[[]]添加的另一个功能是==操作符支持类型匹配,正如路径名展开所做的那样。

if [[ $FILE == foo.* ]]; then

这就使[[]]有助于计算文件和路径名。

(()) -为整数设计

除了[[]]复合命令外,bash也提供了(())复合命令,其有利于操作整数,它支持一套完整的算术计算。
(())被用来执行算术真测试。如果算术计算的结果是非零值,其测试值为真。

结合表达式

ye也有可能把表达式结合起来创建更复杂的计算,通过使用逻辑操作符来结合表达式。有三个用于test和[[]]的逻辑操作,它们是AND,OR和NOT。test和[[]]使用不同的操作符来表达这些操作。

操作符 测试 [[]]and(())
AND -a &&
OR -o ||
NOT ! !

因为test使用的所有的表达式和操作符都被shell看做是命令参数(不像[[]]和(()))对于bash有特殊含义的字符,比如说<,>,(和),必须引起来或者转义。
知道了test和[[]]基本上完成相同的事情,test更传统,然而[[]]特定于bash,知道怎样使用test很重要,因为它被非常广泛地使用。但是显然[[]]更有用,并更易于编码。

控制操作符:分支的另一种方法

bash支持两种可以执行分支任务的控制操作符。&&(AND)和||(OR)操作符作用如同复合命令[[]]中的逻辑操作符。

猜你喜欢

转载自blog.csdn.net/weixin_41811413/article/details/87908609