[ STRING1 == STRING2 ]
如果2个字符串相同。 “=” may be used instead of “==” for strict POSIX compliance则为真。
[ STRING1 != STRING2 ]
如果字符串不相等则为真。
[ STRING1 < STRING2 ]
如果 “STRING1” sorts before “STRING2” lexicographically in the current locale则为真。
[ STRING1 > STRING2 ]
如果 “STRING1” sorts after “STRING2” lexicographically in the current locale则为真。
[ ARG1 OP ARG2 ]
“OP” is one of -eq , -ne , -lt , -le , -gt or -ge. These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers.
表达式可以借以下操作符组合起来,以降序列出:listed in decreasing order of precedence:
表 7.2. 组合表达式
操作效果
[ ! EXPR ]
如果 EXPR 是false则为真。
[ ( EXPR ) ]
返回 EXPR 的值。这 样可以用来忽略正常的操作符优先级。
[ EXPR1 -a EXPR2 ]
如果 EXPR1 and EXPR2 全真则为真。
[ EXPR1 -o EXPR2 ]
如果 EXPR1 或者 EXPR2 为真则为真。
[ (或作 test ) 内建命令对条件表达式使用一系列基于参数数量的规则来求值。更多关于这个主题的信息可以在Bash文档中查找。就像if 使用fi 来结束一样,在条件列完之后必须用">"来结束。
7.1.1.2. 后接then语句的命令
CONSEQUENT-COMMANDS 列出了跟在 then 语句后面可以是任何有效的UNIX命令,任何可执行的程序,任何可执行的shell脚本或者任何shell语句,除了 fi . 。重要地记住 then 和 fi 在shell里面被认为是分开的语句。因此,在命令行上使用的时候,他们用分号隔开。
在脚本中,if语句的不同部分通常是良好分隔的。以下是一些简单的例子:
7.1.1.3. 检查文件
第一个例子检查一个文件是否存在:
anny ~> cat msgcheck.sh
#!/bin/bash
echo "This scripts checks the existence of the messages file."
echo "Checking..."
if [ -f /var/log/messages ]
then
echo "/var/log/messages exists."
fi
echo
echo "...done."
anny ~> ./msgcheck.sh
This scripts checks the existence of the messages file.
Checking...
/var/log/messages exists.
...done.
7.1.1.4. 检查shell选项
加入到你的Bash配置文件中去:
# These lines will print a message if the noclobber option is set:
if [ -o noclobber ]
then
echo "Your files are protected against accidental overwriting using redirection."
fi
[/td]
环境[/tr]
以上的例子将在命令行输入后开始工作:
anny ~> if [ -o noclobber ]
; then echo ; echo "your files are protected
against overwriting."
; echo ; fi
your files are protected against overwriting.
anny ~>
以下的例子证明了 TEST-COMMANDS 可以是任何有返回和退出状态的UNIX命令,之后 if 再次返回零的退出状态:
anny ~> if ! grep $USER
/etc/passwd
More input> then echo "your user account is not managed locally"
; fi
your user account is not managed locally
anny > echo $?
0
anny >
以下能得到同样的结果:
anny > grep $USER
/etc/passwd
anny > if [ $? -ne 0 ]
; then echo "not a local account"
; fi
not a local account
anny >
7.1.2.2. 数字的比较
以下的例子是用了数值的比较:
anny > num
=`wc -l work.txt`
anny > echo $num
201
anny > if [ "$num" -gt "150" ]
More input> then echo ; echo "you've worked hard enough for today."
More input> echo ; fi
you've worked hard enough for today.
anny >
这个脚本在每个星期天由cron来执行。如果星期的数是偶数,他就提醒你把垃圾箱清理:
#!/bin/bash
# Calculate the week number using the date command:
WEEKOFFSET=$[ $(date +"%V") % 2 ]
# Test if we have a remainder. If not, this is an even week so send a message.
# Else, do nothing.
if [ $WEEKOFFSET -eq "0" ]; then
echo "Sunday evening, put out the garbage cans." | mail -s "Garbage cans out" your@your_domain.org
7.1.2.3. 字符串比较
一个通过比较字符串来测试用户ID的例子:
if [ "$(whoami)" != 'root' ]; then
echo "You have no permission to run $0 as non-root user."
exit 1;
fi
使用Bash,你可以缩短这样的结构。下面是以上测试的精简结构:
[ "$(whoami)" != 'root' ] && ( echo you are using a non-privileged account; exit 1 )