Linux程序设计(Linux shell编程五)

各位看官上一回咱们说到shell编程中的test命令,当时只是开了个头,简单说了说。今天咱们特意给它

开一个专场,讨论如何使用它。闲话休提,专场正式开始。


test命令经常用在判断结构或者循环结构的条件中,不过使用的是它的替身:[]。判断结构和循环结构依

据test命令的返回结果来执行结构中其它的程序块。和test命令一起配合使用的结构有if系列的判断结

构,while和until系列的循环结构。test命令有3大绝招:比较数字,比较字符串,比较文件。接下来

咱们 一一说明这3个绝招。


绝招1:比较数字

比较数字使用eq(等于),ge(大于等于),gt(大于),le(小于等于),lt(小于),ne(不等于)这些选项.为了方便大家

理解,我在括号里使用汉字解释选项的意思。 “这么多选项,哪能记的住呢,这test也太难使用了吧“。

看官莫急。我来梳理一下,就容易记住了。大家还记得以前学过的英语中equal表示等于,大于或者

小于用greater/less than表示吧,知道这些就够了。eq是equal的缩写,所以它在命令中就表示等于。

ne是not equal的缩写,因此用它表示不等于。gt/lt是greater/less than的缩写,所以用它来表示大于

或者小于。ge/le是greater/less than or equal的缩写,因此用它来表示大于等于或者小于等于。各位

看官们,我这么说,大家记忆起来就容易多了吧。如果还是记不住,那么也不用担心。找男人问问就

知道:man test。“举个例子吧。”哪是必须的。

  #! /bin/bash
   echo "-----------------the starting line of shell-----------------"

   a=0
   b=0

   echo "please input the number for compare"
   echo "such as: input 3 5,and then push Enter Key"

  read a b

  if [ $a -eq $b ]
  then
  >-------echo "$a is equal to $b"
  elif [ $a -ne $b ]
  then
 >-------echo "$a is not equal to $b"
  >-------if [ $a -gt $b ]
  >-------then                                                 
  >------->-------echo "$a is greater than $b"
 >-------else
    >------->-------echo "$a is less than $b"
 >-------fi
 else
 >-------echo "please input correct number!"
  fi
 
 echo "-----------------the ending line of shell-----------------"

新建立一个名叫t1.sh的脚本文件,把上面的内容输入到文件中,保存后,给文件加上执行权限,然后在

终端中运行该文件,并用依据程序提示输入内容得到以下结果(输入不同的内容,得到不同的结果):

-----------------the starting line of shell-----------------

please input the number for compare

such as: input 3 5,and then push Enter Key

3 4

3 is not equal to 4

3 is less than 4

-----------------the ending line of shell-----------------

-----------------the starting line of shell-----------------

please input the number for compare

such as: input 3 5,and then push Enter Key

3 4

3 is not equal to 4

3 is less than 4

-----------------the ending line of shell-----------------

-----------------the starting line of shell-----------------

please input the number for compare

such as input 3 5,and then push Enter Key

a b

./sh.sh: line 12: [: a: integer expression expected

./sh.sh: line 15: [: a: integer expression expected

please input correct number

-----------------the ending line of shell-----------------


绝招2:比较字符串

在介绍比较字符串这个绝招前,咱们先说说字符串的大小。看官们应该听说过ASCII码吧,它把每个字符

都进行了编码。字符串的大小,实际是这些编码的大小。看官们,咱们先看看ASCII码表,打开终端,在

终端中输入:man ascii。大家可以看到字母:A的ASCII码值是65,字母:a的ASCII码值是97.97显然比65

大,所以我们说a比A大。而比较字符串的大小呢就是依次比较字符串中每个字母ASCII码值的大小。比如

red和read。前两个字母一样,分不出大小来,接着比较第3个字母,d的ASCII码值为100,a的ASCII码

值为97.100显然大于97.所以我们说red比如read大。


比较字符串使用=,!=,<,>这些数学中的符号,我相信这些符号对大家来说很容易理解,即使我不说它们 的意

思,大家也都能知道。另外还有2个特殊的比较符号,n用来检测字符串长度是否为非0,z表示用来检测字符

串长度是否为0。在shell编程中,空或者未初始化的变量会在程序运行时产生逻辑错误,因此在使用数字

类型变量,或者字符串变量前最好判断它们的值是否为空。这时候n和z就派上用场了。


这时台下 的看官说话了“这个比较字符串也太容易了,像>,<,=这些符号真是再再熟悉不过了,这个绝招不用

说了,说下一个绝招吧”。看官莫急。这绝招看似容易,实际上容易出错呀。来看个例子就知道了。

 #! /bin/bash
   echo "-----------------the starting line of shell-----------------"

   a=0
   b=0

   echo "please input the string for compare"
   echo "such as: input apple, orange ,and then push Enter Key"

  read a b

  echo "a=$a, b=$b"
  if [ $a = $b ]
  then
  >-------echo "$a is equal to $b"
  else
 >-------echo "$a is not equal to $b"
 >-------if [ $a > $b ]-
 >-------then
 >------->-------echo "$a is greater than $b"
>-------else
 >------->-------echo "$a is less than $b"
 >-------fi
 fi                                                                                                  
  echo "-----------------the ending line of shell-----------------"

新建立一个名叫t2.sh的脚本文件,把上面的内容输入到文件中,保存后,给文件加上执行权限,然后在

终端中运行该文件,并用依据程序提示输入内容得到以下结果(输入不同的内容,得到不同的结果):

-----------------the starting line of shell-----------------

please input the string for compare

such as: input apple, orange ,and then push Enter Key

a b

a=a, b=b

a is not equal to b

a is greater than b

-----------------the ending line of shell-----------------


-----------------the starting line of shell-----------------

please input the string for compare

such as: input apple, orange ,and then push Enter Key

read red

a=read, b=red

read is not equal to red

read is greater than red

-----------------the ending line of shell-----------------


-----------------the starting line of shell-----------------

please input the string for compare

such as: input apple, orange ,and then push Enter Key

apple orange

a=apple, b=orange

apple is not equal to orange

apple is greater than orange

-----------------the ending line of shell-----------------

看官们,仔细看一下程序运行的结果,a比b大,read比red大,这和咱们刚才分析的结果不一样呀。难道

咱们分析有问题?”哪个苹果(apple)比桔子(orange)也大“,有位看官边说,边流口水。这位看官呀,大

就大呗,怎么口水也流出来了,你这是想吃苹果呢还是想吃桔子,自己用if写个判断结构验证一下。台下

一片大笑。看官们回到主题上来,咱们刚才的分析是正确的,只是这程序运行的结果不对,这说明咱们的

程序中有错误。谁能找出程序中的错误,就奖励一个苹果。看官们都在拼命的找,特别是刚才流口水的哪

位看官,更加卖力,看来他是想吃苹果呀。看官们找了半天也没有找到,有些不高兴。这时只听醒木啪的

一声响。大家安静了下来。看官们,这个字符串比较大小时使用>和<符号没有错误,只是使用时需要在它

们前面加上转义字符\,不然会被当作管道符号。这是一个陷阱,特别容易出错。而且这种错误比较隐蔽,

它虽然没有语法错误,但是在运行时得到的是错误结果。看官们,于是在大于号前面加了一个转义符号。

这时再运行程序时,得到了正确的结果。另外,还有一种修改错误的方法,就是使用(( ))替代[ ],这样的话

就不需要使用转义符号了。看官们,通过这个例子,我们可以看到,一个看似简单的绝招(比较字符串),

其实使用的时候却容易出错。所以学习绝招时不能大意呀。


绝招3:比较文件

比较文件时也是使用字符。列举几个经常使用的符号及其意义。e检查文件或者目录是否存在,d检查对象

是否是一个目录,并且该目录存在。f检查对象是否是一个文件,并且文件存在。s检查文件的内容是否为

空。rwx分别用来检查文件是否存在,并且有读写和执行的权限。nt判断哪个文件新,no与nt相反,用来

判断哪个文件旧。还和以前一样,咱们举例子来说明。

 #! /bin/bash

   echo "-----------------the starting line of shell-----------------"

   dir="/etc"
   file="passwd"

   if [ -d $dir ]
   then
   >-------echo "$dir is a directiry "

  >-------cd $dir
  >-------if [ -f $file ]                                    
 >-------then
 >------->-------echo "$file is a file"
  >-------else
  >------->-------echo "$file is a file"
 >-------fi
 else
 >-------echo "$dir is not  a directiry "
 fi

  echo "-----------------the ending line of shell-----------------"


新建立一个名叫t3.sh的脚本文件,把上面的内容输入到文件中,保存后,给文件加上执行权限,然后在

终端中运行该文件,并用依据程序提示输入内容得到以下结果:

-----------------the starting line of shell-----------------

/etc is a directiry

passwd is a file

-----------------the ending line of shell-----------------


看官们关于test命令专场到此结束。欲知后事如何,且听下回分解。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talk_8

真诚赞赏,手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值