shell脚本之if_else

2018-11-15  本文已影响0人  L白水飘萍

(一)if/else 命令

if command1
then
commands
elif command2
then
more commands
else
more commands 
fi

(二) test 命令

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

test condition
if test condition
then
commands
fi

Ex1:

#!/bin/bash
# Testing the test command
#
if test
then
echo "No expression returns a True"
else
echo "No expression returns a False"
fi

out1:

No expression returns a False

Ex2:

#!/bin/bash
# Testing the test command
#
my_variable="Full"
#
if test $my_variable
then
echo "The $my_variable expression returns a True"
#
else
echo "The $my_variable expression returns a False"
fi

# Testing the test command
#
my_variable=""
#
if test $my_variable
then
echo "The $my_variable expression returns a True"
#
else
echo "The $my_variable expression returns a False"
fi

Out:

The Full expression returns a True
The expression returns a False
bash shell提供了另一种条件测试方法,无需在if-then语句中声明test命令。
if [ condition ]
then
commands
fi

(三)、数值比较

使用test命令最常见的情形是对两个数值进行比较。

n1 -eq n2 检查n1是否与n2相等   //equal 
n1 -ge n2 检查n1是否大于或等于n2 //great equal 
n1 -gt n2 检查n1是否大于n2 //great then 
n1 -le n2 检查n1是否小于或等于n2
n1 -lt n2 检查n1是否小于n2
n1 -ne n2 检查n1是否不等于n2

Ex1:

#!/bin/bash
# Using numeric test evaluations
value1=10
value2=11
#
if [ $value1 -gt 5 ]
then
echo "The test value $value1 is greater than 5"
fi
#
if [ $value1 -eq $value2 ]
then
echo "The values are equal"
else
echo "The values are different"
fi

Out:

The test value 10 is greater than 5
The values are different

(四)、字符串比较

条件测试还允许比较字符串值。

str1 != str2 检查 str1 是否和 str2 不同
str1 < str2 检查 str1 是否比 str2 小
str1 > str2 检查 str1 是否比 str2 大
-n str1 检查 str1 的长度是否非0
-z str1 检查 str1 的长度是否为0

注意在使用字符串的时候最好使用以下形式

[ -n "$str1" ]
[ -z  "$str2" ]

要测试一个字符串是否比另一个字符串大就是麻烦的开始。当要开始使用测试条件的大于或
小于功能时,就会出现两个经常困扰shell程序员的问题:

#!/bin/bash
# mis-using string comparisons
#
val1=baseball
val2=hockey
#
if [ $val1 \> $val2 ]244 第 12 章 使用结构化命令
then
echo "$val1 is greater than $val2"
else
echo "$val1 is less than $val2"
fi

Out:

baseball is less than hockey

(五) 文件比较

-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旧

(六) 复合条件的测试

if-then语句允许你使用布尔逻辑来组合测试。有两种布尔运算符可用

[ condition1 ] && [ condition2 ]  
[ condition1 ] || [ condition2 ]

(七) if-then的高级特性

bash shell提供了两项可在if-then语句中使用的高级特性:

双括号命令允许你在比较过程中使用高级数学表达式。其表达式如下:

(( expression ))

expression可以是任意的数学赋值或比较表达式。

val++ 后增
val-- 后减
++val 先增
--val 先减
! 逻辑求反
~ 位求反
** 幂运算
<< 左位移
>> 右位移
& 位布尔和
| 位布尔或
&& 逻辑和
|| 逻辑或

双方括号命令提供了针对字符串比较的高级特性。双方括号命令的格式如下:

[[ expression ]]

Ex1:

#!/bin/bash
# using pattern matching
#
if [[ $USER == r* ]]
then
echo "Hello $USER"
else
echo "Sorry, I do not know you"
fi

(八) Case命令

case variable in
pattern1 | pattern2) commands1;;
pattern3) commands2;;
*) default commands;;
esac

case命令会将指定的变量与不同模式进行比较。如果变量和模式是匹配的,那么shell会执行
为该模式指定的命令。可以通过竖线操作符在一行中分隔出多个模式模式。星号会捕获所有与已
知模式不匹配的值。
Ex1:

#!/bin/bash
# using the case command
#
case $USER in
rich | barbara)
echo "Welcome, $USER"
echo "Please enjoy your visit";;
testing)
echo "Special testing account";;
jessica)
echo "Do not forget to log off when you're done";;
*)
echo "Sorry, you are not allowed here";;
esac
上一篇 下一篇

猜你喜欢

热点阅读