Linux Shell脚本编写——使用结构化命令(二)
if command
then
command
fi
bash shell 的 if 语句会执行 if 行定义的那个命令,如果命令的退出状态码是 0,则代表成功执行,位于 then 部分的命令就会执行。如果 if 行定义的命令的退出状态码是其他,则 then 部分的命令将不会执行,且 if-then 语句不能测试跟命令的退出状态码无关的条件
代码 2-1
- : /data# cat demo1
- #!/bin / bash
- if date then echo "hello world"fi: /data# ./demo1 Fri Dec 2 05 : 26 : 27 CST 2016 hello world
如果故意放置一个不能执行的命令,则不会进入 then
代码 2-2
- : /data# cat demo2
- #!/bin / bash
- if aaa then echo "hello world"fi: /data# ./demo2. / demo2: line 2 : aaa: command not found
if-then-else
代码 2-3
- : /data# cat demo3
- #!/bin / bash
- if aaa then echo "hello world"
- else echo "hello java"fi: /data# ./demo3. / demo3: line 2 : aaa: command not found hello java
嵌套 if,else 可有可无,如果所有条件都不符合,则进入 else
if command1
then
command set 1
elif command2
then
command set 2
elif command3
then
command set 3
else
command set 4
fi
代码
2-4
- : /data# cat demo4
- #!/bin / bash
- if aaa then echo "hello world"elif who then echo "hello java"
- else echo "hello linux"fi: /data# ./demo4. / demo4: line 2 : aaa: command not found root pts / 0 2016 - 12 - 02 04 : 39(122.91.222.126) hello java
test 命令可以判断 3 类条件(注:[condition]condition 两边必须加空格):
if test condition
then
commands
fi
或
if[condition]
then
commands
fi
数字比较
比较 |
描述 |
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 |
代码 2-5
- : /data# cat demo3
- #!/bin / bash val1 = 10 val2 = 20
- if [$val1 - gt $val2] then echo "The test value $val1 is greater than $val2 "elif[$val1 - lt $val2] then echo "The test value $val1 is less than $val2"
- else echo "The test value $val1 is equal $val2 "fi: /data# ./demo3 The test value 10 is less than 20
字符串比较
比较 |
描述 |
str1 = str2 |
检查 str1 是否和 str2 相同 |
str1 != str2 |
检查 str1 是否和 str2 不同 |
str1 > str2 |
str1 是否比 str2 大 |
str1 < str2 |
str1 是否比 str2 小 |
-n str1 |
检查 str1 的长度是否非 0 |
-z str2 |
检查 str1 的长度是否为 0 |
代码 2-6
- : /data# cat demo4
- #!/bin / bash testUser = $USER badUser = Tom
- if [$USER = $testUser] then echo "hello $USER"fi
- if [$USER = $badUser] then echo "hello $USER"
- else echo "User is $USER"fi: /data# ./demo4 hello root User is root
字符串顺序
要测试一个字符串是否比另一个字符串大就变得繁琐了:
-
大于小于符号必须转义,否则 shell 会把它们当做重定向符号而把字符串值当做文件名
-
大于小于顺序和 sort 命令所采用的不同
代码 2-7
- : /data# cat demo5
- #!/bin / bash val1 = "hello"val2 = "Hello"
- if [$val1\ > $val2] then echo "The result is $val1 greater than $val2"elif[$val1\ < $val2] then echo "The result is $val1 less than $val2"fi: /data# ./demo5 The result is hello greater than Hello: /data# cat demo6
- #!/bin / bash val1 = "hello"val2 = "world"
- if [$val1\ > $val2] then echo "The result is $val1 greater than $val2"elif[$val1\ < $val2] then echo "The result is $val1 less than $val2"fi: /data# ./demo6 The result is hello less than world
sort 和 test 处理字符串相反,sort 默认升序,sort 中大写字母大于小写字母,而 test 中则是小写字母大于大写字母
代码 2-8
- : /data# cat demo7
- Hello
- hello
- :/data#sort demo7 hello Hello
字符串大小
- : /data# cat demo8
- #!/bin / bash val1 = "testing"val2 = ""
- if [ - n $val1] then echo "The string '$val1' is not empty"
- else echo "The string '$val1' is empty "fi
- if [ - n $val2] then echo "The string '$val2' is not empty"
- else echo "The string '$val2' is empty "fi
- if [ - n $val3] then echo "The string '$val3' is not empty"
- else echo "The string '$val3' is empty "fi: /data# ./demo8 The string'testing'is not empty The string''is not empty The string''is not empty
代码 2-8 中:
if [-n $val1] 判断 val1 变量长度是否为 0,而它长度正好非 0,所以 then 部分被执行了
if [-z $val2] 判断 val2 变量长度是否为 0,而它正好长度为 0,所以 then 部分被执行了
if [-z $val3] 判断 val3 变量长度是否为 0,这个变量并未在 shell 脚本中定义过,所以说明字符串长度仍未 0,所以 then 部分执行
文件比较
比较 |
描述 |
-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 旧 |
检查目录
代码 2-8
- : /data# cat demo9
- #/bin / bash
- if [ - d $HOME] then echo "Your home directory exists"cd $HOME ls - a
- else echo "There is a problem with your HOME directory"fi: /data# ./demo9 Your home directory exists....bash_history.bashrc.cache.hivehistory.pip.profile.pydistutils.cfg.rediscli_history.scala_history.selected_editor.spark_history.ssh.vim.viminfo
检查文件是否为空
代码 2-9
- : /data# cat demo1
- #!/bin / bash file = "text"touch $file
- if [ - s $file] then echo "The $file file exists and has data in it"
- else echo "The $file exists and is empty"fi date > $file
- if [ - s $file] then echo "The $file file exists and has data in it"
- else echo "The $file exists and is empty"fi: /data# ./demo1 The text exists and is empty The text file exists and has data in it
使用布尔逻辑来组合测试:
-
[condition1] && [condition2]
-
[condition1] || [condition2]
代码 2-10
- : /data# cat demo2
- #!/bin / bash
- if [ - d $HOME] && [ - w $HOME / testing] then echo "The file exists and you can write it"
- else echo "I cannot write to the file"fi: /data# ./demo2 I cannot write to the file
使用双圆括号将高级数学表达式放入比较中 ((expression))
符号 |
描述 |
val++ |
后增 |
val-- |
后减 |
++val |
先增 |
--val |
先减 |
! |
逻辑求反 |
- |
位求反 |
** |
幂运算 |
<< |
位左移 |
>> |
位右移 |
& |
位布尔和 |
| |
位布尔或 |
&& |
逻辑和 |
|| |
逻辑或 |
代码 2-11
- : /data# cat demo3
- #!/bin / bash val1 = 10
- if (($val1 * *2 > 90)) then((val2 = $val1 * *2)) echo "The square of $val1 is $val2"fi: /data# ./demo3 The square of 10 is 100
使用双方括号
[[expression]] 提供了 test 命令未提供的另一特性——模式匹配
代码 2-12
- : /data# cat demo4
- #!/bin / bash
- if [[$USER = r * ]] then echo "hello $USER"
- else echo "Sorry. I do not know you"fi: /data# ./demo4 hello root
case 命令会检查单个变量列表格式的多个值,符号 ";;" 类似 C 语言中的 break,但是又与 C 语言有点不太一样,C 语言允许没有 break,一直到匹配结束,而 shell 脚本中的 case 匹配如果没有 ";;" 这个符号则会报错
case variable in
pattern1 | pattern2) command1;;
pattern3) command2;;
*) default commands;;
代码 2-13
- : /data# cat demo5
- #!/bin / bash
- case $USER in root | Tom) 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: /data# ./demo5 Welcome root Please enjoy your visit
来源: http://www.bubuko.com/infodetail-1857778.html