12.1使用if-then语句
最基本的结构化命令
if command
then
command
fi
这里的command必须exit的返回值是0
shijianzhongdeMacBook-Pro:part_12 shijianzhong$ ./test1.sh /Users/shijianzhong/learn_shell/part_12 It worked shijianzhongdeMacBook-Pro:part_12 shijianzhong$ cat test1.sh #! /bin/bash if pwd then echo It worked fi shijianzhongdeMacBook-Pro:part_12 shijianzhong$
有些脚本可能的写法为if command; then
commands
fi
shijianzhongdeMacBook-Pro:part_12 shijianzhong$ ./test3.sh _driverkit:*:270:270:DriverKit:/var/empty:/usr/bin/false This is my first command This is my second command I can even put in other commands besides echo; ls: /home/_driver/.b*: No such file or directory shijianzhongdeMacBook-Pro:part_12 shijianzhong$ cat test3.sh #! /bin/bash testuser=_driver if grep $testuser /etc/passwd # 如果匹配不到就不会执行then语句 then echo "This is my first command" echo "This is my second command" echo "I can even put in other commands besides echo;" ls -a /home/$testuser/.b* fi shijianzhongdeMacBook-Pro:part_12 shijianzhong$
12.2 if-then-else语句
shijianzhongdeMacBook-Pro:part_12 shijianzhong$ cat test4.sh #! /bin/bash testuser=sidian if grep $testuser /etc/passwd then echo "This is my first command" echo "This is my second command" echo "I can even put in other commands besides echo;" ls -a /home/$testuser/.b* else echo "The user $testuser does not exist on this system " fi shijianzhongdeMacBook-Pro:part_12 shijianzhong$
12.3嵌套if
if command
then
if command
then
commands
else
if command
then
commands
fi
还有一种elif格式,跟很多编程语言一样,比较好理解
12.4 test命令
前面的if 条件孩子能测试命令退出状态的条件,test可以测试命令状态退出状态码之外的条件
test 用来测试变量中是否有内容
shijianzhongdeMacBook-Pro:part_12 shijianzhong$ a="" shijianzhongdeMacBook-Pro:part_12 shijianzhong$ b="12" shijianzhongdeMacBook-Pro:part_12 shijianzhong$ test $a shijianzhongdeMacBook-Pro:part_12 shijianzhong$ echo $? 1 shijianzhongdeMacBook-Pro:part_12 shijianzhong$ test $b shijianzhongdeMacBook-Pro:part_12 shijianzhong$ echo $? 0 shijianzhongdeMacBook-Pro:part_12 shijianzhong$
如果写在脚本里面用作判断也非常方便,代码不抄写了,逻辑比较简单
bash shell提供[]的条件测试
if [ condition ]
then
commands
fi
test与[]的功能相等,可以用来判断
数值比较,字符串比较,文件比较
shijianzhongdeMacBook-Pro:part_12 shijianzhong$ test 1 -eq 1 shijianzhongdeMacBook-Pro:part_12 shijianzhong$ echo $? 0 shijianzhongdeMacBook-Pro:part_12 shijianzhong$ test 1 -eq 2 shijianzhongdeMacBook-Pro:part_12 shijianzhong$ echo $? 1 shijianzhongdeMacBook-Pro:part_12 shijianzhong$
数值比较
-eq 等于 -ge 大于等于 -gt 大于 -le小于等于 -lt 小于 -ne 不相等
shijianzhongdeMacBook-Pro:part_12 shijianzhong$ ./numeric_test.sh The test value 10 is greater than 5 The values are different shijianzhongdeMacBook-Pro:part_12 shijianzhong$ cat numeric_test.sh #! /bin/bash value1=10 value2=11 if [ $value1 -gt 5 ] then echo "The test value $value1 is greater than 5" fi if [ $value1 -eq $value2 ] then echo "The values are equl" else echo "The values are different" fi shijianzhongdeMacBook-Pro:part_12 shijianzhong$
在bash shell只能处理整数,可以通过echo输出变量,但如果条件中,浮点数进行大小比较,就会报错了。
字符串比较
str1=str2 检查是否相同 !=不等于 >大于 <小于 -n 检查是否有内容 -z检查是否为空
检查相等
shijianzhongdeMacBook-Pro:part_12 shijianzhong$ ./test7.sh Welcome shijianzhong shijianzhongdeMacBook-Pro:part_12 shijianzhong$ cat test7.sh #! /bin/bash testuer=shijianzhong if [ $USER=$testuer ] then echo "Welcome $testuer" fi shijianzhongdeMacBook-Pro:part_12 shijianzhong$
检查不相等
shijianzhongdeMacBook-Pro:part_12 shijianzhong$ ./test8.sh This is not shijianzh shijianzhongdeMacBook-Pro:part_12 shijianzhong$ cat test8.sh #! /bin/bash testuer=shijianzh if [ $USER != $testuer ] then echo "This is not $testuer" else echo "Welcome $testuer" fi shijianzhongdeMacBook-Pro:part_12 shijianzhong$
检查字符串大小,大于,小于号 要记得用转义符号,比较测试,比较的是ASCII,所以大写字母小于小写字母。sort 命令排序小写字母会先出现
>或<不写转义符号,会当做重定向使用了。
shijianzhongdeMacBook-Pro:part_12 shijianzhong$ ./test9.sh baskball is less than hckey shijianzhongdeMacBook-Pro:part_12 shijianzhong$ cat test9.sh #! /bin/bash val1=baskball val2=hckey if [ $val1 > $val2 ] # 转义符号不要忘记 then echo "$val1 is greater than $val2" else echo "$val1 is less than $val2" fi shijianzhongdeMacBook-Pro:part_12 shijianzhong$
字符串大小的检查,-n,检查字符串是否有内容,有内容返回0 -z检查字符串是否为空,空返回0,不添加参数,自动检查是否有内容,有内容返回0,没有定义的变量,属于空
shijianzhongdeMacBook-Pro:part_12 shijianzhong$ [ $abcd ] shijianzhongdeMacBook-Pro:part_12 shijianzhong$ echo $? 1 shijianzhongdeMacBook-Pro:part_12 shijianzhong$ [ -z $abcd ] shijianzhongdeMacBook-Pro:part_12 shijianzhong$ echo $? 0 shijianzhongdeMacBook-Pro:part_12 shijianzhong$ a="" shijianzhongdeMacBook-Pro:part_12 shijianzhong$ [ $a ] shijianzhongdeMacBook-Pro:part_12 shijianzhong$ echo $? 1 shijianzhongdeMacBook-Pro:part_12 shijianzhong$
文件比较
Linux是一个万物皆文件的系统,if条件里面的一些参数可以测试文件的类型
除了-e 另外的前提是检查是否存在
-d file 检测文件是否为目录
-e 检查文件是否存在
-f 检查是否是一个文件
-r 检查是否是可读文件
-s 检查不为空的文件 (目录都为非空,只能检测文件)
-w 检测是否可读
-x 检测是否可执行
-O 检查并属当前用户所有
-G 检查file是否存在并且默认组与当前用户相同
file1 -nt file2 检查file1是否比 file2 新 (使用之前最好先检查文件是否存在)
file1 -ot file2 检查file1是否比file2 旧 (同上)
符合条件测试
&& and || or,这个跟其他编程差不多
if-then的高级特性
用于数学表达式的双括号(())
用于高级字符串处理功能的双方括号 [[]]
双括号 (( expression ))
shijianzhongdeMacBook-Pro:part_12 shijianzhong$ ./test23.sh The suqare of 10 is 100 shijianzhongdeMacBook-Pro:part_12 shijianzhong$ cat test23.sh #! /bin/bash val1=10 if (( $val1 ** 2 > 90)) # >可以直接用在数学表达式,支持**幂运算 then (( val2=$val1 ** 2)) echo "The suqare of $val1 is $val2" fi shijianzhongdeMacBook-Pro:part_12 shijianzhong$
使用双方括号
双方括号命令提供了针对字符串的比较的高级特性。
[[ exprission ]]
shijianzhongdeMacBook-Pro:part_12 shijianzhong$ ./test24.sh hello shijianzhong shijianzhongdeMacBook-Pro:part_12 shijianzhong$ cat test24.sh #! /bin/bash if [[ $USER == s* ]] # 双== ,通配模式 then echo "hello $USER" else echo "Sorry, I do not know you" fi shijianzhongdeMacBook-Pro:part_12 shijianzhong$
case命令
case与if比较一样
语法格式
case variable in
pattern1 | pattern2) command;;
pattern3)comands;;
*)commands;;
esac
抄写一个书中代码
shijianzhongdeMacBook-Pro:part_12 shijianzhong$ ./test26.sh Welcome shijianzhong Pleass enjoy you visit shijianzhongdeMacBook-Pro:part_12 shijianzhong$ cat test26.sh #! /bin/bash case $USER in # 用关键字in shijianzhong | barbarna) # 多条件用| echo " Welcome $USER" echo "Pleass enjoy you visit";; testing) # 第二种情况 echo "Special testing account";; *) echo "Sorry, you are not allowed here";; esac shijianzhongdeMacBook-Pro:part_12 shijianzhong$ vim test26.sh shijianzhongdeMacBook-Pro:part_12 shijianzhong$ ./test26.sh Sorry, you are not allowed here shijianzhongdeMacBook-Pro:part_12 shijianzhong$ cat test26.sh #! /bin/bash case $USER in # 用关键字in hijianzhong | barbarna) # 多条件用| echo " Welcome $USER" echo "Pleass enjoy you visit";; testing) # 第二种情况 echo "Special testing account";; *) echo "Sorry, you are not allowed here";; esac shijianzhongdeMacBook-Pro:part_12 shijianzhong$
小结
学习了if then 扩展可以为 if then elif then else
if 条件下语句产生一个退出状态码为0执行,非0状态码为false不执行