linux shell脚本使用结构化命令
内容:
1 if command 2 then 3 commands 4 fi
这里需要注意的是在其他语言中if 语句之后的对象是一个等式来测试是TRUE还是FALSE值,而在bash shell中if 语句会运行if 行定义那个命令。如果该命令退出码是数字0,则表示该命令运行成功,位于then 后面的命令就会运行。如果退出码是其他值,那么then后面的命令就不会执行。
1 oracle@suse:~> cat test 2 #!/bin/bash 3 #test if-then 4 if ls 5 then 6 echo "ls worked" 7 fi
将第4行ls命令换成bash shell不识别的命令,比如sdjflsdjfl,执行脚本会报错.
1 oracle@suse:~> cat test 2 #!/bin/bash 3 #test if-then 4 if sdjflsdjfl 5 then 6 echo "ls worked" 7 fi 8 oracle@suse:~> ./test 9 ./test: line 3: sdjflsdjfl: command not found
1 if command 2 then 3 commands 4 else 5 commands 6 fi
当if语句中的命令退出码是0时,then部分命令会执行,否则就会执行else部分命令。
2.1嵌套if,当脚本代码中有多个判断条件时,可以用elif 代替else部分,格式如下:
1 if command1 2 then 3 commands 4 elif command2 5 then 6 commands 7 fi
1 oracle@suse:~> cat test1 2 #!/bin/bash 3 if jjj 4 then 5 echo "1 worked" 6 elif lllls 7 then 8 echo "2 worked" 9 elif date 10 then 11 echo "3 worked" 12 fi 13 oracle@suse:~> ./test1 14 ./test1: line 2: jjj: command not found 15 ./test1: line 5: lllls: command not found 16 Thu Apr 30 11:41:16 CST 2015 17 3 worked
3.如果test命令中列出的条件成立,test命令就会退出并返回退出码0,如果条件不成立test命令就会退出并返回退出码1。test 命令,格式:
1 test condition
与if-then配合使用
1 if test condition 2 then 3 commands 4 fi
bash shell另一种声明test方法,用方括号[],需要注意[]与condition之间有空格,就是必须在左括号右侧和右括号左侧各加一个空格,否则会报错。
if [ condition ] then commands fi
3.1使用test进行数值比较
n1 -eq n2 检查n1是否等于n2
n1 -gt n2 检查n1是否大于n2
n1 -ge n2 检查n1是否大于等于n2
n1 -lt n2 检查n1是否小于n2
n1 -le n2 检查n1是否小于等于n2
n1 -ne n2 检查n1是否不等于n2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 oracle@suse:~> cat test2.sh 2 #!/bin/bash 3 #using numeric test comparisons 4 var1=100 5 var2=11 6 7 if [ $var1 -gt 88 ] 8 then 9 echo "the value $var1 is greater than 88" 10 fi 11 12 if [ $var1 -gt $var2 ] 13 then 14 echo $var1 ">" $var2 15 else 16 echo $var1"<="$var2 17 fi 18 19 oracle@suse:~> ./test2.sh 20 the value 100 is greater than 88 21 100 > 11
3.2 使用test进行字符串比较
str1 = str2 检查str1是否和str2相同
str1 > str2 检查str1是否比str2大
str1 < str2 检查str1是否比str2小
str1 != str2 检查str1是否和str2不同
-n str1 检查str1长度是否非零
-z str1 检查str1长度是否为零
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 oracle@suse:~> cat test3.sh 2 #!/bin/bash 3 #testing string 4 5 str1=hello 6 str2=word 7 8 if [ -z $str ] 9 then 10 str=$str2 11 echo $str 12 fi 13 14 if [ $str1 = $str2 ] 15 then 16 echo "str1 = str2" 17 else 18 echo "str1 != str2" 19 fi 20 oracle@suse:~> ./test3.sh 21 word 22 str1 != str2
需要注意使用大于号和小于号时候要在其前面使用转义字符 \ .否则可能会报错,或者将大小于号当成重定向。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 oracle@suse:~> cat test4.sh 2 #!/bin/bash 3 #testing string 4 5 str1=abcd 6 str2=abcdef 7 8 if [ $str1 \< $str2 ] 9 then 10 echo "$str1 less than $str2" 11 fi 12 oracle@suse:~> ./test4.sh 13 abcd less than abcdef
3.3使用test进行文件比较
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 -d file 检查file是否存在并是一个目录 2 oracle@suse:~/testshell> cat testfile.sh 3 #/bin/bash 4 #test "-d file" 5 if [ -d $HOME/testshell ] 6 then 7 echo "$HOME/testshell is exists" 8 cd $HOME/testshell 9 pwd 10 else 11 echo "$HOME/testshell is not exists" 12 fi 13 14 if [ -d $HOME/nba ] 15 then 16 echo "$HOEM/nba is exists" 17 cd $HOME/nba 18 pwd 19 else 20 echo "$HOME/nba is not exists" 21 fi 22 23 oracle@suse:~/testshell> ./testfile.sh 24 /home/oracle/testshell is exists 25 /home/oracle/testshell 26 /home/oracle/nba is not exists
3.4复合条件测试,if-then允许使用布尔逻辑组合测试
[ command1 ] && [ command2 ]
[ command1 ] || [ command2 ]
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 oracle@suse:~/testshell> cat showhello.sh 2 #/bin/bash 3 echo "hello world" 4 oracle@suse:~/testshell> cat testfile2.sh 5 #/bin/bash 6 #test [ command1 ] && [ command2 ] 7 8 if [ -f $HOME/testshell/showhello.sh ] && [ -x $HOME/testshell/showhello.sh ] 9 then 10 cd $HOME/testshell 11 ./showhello.sh 12 else 13 echo "it is not exists" 14 fi 15 16 oracle@suse:~/testshell> ./testfile2.sh 17 hello world
3.5 if-then高级特性双尖括号与双中括号,(( expression ))双尖括号命令允许将高级数学表达式放入比较中,[[ expression ]]双方括号命令提供了针对字符串比较的高级特性。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 oracle@suse:~/testshell> cat testfile3.sh 2 #/bin/bash 3 #using double parenthesis 4 5 var1=10 6 var2=100 7 8 if (( ++var1*10 > var2 )) 9 then 10 echo "$var1*10 is greater $var2" 11 else 12 echo "$var1*10" 13 fi 14 15 if [[ $USER == ora* ]] 16 then 17 echo "hello $USER" 18 else 19 echo "there is no this user" 20 fi 21 oracle@suse:~/testshell> ./testfile3.sh 22 11*10 is greater 100 23 hello oracle
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
case variable in pattern1 | pattern2 ) commands1 ;; pattern3) commands2;; *) default commands;; esac
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 oracle@suse:~/testshell> cat testcase.sh 2 #/bin/bash 3 #using the case command 4 5 case $USER in 6 rich) 7 echo "hello rich";; 8 jim) 9 echo "hello jim";; 10 tom) 11 echo "hello tom";; 12 *) 13 echo "you are no here" 14 echo "user is $USER";; 15 esac 16 oracle@suse:~/testshell> ./testcase.sh 17 you are no here 18 user is oracle