# 语法结构:if command1
thenif command2
then
commands
fi
commands
fi# 为了程序美观,将嵌套的 if 语句扁平化if command1
then
commands
elif command2
then
commands
else
commands
fi# 以上的 if 条件语句都是以 command (if 后执行的命令的退出码)来进行判断# 但是实际进行逻辑判断时,需要使用很多的条件语句# 在 Shell 中实现条件判断# test 命令: 会在 test 后面的条件成立时返回退出码 0,在失败时返回退出码 1# 也就是说,if 语句本身并不支持条件的表达式,而依旧是通过命令的退出码来进行条件的判断# 只不过在使用条件判断时,可以使用 test 命令来实现对条件的判断# test 命令在 Shell 中提供了 2 种模式,一种是如下显示iftest condition
then
commands
fi# 另一种是使用了中括号来代替 test,也就是说,在中括号“[]”中写入条件判断语句# 这种写法和之前在进行变量的赋值和计算非常相似if[ condition ]then
commands
fi# 或者if[ condition ];then
commands
fi# if 也支持负荷条件判断[condition1]&&[condition2][condition1]||[condition2]# test 命令支持的判断条件:1.数值比较 2.字符串比较 3.文件比较# 可以利用 test 命令通过 if 语句实现数值条件、字符串条件、文件条件的比较判断# 举例:
vim ctest2
# 写入以下内容#! /bin/bashecho"please input a number:"read num
if[$num -gt 10 ]thenecho"the number is bigger than 10"fiif[$num -lt 10 ]&&[$num -gt 5 ]thenecho"the number is between 5 and 10"fi# 保存退出,并加上执行权限chmod u+x ctest2
# 执行
please input a number:
6
the number is between 5 and 10
2. 常用判断条件;
数值比较(使用字母操作符)
数值比较
相等
-eq
大于等于
-ge
大于
-gt
小于等于
-le
小于
-lt
不等于
-ne
字符串比较(使用数学符号)
字符串大于和小于是根据字符串中使用字符的 ASCII 码的顺序进行判断的
字符串比较
str1 = str2
相等
str1 != str2
不等
str1 > str2
大于
str1 < str2
小于
-n str1
字符串长度是否非 0
-z str1
字符串长度是否为 0
# 举例:
vim ctest3
# 写入以下内容#! /bin/bash
str1='abcd'
str2='bacd'if[$str1 \<$str2]thenecho'str1 is less than str2'elif[$str1 />$str2]thenecho'str1 is bigger than str2'elif[$str1=$str2]thenecho'str1 is equal than str2'fi# 保存退出,并加上执行权限chmod u+x ctest3
# 执行
./ctest3 # 输出 str1 is less than str2
文件比较
文件比较
-d file
检查 file 是否存在并是一个目录
-e file
检查 file 是否存在
-f file
检查 file 是否存在并是一个文件
-s file
检查 file 是否存在并非空
file1 -nt file2
检查 file1 是否比 file2 新
file1 -ot file2
检查 file1 是否比 file2 旧
-r file
检查 file 是否存在并可读
-w file
检查 file 是否存在并可写
-x file
检查 file 是否存在并可执行
-O file
检查 file 是否存在并属于当前用户所有
-G file
检查 file 是否存在并且默认组与当前用户相同
# 举例:
vim ctest4
# 写入以下内容#! /bin/bash
shellscript='ctest3'if[ -d $shellscript]thenecho" $shellscript is a directory"fiif[ -f $shellscript]thenecho" $shellscript is a file"fiif[ -x $shellscript]thenecho" $shellscript is an execute file"fi# 保存退出,并加上执行权限chmod u+x ctest4
# 执行
./ctest4
# 返回
ctest3 is a file
ctest3 is an execute file
高级判断
# 高级数学表达式(( expression ))# 高级字符串比较[[ expression ]]# 举例(高级数学表达式):
vim ctest5
# 写入以下内容#! /bin/bash
val1=10
# 在这里使用数值比较,# 并没有像刚才使用 test 命令时使用转义字符的方式,而是直接使用大于号if(( $val1 **2>90))then# 这里使用“(())”进行一个数学运算,# 不但可以将它作为一个判断的条件,也可以将它变成一个独立的命令执行(( val2 = $val1 **2))# 双引号中引用变量,会解析为变量的值# 为 val2 的值赋了一个 val1 的平方值echo"The square of $val1 is $val2"fi# 保存退出,并加上执行权限chmod u+x ctest5
# 执行
./ctest5 # 输出 The square of 10 is 100# 举例(高级字符串比较):
vim ctest6
# 写入以下内容#! /bin/bashif[[$USER== h* ]]thenecho"hello $USER"fi# 保存退出,并加上执行权限chmod u+x ctest6
# 执行
./ctest6 # 输出 hello hualaoshuan
3. case 语句。
case 和 if 不同,case 中可以同时判断多种条件
Shell 中的 case 语法就类似于 C 语言中的 switch—case。在 C 语言中,每一种条件都是在 switch 中的 case 中来实现的。
在 Shell 中,使用 case 语法,这里的 case 就相当于 C 语言中的 switch。这种语法类似于数据库查询中的 sql 语言中的 case
# 语法结构:case variable in
pattern1 | pattern2) commands1 ;;# 在 pattern1 或者 pattern2 这两种条件任何一种条件成立时会执行的命令
pattern3) commands3 ;;# 两个分号作为结束标志
*) default commands ;;# “*”匹配所有条件。case 是从上到下匹配条件的,星号表示上面没匹配到的条件都在这里执行
esac # 结束标志,和 case 字母顺序相反# 举例:
vim ctest7
# 写入以下内容#! /bin/bashecho"please input number of month"read num
case$numin
1)echo"the month is Jan.";;
2)echo"the month is Febr.";;
3)echo"the month is Mar.";;
4)echo"the month is Apr.";;
*)echo"wrong number";;
esac
# 保存退出,并加上执行权限chmod u+x ctest7
# 执行
./ctest7
please input number of month
3
the month is Mar.
## 由于 case 中的条件是从上到下执行的,## 那么在写默认值时,必须将“*”放在最后,否则后面的命令就不会被执行# 字符串判断# 举例:
vim ctest8
# 写入以下内容#! /bin/bashecho"please input Yes | No:"read var
case$varinyes| Yes | YES | Y | y)echo"You said yes"echo"OK!";;
no)echo"You said no";;
*)echo"wrong saying";;
esac
# 保存退出,并加上执行权限chmod u+x ctest8
# 执行
./ctest8
本篇概要:1 if — then 结构;2. 常用判断条件;3. case 语句。在实际使用中,我们需要对脚本中的逻辑进行控制。在面向过程的编程语言中,通常进行逻辑流程控制的结构就是条件和循环。在 Shell 中,提供了两种结构进行条件的判断if — thencase1 if — then 结构;单条件# 格式:# command 是 Shell 中的命令,会在 c...