在实际中的shell编程中,我们需要对脚本中的逻辑进行控制,通常使用得恶就是条件和循环对逻辑流进行控制。
在shell中提供两种结构进行条件判断,即:
if then
(常用判断条件)case
if then
条件判断的优点:可以使用test
命令进行一个值范围的条件判断,case
只能使用特定的值。
case
条件判断的优点:简化值的判断,使多值判断变的清晰、易读。
if then条件判断
if then
条件判断类型
单条件
if command //command是shell中的一个命令,当command返回码为 0 时,条件成立
then
commands //shell中的命令
fi //fi和if相对应
shell种的if条件判断和其他很多高级语言有所不同:
if (condition) //其他语言的条件判断,一般都会有小括号,里面包含的是判断条件
{
commands
}
示例:
[root@shell if_then]# ls test.sh [root@shell if_then]# cat test.sh #!/bin/bash if date //条件判断成立 then echo "command worked" //输出字符串 fi [root@shell if_then]# bash test.sh Wed Apr 11 01:05:42 EDT 2018 //输出date command worked //输出字符串
全覆盖条件
if command
then
commands
else
command
fi
示例1:
[root@shell if_then]# cat test1.sh #!/bin/bash if date then echo "command worked" else echo "command filed" fi [root@shell if_then]# bash test1.sh Wed Apr 11 01:14:12 EDT 2018 command worked //条件成立返回command worked字符串
示例2:
[root@shell if_then]# cat test1.sh #!/bin/bash if date1 //shell中不存在date1命令 then echo "command worked" else echo "command filed" fi [root@shell if_then]#bash test1.sh test1.sh: line 3: date1: command not found command filed //条件不成立时,返回command filed字符串
多条件
if command1
then
commands
elif command2
then
commands
else
commands
fi
elif可以写多个,执行不同情况的判断
test命令
在shell中if并不支持条件判断表达式,我们需要使用test
命令来实现。
if test condition //这也是用退出码检测,test只是实现条件判断,if test condition == if [ condition ]
then
command
fi
复合条件判断
[condition1] && [condition2]:表示两个条件判断都必须成立,才进入条件语句
[ condition1] || [condition2]:表示两个条件判断只要一个成立,就进入条件语句
shell中常用的条件判断
test命令三类条件
数值比较
数值比较包含了:
相等 n1 -eq n2
大于等于 n1 -ge n2
大于 n1 -gt n2
小于等于 n1 -le n2
小于 n1 -lt n2
不等于 n1 -ne n2
第一个列子:
[root@shell if_then]# cat test2.sh //编辑脚本文件,test2.sh #!/bin/bash echo "please input a number:" read num if [ $num -gt 10 ] -gt表示大于的意思;中括号内必须有空格 then echo "the number is bigger than 10" elif [ $num -lt 10 ] && [ $num -gt 0 ] -lt表示小于的意思,这里表示要同时满足小于10,大于0 then echo "the number is in 0-10" fi [root@shell if_then]# bash test2.sh //测试一:输入12,大于10,输出 the number is bigger than 10 please input a number: 12 the number is bigger than 10 [root@shell if_then]# bash test2.sh //测试2:输入5,大于0小于10,输出 the number is in 0-10 please input a number: 5 the number is in 0-10
字符串比较
字符串比较包括:
str1 = str2 相同
str1 != str2 不同
str1 > str2 str1比str2大
str1 < str2 str1比str2小
//字符串的大小怎么判断?和ASIC码有关系,具体规则,按位比较,一但比较出结果,就结果比较
-n str1 字符串长度是否非0
-z str1 字符串长度是否为0
示例:
[root@shell if_then]# cat test_str.sh #!/bin/bash str1="abc" str2="bac" if [ $str1 \< $str2 ] //注意要有转义,才是比较,否则是重定向 then echo "str1 is less than str2" elif [ $str1 \> $str2 ] then echo "str1 is more than str2" elif [ $str1 \= $str2 ] then echo "str1 == str2" fi [root@shell if_then]# bash test_str.sh str1 is less than str2
文件比较
文件比较包括:
-d file 检查file是否存在并是一个目录
-e file 检查赋ile是否存在
-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是否存在并且默认组与当前用户相同
示例:
[root@shell if_then]# cat test_file.sh #!/bin/bash shellscript='ctest3' if [ -d $shellscript ] //判断是否存在并为一个目录 then echo "$shellscript is a directory" fi if [ -f $shellscript ] //判断是否存在并为一个文件 then echo "$shellscript is a file" fi if [ -x $shellscript ] //判断是否可执行 then echo "$shellscript is excutable" fi //以上是脚本具体内容
测试:
[root@shell if_then]# bash test_file.sh //当不存在ctest3文件时,条件都不成立,无返回 [root@shell if_then]# touch ctest3 //创建一个ctest3文件 [root@shell if_then]# bash test_file.sh //执行条件判断,是一个文件 ctest3 is a file [root@shell if_then]# rm -rf ctest3 && mkdir ctest3 //删除文件,创建成一个目录test3 [root@shell if_then]# bash test_file.sh //执条件判断,是一个目录,且可执行 ctest3 is a directory ctest3 is excutable
高级判断
shell提供了两种高级判断形式:
(( expression )):高级数学表达式比较
[[ expression ]]:高级字符串比较
高级数学表达式
示例:
[root@shell if_then]# cat test3.sh #!/bin/bash echo "input a num" read val //读取终端输入的数字,并用变量val保存 if (( $val ** 2 > 90)) //判断变量的平方是否大于90,如果大于则进入条件 then (( val1 = $val ** 2 )) //这是一个赋值过程,将输入的变量值的平方赋值给一个新的变量 echo "the square of $val is $val1" elif (( $val **2 <90)) then echo "reject!!!" fi
执行测试:
[root@shell if_then]# bash test3.sh input a num 11 the square of 11 is 121 [root@shell if_then]# bash test3.sh input a num 4 reject!!!
高级字符串
模式匹配
示例:
[root@shell if_then]# cat test4.sh #!/bin/bash if [[ $USER == root ]] //如果当前用户时root则条件成立。$USER是一个全局变量,表示当前用户名 then echo "hello $USER" fi [root@shell if_then]# bash test4.sh hello root
case条件判断
case
的语法:
case variable in
pattern1 | pattern2) commands1;;
pattern3) commands3;;
*) default commands;;
esac
其中:
variable是需要判断的变量。
pattern是判断条件,pattern1 | pattern2是或的关系,如果其中一个成立,则执行command1。如果pattern3成立,执行commands3。如果之前的都不成立,则执行default commands。最末位用两个分号结束。星号匹配所有其他条件。
esac和case也是成对出现的。
注意:case的条件判断是从上往下执行的,如果星号在最上面,就不会条件判断匹配了。
示例1:
[root@shell if_then]# cat test_case.sh #!/bin/bash echo "input a num(0-3)" read val //读取终端输入的值,并用变量val保存 case $val in //进入条件判断 0) echo "hello 0";; //如果是0,输出hello 0 1) echo "hello 1";; 2) echo "hello 2";; 3) echo "hello 3";; *) echo "who are you";; //如果上面的条件都不成立,则输出who are you esac [root@shell if_then]# bash test_case.sh input a num(0-3) 1 hello 1 [root@shell if_then]# bash test_case.sh input a num(0-3) 23 who are you
示例2:
case匹配字符串:
[root@shell if_then]# cat test_case2.sh #!/bin/bash echo "please input Yes | No" read val //读取字符串变量并保存 case $val in //进入case条件判断 Yes|yes|YES) //注意:shell严格区分大小写,所以为了保证用户体验,应该包含大小写的“YES” echo "Yse!!!";; No|no|NO) echo "No!!!";; *) echo "what?";; //如果不是上面的条件,则输出what? esac
执行测试:
[root@shell if_then]# bash test_case2.sh please input Yes | No yes Yse!!! [root@shell if_then]# bash test_case2.sh please input Yes | No YES Yse!!! [root@shell if_then]# bash test_case2.sh please input Yes | No FSX what?