1. if-then语句
shell
1. if-then语句express
格式:bash
if COMMAND ide
then
测试
COMMAND
this
filua
说明:当if 后的shell命令运行结果返回0值时,则then后的命令会执行,反之则不会执行ci
示例:字符串
[root@localhost ~]# vi test.sh #!/bin/bash #Testing if-then if pwd then echo "It worked" fi ~ ~ ~ "test.sh" 6L, 62C written [root@localhost ~]# ./test -bash: ./test: No such file or directory [root@localhost ~]# ./test.sh /root It worked
if后的pwd正常执行后返回0值,then后的语句继续执行
get
示例:
[root@localhost ~]# vi test.sh #!/bin/bash #Testing a bad command if BadCommand then echo "It worked" fi echo "We are outside the if statment" ~ ~ ~ "test.sh" 7L, 113C written [root@localhost ~]# ./test.sh ./test.sh: line 3: BadCommand: command not found We are outside the if statment
if 后的命令执行失败,then后的语句也未执行,可是if-then后的语句执行了。
2. if-then-else语句
格式:
if COMMAND
then
COMMAND
else
COMMAND
fi
说明:当if后的命令执行成功后会执行then下的命令,若是if后的命令没执行成功则会执行else下的命令
示例:
[root@localhost ~]# vi test.sh #!/bin/bash #Testing the else section # testuser=NoSuchUser # if grep $testuser /etc/passwd then echo "The bash files for user $testuser are:" ls -a /home/$testuser/.b* echo else echo "The user $testuser does not exist on this system." echo fi ~ ~ ~ ~ "test.sh" 14L, 249C written [root@localhost ~]# ./test.sh The user NoSuchUser does not exist on this system.
3. 嵌套if
3.1 if-then能够嵌套在else中,用于作条件判断。
格式:
if COMMAND
then
COMMAND
else
COMMAND
if COMMAND
then
COMMAND
fi
fi
示例:
[root@localhost ~]# vi test.sh #!/bin/bash #Testing nested if testuser=NosuchUser # if grep $testuser /etc/passwd then echo "The user $testuser is exist on this system" else echo "The user $testuser does not on this system" if ls /home/$testuser then echo "/home/$testuser is a directory" fi fi ~ ~ ~ ~ ~ "test.sh" 14L, 271C written [root@localhost ~]# ./test.sh The user NosuchUser does not on this system /home/NosuchUser is a directory
3.2 if-then-elif-then-elif-then...[else-]fi:能够增长多个判断条件并且比if嵌套的方式逻辑更加清晰
格式:
if
then
elif
then
...
...
else #可根据实际条件用或不用。
fi
示例:
[root@localhost ~]# vi test.sh #!/bin/bash #Testing if-then-elif-then-else-fi # file1=/home/NosuchUser if [ -f $file1 ] then echo "$file1 is a file" elif [ -d $file1 ] then echo "$file1 is a directory" else echo "$file1 isn\'t exist" fi ~ ~ ~ ~ ~ "test.sh" 13L, 210C written [root@localhost ~]# ./test.sh /home/NosuchUser is a directory
3.3 test语句
格式:test condition
在if-then-fi中的格式为:
if test condition
then
COMMAND
fi
说明:test主要用于在脚本中进行条件测试,也能够在脚本中用于测试命令,当命令成功执行或判断条件为ture时则test会返回0值,而后then下的命令继续执行。
[root@localhost ~]# vi test.sh #!/bin/bash #Testing test condition # if test then echo "It\'t ture" else echo "It\'t false" fi ~ ~ ~ ~ ~ "test.sh" 9L, 98C written [root@localhost ~]# ./test.sh It\'t false
上述可看出test后为空,返回的值非0,则执行了else下面的命令。
test命令还能够检测变量值是否为空。若不为空则返回0值执行then下的COMMADN。
[root@localhost ~]# vi test.sh #!/bin/bash #Testing test condition # variable=full # if test $variable then echo "$variable is exist" else echo "$variable is not exist" fi ~ ~ ~ [root@localhost ~]# ./test.sh full is exist
test命令还能够使用 [ condition ] 或` condition `来实现,方括号内先后都必须有空格,不然没法正常执行。
test命令可执行如下三种判断条件:数值比较、字符串比较、文件比较
3.3.1 数值比较
数值比较表达式
n1 -gt n2 :n1是否大于n2
n1 -ge n2 :n1是否大于等于n2
n1 -eq n2 :n1是否等于n2
n1 -le n2 :n1是否小于等于n2
n1 -lt n2 :n1是否小于n2
n1 -eq n2 :n1是否不等于n2
[root@localhost ~]# vi test.sh #!/bin/bash #Testing $var1 numeric test evaluations # var1=10 var2=4 # if [ $var1 -gt 5 ] then echo "Value $var1 is greater than 5" else echo "Value $var1 is not greater than 5" fi # if [ $var1 -eq $var2 ] then echo "Value $var1 is equal to $var2 " else echo "Value $var1 is not equal to $var2 " fi ~ ~ ~ ~ "test.sh" 20L, 305C written [root@localhost ~]# ./test.sh Value 10 is greater than 5 Value 10 is not equal to 4
test命令没法测试浮点数。
3.3.2 字符串比较
字符串比较表达式
str1 = str2 :str1等于str2
str1 != str2 :str1不等于str2
str1 < str2 :str1小于str2
str1 > str2 :str1大于str2
-n str1 :str1长度是否不为0
-z str1:str1长度是否为0
字符串比较时还应注意如下几点:
(1)字符串比较会比较全部字符的特征,例如字符大小写、标点符号等;
(2)在使用大于号和小于号时必须转义
(3)比较测试中大写字符被认为是小于小写字母的
(4)-n和-z是用来判断变量是否包含字符串的。
3.3.3 文件比较
文件比较表达式以下:
-d filename :检查文件是否存在且为一个目录
-f filename :检查文件是否存在且为一个文件
-e filename:检查文件是否存在
-r :检查文件是否可读
-s:检查文件是否非空
-w:检查文件是否可写
-x:检查文件是否可执行
-O:检查执行用户是否为文件的属主
-G:检查执行用户是否为文件的属组
file1 -nt file2 :检查file1是否比file2新,一般以文件建立时间来比较
file1 -ot file2 :检查file是否比file2旧,一般以建立时间来比较
4. 复合条件测试
if [ condition1 ] || [ condition2 ] then COMMAND fi
当[ condition1 ] 和 [ condition2 ] 任一为执行结果为真时会执行then下的COMMAND
if [ condition1 ] && [ condition2 ] then COMMAND fi
当[ condition1 ] 和 [ condition2 ]执行结果都为真时才会执行then下的COMMAND
5. if-then的高级特性
5.1 (( expression )):test还支持(( ))的这种用法
会用到的运算表达式除了test所支持的,还有如下几种:
var++:后增
var-- :后减
++var :先增
--var :先减
!:逻辑求反
~:位运算
**:幂运算
<<:左位移
>>:右位移
&:位布尔和
|:位布尔或
&&:逻辑和
||:逻辑或
双括号特性:
(1)大于号与小于号无需转义
(2)支持更多的运算表达式
(( $var ** 2 >90 ))
5.2 ` expression `:test支持[[]]模式
双方括号特性:支持模式匹配
[[ $USER == r* ]]
6. case命令
说明:case能够将if-then-elif-then-else-fi的模式简化,使用表格形式检查单个变量的多个值。
格式:
case VARIABLE in
PATTERN1 | PATTER2 ) COMMANDS1;;
PATTERN3 ) COMMANDS2;;
*)DEFAULT COMMANDS3;;
esac
示例:
将以下if-then脚本改造程case模式
if-then模式
[root@localhost ~]# vi test.sh #!/bin/bash #Looking for a possible value # if [ $USER = "root" ] then echo "Welcome $USER" echo "Please enjoy your visit" elif [ $USER = "rich" ] then echo "Welcome $USER" echo "Please enjoy your visit" elif [ $USER = "jessica" ] then echo "Special testing accont" elif [ $USER = "barbara" ] then echo "Do not forget to logout when you\'re done" else echo "Sorry, you are not allowed here" fi ~ "test.sh" 20L, 401C written [root@localhost ~]# ./test.sh Welcome root Please enjoy your visit
case模式:
[root@localhost ~]# vi test.sh #!/bin/bash #Looking for case case $USER in "root" | "rich") echo "Welcome $USER" echo "Please enjoy your visit";; "jessica") echo "Special testing accont";; "barbara") echo "Do not forget to logout when you\'re done";; *) echo "Sorry ,you are not allowed here";; esac ~ ~ "test.sh" 12L, 273C written [root@localhost ~]# ./test.sh Welcome root Please enjoy your visit