if command
then
commands
fi
if command; then
commands
fi
if command
then
commands
else
commands
fi
if command1
then
commands
else
if command2
then
more commands
fi
fi
if command1
then
commands
elif command2
then
more commands
fi
if command1
then
commands
elif command2
then
more commands1
else
more commands2
fi
if command1
then
command set 1
elif command2
then
command set 2
elif command3
then
command set 3
elif command4
then
command set 4
fi
test condition
if test condition
then
commands1
else
commands2
fi
$ cat test6.sh
#!/bin/bash
# testing if a variable has content
#
my_variable="Full" // 情况二:my_variable=""
#
if test $my_variable
then
echo "The my_variable variable has content and returns a True."
echo "The my_variable variable content is: $my_variable"
else
echo "The my_variable variable doesn't have content,"
echo "and returns a False."
fi
$
$ ./test6.sh
The my_variable variable has content and returns a True.
The my_variable variable content is: Full
$
if [ condition ]
then
commands
fi
test 命令的数值比较功能:
| 比较 | 描述 |
|---|---|
| n1 -eq n2 | 检查 n1 是否等于 n2 |
| n1 -ge n2 | 检查 n1 是否大于或等于 n2 |
| n1 -gt n2 | 检查 n1 是否大于 n2 |
| n1 -le n2 | 检查 n1 是否小于或等于 n2 |
| n1 -lt n2 | 检查 n1 是否小于 n2 |
| n1 -ne n2 | 检查 n1 是否不等于 n2 |
数值条件测试可用于数字和变量。
if [ $value1 -gt 5 ]
if [ $value1 -eq $value2 ]
对于条件测试, bash shell 只能处理整数。尽管可以将浮点值用于某些命令(比如 echo),但它们在条件测试下无法正常工作。
条件测试还允许比较字符串值。
test 命令的字符串比较功能:
| 比较 | 描述 |
|---|---|
| str1 = str2 | 检查 str1 是否和 str2 相同 |
| str1 != str2 | 检查 str1 是否和 str2 不同 |
| str1 < str2 | 检查 str1 是否小于 str2 |
| str1 > str2 | 检查 str1 是否大于 str2 |
| -n str1 | 检查 str1 的长度是否不为 0 |
| -z str1 | 检查 str1 的长度是否为 0 |
字符串相等性
if [ $testuser = christine ]
if [ $testuser != christine ]
字符串顺序
if [ $string1 > $string2 ] // 没有报错,但结果与预期结果不符。需要使用反斜线(\)正确地转义大于号。
if [ $string1 \> $string2 ]
sort 命令处理大写字母的方法刚好与 test 命令相反:
test 命令和测试表达式使用标准的数学比较符号来表示字符串比较,而用文本代码来表示数值比较。
字符串大小
if [ -n $string1 ]
if [ -z $string2 ]
空变量和未初始化的变量会对 shell 脚本测试造成灾难性的影响。如果不确定变量的内容,那么最好在将其用于数值或字符串比较之前先通过-n 或-z 来测试一下变量是否为空。
比较测试允许测试 Linux 文件系统中文件和目录的状态。
test 命令的文件比较功能
| 比较 | 描述 |
|---|---|
| -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 旧 |
检查目录:-d 测试会检查指定的目录是否存在于系统中。如果打算将文件写入目录或是准备切换到某个目录, 那么先测试一下总是件好事:
#!/bin/bash
jump_directory=/home/Torfa
if [ -d $jump_directory ]
then
cd $jump_directory
ls
else
echo "The $jump_directory directory does NOT exist."
fi
检查对象是否存在:-e 测试允许在使用文件或目录前先检查其是否存在:
location=$HOME
file_name="sentinel"
if [ -d $location ]
if [ -e $location/$file_name ]
检查文件:-e 测试可用于文件和目录。如果要确定指定对象为文件,那就必须使用-f 测试:
object_name=$HOME // object_name=$HOME/sentinel
if [ -e $object_name ]
if [ -f $object_name ]
检查是否可读:在尝试从文件中读取数据之前,最好先使用-r 测试检查一下文件是否可读:
pwfile=/etc/shadow
if [ -f $pwfile ]
if [ -r $pwfile ]
检查空文件:应该用-s 测试检查文件是否为空, 尤其是当你不想删除非空文件时。要当心,如果-s 测试 成功,则说明文件中有数据:
file_name=$HOME/sentinel
if [ -f $file_name ]
if [ -s $file_name ]
检查是否可写:-w 测试可以检查是否对文件拥有可写权限:
item_name=$HOME/sentinel
if [ -f $item_name ]
if [ -w $item_name ]
检查文件是否可以执行:-x 测试可以方便地判断文件是否有执行权限。虽然可能大多数命令用不到它,但如果想在 shell 脚本中运行大量程序, 那就得靠它了:
item_name=$HOME/scripts/can-I-write-to-it.sh
if [ -x $item_name ]
检查所有权:-O 测试可以轻松地检查你是否是文件的属主:
if [ -O /etc/passwd ]
检查默认属组关系:-G 测试可以检查文件的属组, 如果与用户的默认组匹配,则测试成功。 -G 只会检查默认组而非用户所属的所有组:
if [ -G $HOME/TestGroupFile ]
检查文件日期:
if [ $HOME/Downloads/games.rpm -nt $HOME/software/games.rpm ]
(command)
echo $BASH_SUBSHELL
if (echo $BASH_SUBSHELL)
then
xxx
else
xxx
fi
双括号命令允许在比较过程中使用高级数学表达式。 test 命令在进行比较的时候只能使用 简单的算术操作。双括号命令提供了更多的数学符号。双括号命令的格式如下:
(( expression ))
除了test 命令使用的标准数学运算符,下表还列出了双括号中可用的其他运算符。双括号命令符号:
| 符号 | 描述 |
|---|---|
| val++ | 后增 |
| val– | 后减 |
| ++val | 先增 |
| –val | 先减 |
| ! | 逻辑求反 |
| ~ | 位求反 |
| ** | 幂运算 |
| << | 左位移 |
| >> | 右位移 |
| & | 位布尔 AND |
| | | 位布尔 OR |
| && | 逻辑 AND |
| || | 逻辑 OR |
双括号命令既可以在 if 语句中使用,也可以在脚本中的普通命令里用来赋值。
val1=10
if (( $val1 ** 2 > 90 ))
then
(( val2 = $val1 ** 2 ))
fi
[[ expression ]]
if [[ $BASH_VERSION == 5.* ]]
case variable in
pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) default commands;;
esac
$ cat ShortCase.sh
#!/bin/bash
# Using a short case statement
#
case $USER in
rich | christine)
echo "Welcome $USER"
echo "Please enjoy your visit.";;
barbara | tim)
echo "Hi there, $USER"
echo "We're glad you could join us.";;
testing)
echo "Please log out when done with test.";;
*)
echo "Sorry, you are not allowed here."
esac
$
$ ./ShortCase.sh
Welcome christine
Please enjoy your visit.
$
if (which yum &> /dev/null) // 检查是否有 yum 工具