shell脚本编程-使用结构化命令(if/else)(转)
2016-08-26 18:29
826 查看
11.1 使用if-then语句
格式如下if语句会执行if行定义的那个命令,如果该命令的退出状态码是0,则then部分的语句就会执行,其他值,则不会
1 2 3 4 | if command then commands fi |
1 2 3 | if command; then commands fi |
11.2 if-then-else语句
格式如下;1 2 3 4 5 6 | if command then commands else commands fi |
11.3 嵌套if
格式如下:1 2 3 4 5 6 7 | if command1 then commands elif command2 then more commands fi |
11.4 test命令
格式如下1 | test condition |
1 2 3 4 | if test condition then commands fi |
方括号定义了test命令中用到的条件,注意方括号左右有一个空格
1 2 3 4 | if [ condition ] then commands fi |
数值比较
字符串比较
文件比较
11.4.1 数值比较
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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | ... val1=10 val2=11 if [ $val1 -gt 5 ] then echo ... fi if [ $val1 -eq $val2 ] then echo ... else echo ... fi ... |
1 2 3 4 5 6 7 8 | ... val1=`echo "scale = 4; 10 / 3" | bc` if[ $val1 -gt 3 ]//运行脚本时会报错 then echo ... fi ... |
11.4.2 字符串比较
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 |
– 字符串顺序注意事项:
– 大于小于符号必须转义,否则shell会把他们当做重定向符号而把字符串当做文件名
– 大于小于顺序和sort命令所采用的不同
– 在test命令中大写字母会被当成小于小写字母
– test命令使用标准的ASCII顺序,根据每个字符的ASCII数值来决定排序顺序
– 当将同样的字符串放进文件中并用sort命令排序时,小写字母会先出现
– sort命令使用系统的本地化语言设置中定义的排序顺
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $cat test #!/bin/bash val1=baseball val2=hockey if [ $val1 \> $val2 ] then echo "$val1 is greater than $val2" else echo "$val1 is less than $val2" fi $ $./test baseball is less than hockey |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $cat test #!/bin/bash val1=Testing val2=testing if [ $val1 \> $val2 ] then echo "$val1 is greater than $val2" else echo "$val1 is less than $val2" fi $ $./test Testing is less than testing $sort testfile testing Testing $ |
-n和-z参数:检查一个变量是否含有数据
1 2 3 4 5 6 7 8 9 10 11 | val1=testing val2=' ' if [ -n "$val1" ] //检查val1变量是否长度非零 then ... if [ -z "$val2" ] //检查val2变量是否长度为零 then ... if [ -z "$val3" ] //检查val1变量是否长度非零,这个变量并未在shell脚本中定义过,所以说明字符串长度仍然为零 then |
11.4.3 文件比较
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 | 检查file是否比file2新 |
file1 -ot file2 | 检查file是否比file2旧 |
11.5 复合条件测试
fi-then语句允许使用布尔逻辑来组合测试[ condition1 ] && [ condition2 ]
[ condition1 ] || [ condition2 ]
11.6 if-then的高级特性
用于数学表达式的双尖括号用于高级字符串处理功能的双方括号
11.6.1 使用双尖括号
双脚括号允许将高级数学表达式放入比较中格式:(( expression ))
expression:可以是任意的数学赋值或比较表达式
如下常用的表达式
符号 | 描述 |
---|---|
val++ | 后增 |
val– | 后减 |
++val | 先增 |
–val | 先减 |
** | 幂运算 |
<< | 左位移 |
& | 位布尔和 |
&& | 逻辑和 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $cat test #!/bin/bash val1=10 if (( $val1 ** 2 > 90 )) //大于号无需转义 then (( val2 = $val1 ** 2 )) echo "The square of $val1 is $val2" fi $ $./test The square of 10 is 100 $ |
11.6.2 使用双方括号
双方括号命令提供了针对字符串的高级特性格式:[[ expression ]]
expression:使用了test命令中采用的标准字符串进行比较
它提供了test命令为提供的另一个特性——模式匹配
模式匹配中,可以定义一个规则表达式来匹配字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 | $cat test #!/bin/bash if [[ $USER == r* ]] then echo "Hello $USER" else echo "Sorry, I do not know you" fi $ $./test Hello rich $ |
11.7 case命令
格式如下case命令会将指定的变量同不同模式进行比较,匹配则shell会执行该模式指定的命令
可以通过竖线操作符来分隔模式,在一行输出多个模式
星号会补货所有跟所有列出的模式都不匹配的值
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | //格式: case variable in pattern1 | pattern2) commands1;; pattern3) commands2;; *) default commands;; esac //例程: $ cat test #!/bin/bash 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 $ $./test Welcome, rich Please enjoy your visit $ |
相关文章推荐
- Linux-Shell脚本编程-学习-5-Shell编程-使用结构化命令-if-then-else-elif
- Shell脚本编程基础 三 使用结构化命令
- shell脚本编程基础(二)----使用结构化命令
- Linux-Shell脚本编程-学习-6-Shell编程-使用结构化命令-文件比较-case编程
- SHELL脚本的基础知识2——使用结构化命令
- shell脚本:使用if-then语句和test命令
- shell脚本编程之for语句、if语句使用介绍
- shell脚本编程之for语句、if语句使用介绍
- Linux编程 23 shell编程(结构化条件判断 命令if -then , if-then ... elif-then ...else,if test)
- centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课
- Shell脚本编程基础 四 更多的结构化命令
- Shell脚本编程之结构化命令
- shell脚本编程之for语句、if语句使用介绍
- shell脚本编程之for语句、if语句使用介绍
- shell脚本编程:条件判断if语句使用小结
- SHELL脚本的基础知识2——使用结构化命令
- C程序获取shell脚本命令输出,使用popen()函数
- 用来记录登录后使用命令的shell脚本
- 使用eval命令解决shell脚本中函数嵌套调用中的参数问题
- Shell脚本语法-- if/then/elif/else/fi