shell脚本之if语句

来源:互联网 发布:休闲食品网络市场调查 编辑:程序博客网 时间:2024/06/04 18:14

测试

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

test expression

比较流行的格式是:

[ expression ]

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

例如:

x=5
if test $x=5;then
    echo "x equals 5"
else
    echo "x is not equals 5"
fi


if [ $x=5 ];then           //[   和]的左右必须有空格
    echo "x equals 5"
else
    echo "x is not equals 5"
fi

文件表达式

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

表28-1: 测试文件表达式表达式如果下列条件为真则返回Truefile1 -ef file2file1 和 file2 拥有相同的索引号(通过硬链接两个文件名指向相同的文件)。file1 -nt file2file1新于 file2。file1 -ot file2file1早于 file2。-b filefile 存在并且是一个块(设备)文件。-c filefile 存在并且是一个字符(设备)文件。-d filefile 存在并且是一个目录。-e filefile 存在。-f filefile 存在并且是一个普通文件。-g filefile 存在并且设置了组 ID。-G filefile 存在并且由有效组 ID 拥有。-k filefile 存在并且设置了它的“sticky bit”。-L filefile 存在并且是一个符号链接。-O filefile 存在并且由有效用户 ID 拥有。-p filefile 存在并且是一个命名管道。-r filefile 存在并且可读(有效用户有可读权限)。-s filefile 存在且其长度大于零。-S filefile 存在且是一个网络 socket。-t fdfd 是一个定向到终端/从终端定向的文件描述符 。 这可以被用来决定是否重定向了标准输入/输出错误。-u filefile 存在并且设置了 setuid 位。-w filefile 存在并且可写(有效用户拥有可写权限)。-x filefile 存在并且可执行(有效用户有执行/搜索权限)。

这里我们有一个脚本说明了一些文件表达式:

#!/bin/bash# test-file: Evaluate the status of a fileFILE=~/.bashrcif [ -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."    fielse    echo "$FILE does not exist"    exit 1fiexit

字符串表达式

以下表达式用来计算字符串:

表28-2: 测试字符串表达式表达式如果下列条件为真则返回Truestringstring 不为 null。-n string字符串 string 的长度大于零。-z string字符串 string 的长度为零。

string1 = string2

string1 == string2

string1 和 string2 相同. 单或双等号都可以,不过双等号更受欢迎。string1 != string2string1 和 string2 不相同。string1 > string2sting1 排列在 string2 之后。string1 < string2string1 排列在 string2 之前。
#!/bin/bash# test-string: evaluate the value of a stringANSWER=maybeif [ -z "$ANSWER" ]; then    echo "There is no answer." >&2    exit 1fiif [ "$ANSWER" = "yes" ]; then    echo "The answer is YES."elif [ "$ANSWER" = "no" ]; then    echo "The answer is NO."elif [ "$ANSWER" = "maybe" ]; then    echo "The answer is MAYBE."else    echo "The answer is UNKNOWN."fi

整型表达式

下面的表达式用于整数:

表28-3: 测试整数表达式表达式如果为真...integer1 -eq integer2integer1 等于 integer2.integer1 -ne integer2integer1 不等于 integer2.integer1 -le integer2integer1 小于或等于 integer2.integer1 -lt integer2integer1 小于 integer2.integer1 -ge integer2integer1 大于或等于 integer2.integer1 -gt integer2integer1 大于 integer2.

这里是一个演示以上表达式用法的脚本:

#!/bin/bash# test-integer: evaluate the value of an integer.INT=-5if [ -z "$INT" ]; then    echo "INT is empty." >&2    exit 1fiif [ $INT -eq 0 ]; then    echo "INT is zero."else    if [ $INT -lt 0 ]; then        echo "INT is negative."    else        echo "INT is positive."    fi    if [ $((INT % 2)) -eq 0 ]; then        echo "INT is even."    else        echo "INT is odd."    fifi

这个脚本中有趣的地方是怎样来确定一个整数是偶数还是奇数。通过用模数2对数字执行求模操作, 就是用数字来除以2,并返回余数,从而知道数字是偶数还是奇数。

表28-4: 逻辑操作符操作符测试[[ ]] and (( ))AND-a&&OR-o||NOT!!

if [ ! \( $INT -ge $MIN_VAL -a $INT -le $MAX_VAL \) ]; then    echo "$INT is outside $MIN_VAL to $MAX_VAL."else    echo "$INT is in range."fi

因为 test 使用的所有的表达式和操作符都被 shell 看作是命令参数, 对于 bash 有特殊含义的字符,比如说 <,>,(,和 ),必须引起来或者是转义。


1 0
原创粉丝点击