Shell编程—使用结构化命令

1使用if-then语句

f-then语句有如下格式。

if command 
then
    commands 
fi

bash shell的if语句会运行if后面的那个命令。如果该命令的退出状态码是0(该命令成功运行),位于then部分的命令就会被执行。如果该命令的退出状态码是其他值, then部分的命令就不会被执行,bash shell会继续执行脚本中的下一个命令。fi语句用来表示if-then 语句到此结束。

举例:

$ vim test1.sh
#!/bin/bash
# testing the if statement 
if pwd
then 
    echo "It worked" 
fi 

命令行运行该脚本时,会得到如下结果:

 

 这个例子中,shell执行了if行中的pwd命令。由于退出状态码是0,它就又执行了then部分的echo语句。

下面是另外一个例子:

$ vim test2.sh                                              
#!/bin/bash
# testing a bad command 
if IamNotaCommand
then     
echo "It worked" 
fi
echo "We are outside the if statement"

执行:

 

 在这个例子中,我们在if语句行故意放了一个不能工作的命令。由于这是个错误的命令,所以它会产生一个非零的退出状态码,且bash shell会跳过then部分的echo语句。还要注意,运行 if语句中的那个错误命令所生成的错误消息依然会显示在脚本的输出中。

 

此外,还有一种写法:

if command; then  
commands
fi

通过把分号放在待求值的命令尾部,就可以将then语句放在同一行上了。

 

在then部分,你可以使用不止一条命令:

$ vim test3.sh
#!/bin/bash
# testing multiple commands in the then section
 testuser=Christine
 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* 
fi

if语句行使用grep命令在/etc/passwd文件中查找某个用户名当前是否在系统上使用。如果有用户使用了那个登录名,脚本会显示一些文本信息并列出该用户HOME目录的bash文件

 

 但是,如果将testuser变量设置成一个系统上不存在的用户,则什么都不会显示:

 

 if-then-else语句

格式:

if command 
then    
    commands 
else    
    commands 
fi

例子:

$ vim test4.sh
#!/bin/bash
# testing the else section
 testuser=NoSuchUser
 if grep $testuser /etc/passwd 
 then
     echo "The bash files for user $testuser are:"    
     ls -a /home/$testuser/.b*    
     echo 
else    
    echo "The user $testuser does not exist on this system."    
    echo 
fi

执行:

 

3 嵌套if

有时需要检查脚本代码中的多种条件。对此,可以使用嵌套的if-then语句。

比如要检查/etc/passwd文件中是否存在某个用户名以及该用户的目录是否尚在,可以使用嵌套的if-then语句。嵌套的if-then语句位于主if-then-else语句的else代码块中。

$ ls -d /home/NoSuchUser/
/home/NoSuchUser/
$
$ vim test5.sh
#!/bin/bash
# Testing nested ifs
 testuser=NoSuchUser
 if grep $testuser /etc/passwd 
 then
     echo "The user $testuser exists on this system." 
else
    echo "The user $testuser does not exist on this system."    
 if ls -d /home/$testuser/    
 then
       echo "However, $testuser has a directory."    
  fi 
fi

执行:

 

上述例子可以使用另一个if-then语句延续else部分。

if command1 
then
    commands 
elif command2 
then
     more commands 
fi

elif语句行提供了另一个要测试的命令,这类似于原始的if语句行。如果elif后命令的退出状态码是0,则bash会执行第二个then语句部分的命令。

改变上面的例子:

$ vim test5.sh
#!/bin/bash
# Testing nested ifs - use elif
 testuser=NoSuchUser 
if grep $testuser /etc/passwd 
then
    echo "The user $testuser exists on this system."
elif ls -d /home/$testuser 
then
    echo "The user $testuser does not exist on this system."
    echo "However, $testuser has a directory."
else
   echo "The user $testuser does not exist on this system."
   echo "And, $testuser does not have a directory."
fi

执行:

 

 在/home/NoSuchUser目录被删除之前,这个测试脚本执行的是elif语句,返回零值的退出状态。因此elif的then代码块中的语句得以执行。删除了/home/NoSuchUser目录之后,elif语句返回的是非零值的退出状态。这使得elif块中的else代码块得以执行。

 

可以继续将多个elif语句串起来,形成一个大的if-then-elif嵌套组合:

if command1 
then
    command set 1 
elif command2 
then
   command set 2 
elif command3 
then
   command set 3 
elif command4 
then
   command set 4
else
   command set 5
fi

test命令

test命令提供了在if-then语句中测试不同条件的途径。如果test命令中列出的条件成立, test命令就会退出并返回退出状态码0。如果条件不成立,test命令就会退出并返回非零的退出状态码,使得if-then语句不会再被执行。

test命令的格式非常简单:

test condition

当用在if-then语句中时,test命令看起来是这样的:

if test condition 
then 
    commands 
fi

如果不写test命令的condition部分,它会以非零的退出状态码退出,并执行else语句块:

$ vim test6.sh 
#!/bin/bash 
# Testing the test command 
 if test 
 then
     echo "No expression returns a True" 
 else
     echo "No expression returns a False" 
 fi 

执行结果:

 

 当加入一个条件时,test命令会测试该条件。

例如,可以使用test命令确定变量中是否有内容,这只需要一个简单的条件表达式:

$ vim test6.sh 
#!/bin/bash 
# Testing the test command 
 my_variable="Full" 
 if test $my_variable 
 then
     echo "The $my_variable expression returns a True" 
 else
     echo "The $my_variable expression returns a False" 
 fi 

执行结果:

 

 变量my_variable中包含有内容(Full),因此当test命令测试条件时,返回的退出状态为0。这使得then语句块中的语句得以执行。如果该变量中没有包含内容,就会出现相反的情况:

$ vim test6.sh
#!/bin/bash
# Testing the test command
 my_variable=""
 if test $my_variable 
 then
     echo "The $my_variable expression returns a True"
 else
     echo "The $my_variable expression returns a False" 
 fi

执行结果:

 

 bash shell提供了另一种条件测试方法,无需在if-then语句中声明test命令:

if [ condition ] 
then
    commands 
fi

方括号定义了测试条件。注意,第一个方括号之后和第二个方括号之前必须加上一个空格,否则就会报错。 test命令可以判断三类条件:

  • 数值比较
  • 字符串比较
  • 文件比较

4.1数值比较

比  较

描  述

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

举例:

$ vim numeric_test.sh 
#!/bin/bash 
# Using numeric test evaluations 
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 equal" 
 else
      echo "The values are different" 
 fi 

第一个条件测试:

if [ $value1 -gt 5 ] : 测试变量value1的值是否大于5。

第二个条件测试:

if [ $value1 -eq $value2 ] : 测试变量value1的值是否和变量value2的值相等

结果:

 

 但是涉及浮点值时,数值条件测试会有一个限制:

$ vim floating_point_test.sh 
#!/bin/bash 
# Using floating point numbers in test evaluations 
 value1=5.555 
 echo "The test value is $value1" 
 if [ $value1 -gt 5 ] 
 then
      echo "The test value $value1 is greater than 5" 
 fi 

结果:

 

 bash shell只能处理整数。

4.2字符串比较

比  较

描  述

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命令所采用的不同。

第一个问题:

$ vim badtest.sh 
#!/bin/bash 
# mis-using string comparisons 
 val1=baseball 
 val2=hockey 
if [ $val1 > $val2 ] 
then
    echo "$val1 is greater than $val2" 
else
    echo "$val1 is less than $val2" 
fi 
结果:
$ ./badtest.sh baseball is greater than hockey $ ls -l hockey -rw-r--r-- 1 rich rich 0 Sep 30 19:08 hockey

这个脚本中只用了大于号,没有出现错误,但结果是错的。脚本把大于号解释成了输出重定向。因此,它创建了一个名为hockey的文件。

要解决这个问题,就需要正确转义大于号:

$ vim test9.sh
#!/bin/bash
# mis-using string comparisons
 val1=baseball 
 val2=hockey
if [ $val1 \> $val2 ] 
then
   echo "$val1 is greater than $val2" 
else
    echo "$val1 is less than $val2" 
fi 

结果:
$ ./test9.sh 
baseball is less than hockey

第二个问题:

$ vim test9b.sh
#!/bin/bash
# testing string sort order 
val1=Testing 
val2=testing
if [ $val1 \> $val2 ] 
then
    echo "$val1 is greater than $val2" 
else
    echo "$val1 is less than $val2" 
fi

结果:
$ ./test9b.sh
Testing is less than testing
$ 
$ sort testfile
 testing
 Testing

在比较测试中,大写字母被认为是小于小写字母的。但sort命令恰好相反。当你将同样的字符串放进文件中并用sort命令排序时,小写字母会先出现。这是由各个命令使用的排序技术不同造成的。

比较测试中使用的是标准的ASCII顺序,根据每个字符的ASCII数值来决定排序结果。sort 命令使用的是系统的本地化语言设置中定义的排序顺序。对于英语,本地化设置指定了在排序顺序中小写字母出现在大写字母前。

4.3文件比较

比  较

描  述

-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旧

举一个例子:

$ vim test13.sh
#!/bin/bash
item_name=$HOME 
echo 
echo "The item being checked: $item_name" 
echo 
if [ -e $item_name ] 
then  
    echo "The item, $item_name, does exist."
   echo "But is it a file?"    
   echo    
if [ -f $item_name ]    
then 
       echo "Yes, $item_name is a file."
 else
      echo "No, $item_name is not a file."    
fi 
else
      echo "The item, $item_name, does not exist."
      echo "Nothing to update" 
fi

5复合条件测试

if-then语句允许你使用布尔逻辑来组合测试。有两种布尔运算符可用:

  • [ condition1 ] && [ condition2 ] :要让then部分的命令执行,两个条件都必须满足。
  • [ condition1 ] || [ condition2 ] :如果任意条件为TRUE,then部分的命令就会执行。

下例展示了AND布尔运算符的使用。

$ vim test22.sh
#!/bin/bash
if [ -d $HOME ] && [ -w $HOME/testing ] 
then
    echo "The file exists and you can write to it" 
else
    echo "I cannot write to the file" 
fi 

结果:
$ ./test22.sh
I cannot write to the file
$
$ touch $HOME/testing
$ 
$ ./test22.sh
The file exists and you can write to it

if-then的高级特性

6.1使用双括号

双括号命令允许你在比较过程中使用高级数学表达式,见下表:

符  号

描  述

val++

后增

val--

后减

++val

先增

--val

先减

!

逻辑求反

~

位求反

**

幂运算

<< 

左位移

>> 

右位移

&

位布尔和

|

位布尔或

&&

逻辑和

||

逻辑或

举例:

$ vim test23.sh
#!/bin/bash
# using double parenthesis
val1=10 
if (( $val1 ** 2 > 90 )) 
then
   (( val2 = $val1 ** 2 ))
   echo "The square of $val1 is $val2" 
fi 

执行:
$ ./test23.sh
The square of 10 is 100

注意,不需要将双括号中表达式里的大于号转义。这是双括号命令提供的另一个高级特性。

6.2使用双方括号

双方括号命令提供了针对字符串比较的高级特性。双方括号命令的格式如下:

[[ expression ]]

它提供了test命令未提供的另一个特性——模式匹配(就是支持正则表达式)。

举例:

$ vim test24.sh
#!/bin/bash
# using pattern matching
if [[ $USER == r* ]] 
then
    echo "Hello $USER" 
 else
    echo "Sorry, I do not know you" 
fi 

执行结果:
$ ./test24.sh
Hello rich

 

7case命令

格式:

case variable in 
pattern1 | pattern2) commands1;; 
pattern3) commands2;; 
*) default commands;; 
esac

如果有一个这样的if-then-else语句:

$ vim test25.sh 
#!/bin/bash 
if [ $USER = "rich" ] 
then 
   echo "Welcome $USER"    
   echo "Please enjoy your visit" 
elif [ $USER = "barbara" ] 
then
    echo "Welcome $USER"    
    echo "Please enjoy your visit" 
elif [ $USER = "testing" ] 
then
    echo "Special testing account" 
elif [ $USER = "jessica" ] 
then
    echo "Do not forget to logout when you're done"     
else
    echo "Sorry, you are not allowed here" 
fi 

我们可以使用case语句变成这样,让它看起来更简便:

$ vim test26.sh
#!/bin/bash
# using the case command
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

猜你喜欢

转载自www.cnblogs.com/ericz2j/p/12045534.html