天天看点

Bash shell 编程之基础知识概述 (if语句)

一、Bash shell是什么

shell是什么,Bash与shell又有什么关系。(以前我也不是特别清楚~~~~)

shell 是一个交互性命令解释器。shell独立于操作系统,这种设计让用户可以灵活选择适合自己的shell。shell让你在命令行键入命令,经过shell解释后传送给操作系统(内核)执行。

shell是一个命令处理器(command processor)--是一个读入并解释你输入的命令的程序。除了是一个命令中断器以外,shell还是一个程序设计语言。你可以编写shell可以解释的程序(被称为源程序),这些源程序可以包含shell程序设计命令等等。shell除了解释命令以外,还有其他工作,它也可以配置和编程。  

shell拥有自己的语言允许用户编写程序并以一种复杂方式运行。shell编程语言具有许多常用的编程语言的特征,例如:循环和控制结构等。用户可以生成像其他应用程序一样复杂的shell程序。

以下是shell功能的一个汇总:

查找命令的位置并且执行相关联的程序;

为shell变量赋新值;

执行命令替代;

处理 I/O重定向和管道功能;

提供一个解释性的编程语言界面,包括tests、branches和loops等语句。

bash是borne again shell的缩写,它是shell的一种,Linux上默认采用的是bash。当然还有sh,dash,tcsh和ksh等

1、读入变量(read)

read命令是用于从终端或者文件中读取输入的内建命令,read命令读取整行输入,每行末尾的换行符不被读入。在read命令后面,如果没有指定变量名,读取的数据将被自动赋值给特定的变量REPLY。下面的列表给出了read命令的常用方式:

read

从标准输入中读取一行并赋值给特定的变量ERPLY(没有指定变量名的话)

read one

从标准输入读取并赋值给变量one

read first last

从标准输入读取输入到第一个空格或者回车,将输入的第一个单词放到变量first中,并将该行其他的输入放在变量last中。

read -p

打印提示信息,等待输入,并赋值给默认的变量REPLY

read -t

设置超时时间(单位:秒)

read -a arry_name

将输入在清单存入到"arry_name"数组中

read示例:

1

2

3

4

5

<code>#无变量,默认存于特定变量REPLY中</code>

<code>[root@localhost apache]</code><code># read    #等待控制台输入,并将结果赋值给特定内置变量REPLY。</code>

<code>hello           </code><code>#控制台输入Hello</code>

<code>[root@localhost apache]</code><code># echo $REPLY  #打印变量</code>

<code>hello</code>

<code>[root@localhost apache]</code><code># read one two three</code>

<code>3 9 8  </code><code>#输入1 2 3,它们之间用空格隔开。</code>

<code>[root@localhost apache]</code><code># echo "one is:$one,two is:$two three is:$three"</code>

<code>#打印结果</code>

<code>one is:3,two is:9 three is:8</code>

<code>[root@localhost apache]</code><code># read -p "Please input your name:" name</code>

<code>#输出"Please input your name"文本提示,同时等待输入,并将结果赋值给name</code>

<code>Please input your name:scott</code>

<code>[root@localhost apache]</code><code># echo "$name"</code>

<code>scott</code>

<code>[root@localhost apache]</code><code># read -t 4  -p "Enter your nmber:"</code>

<code>#限时4秒输入,如果过了4秒,将退出不再输入</code>

<code>Enter your nmber:[root@localhost apache]</code><code># echo $REPLY</code>

<code>#结果为空</code>

2.  状态判断:

test是Shell中提供的内置命令,主要用于状态的检验,如果结果为0,表示成功,否则表示失败。

<code>[root@localhost apache]</code><code># name=scott</code>

<code>[root@localhost apache]</code><code># test $name != scoot</code>

<code>[root@localhost apache]</code><code># echo $? #测试上一条命令执行状态的返回值,0表示功</code>

<code>0</code>

注意的是test命令不支持Shell中提供的各种通配符

<code>[root@localhost apache]</code><code># name=tom</code>

<code>[root@localhost apache]</code><code># test $name = [Tt]om</code>

<code>[root@localhost apache]</code><code># echo $?</code>

<code>1</code>

test命令还可以中括号予以替换,其语义保持不变

<code>[root@localhost apache]</code><code># [ "$name" = tom  ]</code>

在Shell中还提供了另外一种用于状态判断的方式:` expr `,和test不同的是,该方式中的表达式支持通配符

<code>[root@localhost apache]# [[ $name==[tT]om ]]</code>

<code>[root@localhost apache]# echo $?</code>

在` expression `中,expression可以包含&amp;&amp;(逻辑与)和||(逻辑或)。

<code>[root@localhost apache]</code><code># friend=Jack</code>

<code>[root@localhost apache]</code><code># [[ $name==[tT]om &amp;&amp; $friend == "Jack" ]]</code>

在Shell中还提供了let命令的判断方式: (( expr ))

6

7

8

<code>[root@localhost apache]</code><code># a=45</code>

<code>[root@localhost apache]</code><code># b=34</code>

<code>[root@localhost apache]</code><code># (( a &gt; b ))</code>

<code>[root@localhost apache]</code><code># (( a == 45 &amp;&amp; b == 34 ))</code>

下面的表格是test命令支持的操作符:

表达式

判断为真的条件

字符串判断

结果

[ StringA=String ]

StringA等于StringB

[ StringA==StringB ]

[ StringA!=StringB ]

StringA不等于StringB

[ String ]

String不为空

[ -z String ]

String长度为0

[ -n String ]

String长度不为0

逻辑判断

[ StringA -a StringB ]

StringA和StringB都是真

[ StringA -o StringB ]

StringA或StringB是真

[ !String ]

String不为真

逻辑判断(复合判断)

[[ pattern1 &amp;&amp; pattern2 ]]

pattern1和pattern2都是真

[[ pattern1 || pattern2 ]]

pattern1或pattern2是真

[[ !pattern ]]

pattern不为真

整数判断

[ intA -eq intB ]

intA等于intB

[ intA -ne intB ]

intA不等于intB

[ intA -ge intB ]

intA大于等于intB

[ intA -lt intB ]

intA小于intB

[ intA -le intB ]

intA小于等于intB

[ intA -gt intB ]

intA大于intB

文件判断中的二进制操作

[ fileA -ot fileB ]

fileA比fileB旧

[ fileA -ef fileB ]

fileA和fileB有相同的设备或者inode值

[ fileA -nt fileB ]

fileA比fileB新

文件检验

[ -d $file ] or [[ -d $file ]]

file为目录且存在时为真

[ -e $file ] or [[ -e $file ]]

file为文件且存在时为真

[ -f $file ] or [[ -f $file ]]

file为非目录普通文件存在时为真

[ -s $file ] or [[ -s $file ]]

file文件存在, 且长度不为0时为真

[ -L $file ] or [[ -L $file ]]

file为链接符且存在时为真

[ -r $file ] or [[ -r $file ]]

file文件存在且可读时为真

[ -w $file ] or [[ -w $file ]]

file文件存在且可写时为真

[ -x $file ] or [[ -x $file ]]

file文件存在且可执行时为真

[ -S $file ] or [[ -S $file ]]

测试文件是否存在在并且是否是一个套接字文件

[ -h $file ] or [[ -h $file ]]

[ -p $file ] or [[ -p $file ]]

测试文件是否存在并且是否是一个管道文件

注:在逻辑判断(复合判断中),pattern可以包含元字符,在字符串的判断中,pattern2必须被包含在引号中。

3.流程控制语句:

if语句格式如下:

if语句的后面是Shell命令,如果该命令执行成功返回0,则执行then后面的命令。

if command;then

       command

 fi

用test命令测试其后面expression的结果,如果为真,则执行then后面的命令。

   if test expression

   then

   fi

下面的格式和test expression等同

   if [ string/numeric expression ]

下面的两种格式也可以用于判断语句的条件表达式,而且它们也是目前比较常用的两种。

   if ` string expression `

       .........

   if (( numeric expression ))           

        .......

示例:

9

10

11

<code>[root@localhost tmp]</code><code># cat test2.sh</code>

<code>#!/bin/bash</code>

<code>read</code> <code>-p  </code><code>"Are you OK(y/n)?"</code> <code>answer</code>

<code>#这里的$answer变量必须要用双引号扩住,否则判断将失败</code>

<code>   </code><code>if</code> <code>[ </code><code>"$answer"</code> <code>= y -o </code><code>"$answer"</code> <code>= Y ]</code>

<code>    </code><code>then</code>

<code>        </code><code>echo</code> <code>"Glad to see it."</code>

<code>    </code><code>fi</code>

<code>[root@localhost tmp]</code><code># bash test2.sh</code>

<code>Are you OK(y</code><code>/n</code><code>)?y</code>

<code>Glad to see it.</code>

上面的判断还可以替换为:

<code>read</code>  <code>-p </code><code>"Are you OK(y/n or Maybe)?"</code> <code>answer</code>

<code>#` `复合命令操作符允许其中的表达式包含元字符,这里输入以y或Y开头的任意单词,或Maybe都执行then后面的echo。</code>

<code>if</code> <code>[[ $answer == [yY]* || $answer = Maybe ]];</code><code>then</code>

<code>        </code><code>echo</code> <code>"Glad to hear it."</code>

<code>Are you OK(y</code><code>/n</code> <code>or Maybe)?yadfadsf</code>

<code>Glad to hear it.</code>

<code>[root@localhost tmp]</code><code># cat test3.sh</code>

<code> </code><code>answer=</code><code>"not really"</code>

<code>    </code><code>if</code> <code>[[ $answer = [Nn]o?( way |t really) ]]</code>

<code>     </code><code>then</code>

<code>        </code><code>echo</code> <code>"I am sorry."</code>

<code>     </code><code>fi</code>

<code>[root@localhost tmp]</code><code># bash -n test3.sh</code>

<code>[root@localhost tmp]</code><code># bash test3.sh</code>

<code>I am sorry.</code>

<code>#对于本示例中的扩展通配符,这里需要给出一个具体的解释。[Nn]o匹配No或no,?( way|t really)则表示0个或1个( way或t really),因此answer变量匹配的字符串为No、no、Not really、not really、No way、no way。</code>

if/elif/else语句的使用方式和if语句极为相似,其格式如下:

   if command

   elif command

   else

12

13

14

15

16

17

18

19

20

21

22

23

<code>[root@localhost tmp]</code><code># cat test4.sh</code>

<code> </code><code>read</code>  <code>-p </code><code>"How old are you?"</code> <code>age</code>

<code>    </code><code>if</code> <code>[ $age -lt 0 -o $age -gt 120 ]                </code><code>#(( age &lt; 0 || age &gt; 120 ))</code>

<code>        </code><code>echo</code> <code>"You are so old."</code>

<code>    </code><code>elif</code> <code>[ $age -</code><code>ge</code> <code>0 -a $age -</code><code>le</code> <code>12 ]               </code><code>#(( age &gt;= 0 &amp;&amp; age &lt;= 12 ))</code>

<code>        </code><code>echo</code> <code>"You are child."</code>

<code>    </code><code>elif</code> <code>[ $age -</code><code>ge</code> <code>13 -a $age -</code><code>le</code> <code>19 ]            </code><code># (( age &gt;= 13 &amp;&amp; age &lt;= 19 ))</code>

<code>        </code><code>echo</code> <code>"You are 13--19 years old."</code>

<code>    </code><code>elif</code> <code>[ $age -</code><code>ge</code> <code>20 -a $age -</code><code>le</code> <code>29 ]           </code><code># ((age &gt;= 20 &amp;&amp; age &lt;= 29 ))</code>

<code>        </code><code>echo</code> <code>"You are 20--29 years old."</code>

<code>    </code><code>elif</code> <code>[ $age -</code><code>ge</code> <code>30 -a $age -</code><code>le</code> <code>39 ]             </code><code># (( age &gt;= 30   &amp;&amp; age &lt;= 39 ))</code>

<code>        </code><code>echo</code> <code>"You are 30--39 years old."</code>

<code>    </code><code>else</code>

<code>        </code><code>echo</code> <code>"You are above 40."</code>

<code>[root@localhost tmp]</code><code># bash test4.sh</code>

<code>How old are you?60</code>

<code>You are above 40.</code>

=======================================完================================================

本文转自 jinlinger 51CTO博客,原文链接:http://blog.51cto.com/essun/1362838,如需转载请自行联系原作者