since2012/04/23

学习 shell scripts
本文以不再维护,新文章请参阅 此处
最近更新日期:2002/06/27
干嘛学习 Scripts
scripts 的执行与第一支 scripts
卷标与运算符号declare
对谈式 scriptsread
scripts 逻辑判断式与表达式
条件式判断if...then...fi, case.....esac
循环for....do....done, while...do...done, until...do...done,
script 如何 debug
本章习题练习

干嘛学习 Scripts
这个问题可有趣的很了,我为什么要学习 scripts 呢?不要学可不可以呀!?呵呵!如果您只想要『会用』 Linux 就好的话,那么这一个部分确实可以先跳过去不看也没关系,不过,如果您想要更加的了解与控制 Linux ,使 Linux 运作更顺畅之外,还可以高枕无忧的让你的 Linux Server 在 Internet 上面提供相关服务的话,那么 scripts 还是多少学一学吧!为什么呢?因为 scripts 可以设计到『很聪明的知道什么条件之下需要进行什么动作!』不要小看这个功能呦!当您不在计算机前面的时候,突然之间,主机被一些不明封包试图入侵了,这个时候如果你的 Linux 主机可以透过『当该封包尝试几次还是联机失败之后,就予以抵挡住该 IP 』,如果可以设计到如此功能的话,呵呵!那么可就不得了了!您的 Linux 主机就可以说是『好聪明呀! smart!』

除了针对主机之外,其实 scripts 还有相当多的运用功能呦!例如您想要在 Linux 上面作你的作业,而这个作业是一些数值方面的计算,这个时候 Scripts 也可以帮您计算呦!还不需要用到 fortran, c 这类高阶的程序语言呢! scripts 本身就是一个可以用的 program 啰!相当的棒吧!其实, scripts 最开始被新手使用的功能就是『汇整一些在 command line 下达的连续指令,将他写入 scripts 当中,而由直接执行 scripts 来启动一连串的 command line 指令输出入!』例如: 防火墙连续规则 ( iptables ),开机加载程序的项目 ( 就是在 /etc/rc.d/rc.local 里头的资料 ) ,等等都是相似的功能啦! 其实,说穿了,如果不考虑 program 的部分,那么 scripts 也可以想成,仅是帮我们把一大串的指令汇整在一个档案里面,而直接执行该档案就可以执行那一串又臭又长的指令段!就是这么简单啦!

另外的另外,在编写 scripts 时,在 Linux 当中,最常使用的就是 vi 这一套文书编辑器了,所以啰,为了更简易的控管我们的 Linux ,嗯!还是学习一下好了啦!好不好呀! ^_^


scripts 的执行与第一支 scripts
嗯!在上一章 bash shell 当中说了一堆变数啦!管线指令啦!等等的,都是为了接着而来的 scripts 的咚咚啦!什么是 script 啊?由字面上的意思来说, script 就是『脚本、剧本』的意思,那够清楚了吧!就是将我们要执行的内容写成一个『脚本』,让系统依据这个『脚本』来执行我们想要的东西!好了!现在我们来讨论一下怎么写 scripts 好了!基本上,一个 script 被执行的时候, bash 会据以判断执行的步骤为:
  1. 如果读取到一个 Enter 符号( CR ),就尝试开始执行该行命令;
  2. 如同前面 bash command 提到的,指令间的多个空白会被忽略掉;
  3. 而空白行也将被忽略掉!,并且 tab 也是不会被理会的!
  4. 至于如果一行的内容太多,则可以使用 \ 来延伸至下一行;
  5. 此外,使用最多的 # 可做为批注!任何加在 # 后面的字,将全部被视为批注文字而被忽略!
然后,在撰写一个 scripts 的时候,最好养成良好的习惯:
  1. 先宣告使用的 shell 为何?(特别留意这一点,在某些状况中,例如 /etc/crontab 情况下,如果没有宣告使用的 shell ,常常会出现错误讯息而导致 scripts 无法被执行呦!)
  2. 注明该 script 的内容功能、版本信息、作者、建文件日期等等
  3. 每一个大步骤的主要功能(也顺便提供自己未来修改之用!)
那么 scripts 这个档案要如何来执行呢?执行的方法有两种:
  • 一个是将该档案改成可以执行的属性,如chmod 755 scripts.file ,然后执行该档案;
  • 另一种则是直接以 sh 这个执行档来执行 script 的内容,如 sh scripts.file
大致上就是这样啦!OK!那么还记得我们在前面一章已经说过了变量的设定方式了吧?好了,那么我们就以第一支 scripts 来将我们设定的变量直接给他 show 出来!嗯!来设计一下这支 script 吧!
好了,我们来建立第一支简单的 script 吧!最简单的一个例子,就是在屏幕上列出『 Hello ! How are you ?』,可以这样写:(注:最常使用来作为 shell scripts 的写作的软件,就是 vi 啦!有空真的要多熟悉一下 vi 呦!)
 
[root @test /root]# mkdir test; cd test  <==建立一个新的目录,所有的 scripts 都暂存在此!
[root @test test]# vi test01-hello.sh
#!/bin/bash                        <==在 # 之后加上 ! 与 shell 的名称,用来宣告使用的 shell
# 这个脚本的用途在于列出 Hello ! How are you 在屏幕上
# 建檔日期: 2002/05/20
# Made by VBird
hello=Hello\ \!\ How\ are\ you\ \?      <==这就是变量啦!
echo $hello

[root @test test]# sh test01-hello.sh
Hello ! How are you ?                   <=输出的结果显示在屏幕上!

这里给他注意一下:

  • 所有在 scripts 里面的东西,基本规则 ( 如变量设定规则 ) 需要与 command line 时相同;
  • scripts 的附档名最好为 .sh 提供他人的认识;
  • 并非加上 .sh 就可以是执行档,还需要查看其属性中是否有 x 这个属性。
呵呵!很兴奋吧!你已经会写 scripts 了呦!已经具有初阶的程序设计能力啰!嗯!是该觉得高兴的!^_^...!好了,接着下来我们写稍微复杂一点的,若一个 script 里头有两个以上的变数要相互引用呢?呵呵!这个时候顺便来比较一下 " 与 ' 的异同吧!
 
[root @test test]# vi test02-2var.sh
#!/bin/bash 
# 这个脚本用途在于引用两个变量,顺便比较一下 " 与 ' 的异同
# Date: 2002/06/27
# Made by VBird
name="V.Bird"
myname1="My name is $name"
myname2='My name is $name'
echo $name
echo $myname1
echo $myname2

[root @test test]# sh test02-2var.sh
V.Bird
My name is V.Bird
My name is $name

看到输出的结果了吧!呵呵!没错!那个 " 与 ' 最大的不同就是在于能不能保有『变量内容』啦!再提醒一次,那个单引号『 ' 』里头的数据都将变成『单纯的字符』而不是有特殊的字体呦!


卷标与运算符号:declare

  • declare

  • 宣告变量内容
    语法
    [test @test test]# declare [-afirx]
    参数说明:
    -a  :定义为数组 array
    -f  :定义为函数 function 
    -i  :定义为整数 integer
    -r  :定义为『只读』
    -x  :定义为透过环境输出变量
    范例:
    [test @test test]# declare -i a=3
    [test @test test]# declare -i b=5
    [test @test test]# declare -i c=$a*$b
    [test @test test]# echo $c
    15  <==变成数字啰! ^_^
初步了解了吧!?好了,现在我们来玩玩看,如果您的计算结果当中,需要输入为 2*3+5*13-32+25 时,并且在最后输出『 Your result is ==> 』该怎样写这一支简单的 script 呢?可以这样试试看:
 
[root @test test]# vi test03-declare.sh
#!/bin/bash
# This program is used to "declare" variables
# VBird 2002/06/27
number1=2*3+5*13-32+25
declare -i number2=2*3+5*13-32+25
echo "Your result is ==> $number1"
echo "Your result is ==> $number2"
[root @test test]# sh test03-declare.sh
Your result is ==> 2*3+5*13-32+25
Your result is ==> 64

怎样?很有趣吧!更好玩的还在后头呦!再往下看一下吧!


对谈式 scripts :
什么是对谈式的 scripts 呢?很简单啦!例如你在执行 Windows 的安装程序时,系统不是常常会跳出一个窗口,问你『下一步』、『上一步』或『取消』吗?那就是对谈啦!程序会依据您输入的数据来进行判断,OK!那么最简单的对谈式指令是什么呢?呵呵!就是 read 这个指令啦! read 的功能就是『依据您在键盘输入的结果 input 到变量内容中』,例如:
 
[root @test test]# read name
VBird <==这是键盘输入的结果
[root @test test]# echo $name
VBird

好了!那么我们来设定一下,当您的 script 在执行的时候,将您由键盘输入的数据列出来!如何做呢?
 
[root @test test]# vi test04-read.sh
#!/bin/bash
# This program is used to "read" variables
# VBird 2002/06/27
echo "Please keyin your name, and press Enter to start."
read name
echo "This is your keyin data ==> $name"
[root @test test]# sh test04-read.sh
Please keyin your name, and press Enter to start.
VBird Tsai
This is your keyin data ==> VBird Tsai

就是这么简单,我们后面还会继续的谈到判别式,那个时候输入的数据可就更重要了!

好了!下一步我们再来说一说怎样定义一个 script 的参数的代号!?以底下我们的说明为例:
 
[root @test test]# myscript opt1 opt2 opt3 opt4
          $0    $1  $2  $3  $4

这是什么意思呢?嘿嘿!就是说,在这个 script ( myscript )里面,只要变量名称为 $0 就表示为 myscript 这个咚咚,也就是说:

$0 : myscript 亦即是 script 的檔名
$1 : opt1 亦即是第一个附加的参数 (parameter)
$2 : opt2
$3 : opt3
这样说或许不是很清楚,我们来玩一玩底下这个 script 看看就晓得他的意思啦!
 
[root @test test]# vi test05-0123
#!/bin/bash
# This program will define what is the parameters
# VBird 2002/06/27
echo "This script's name => $0"
echo "parameters $1 $2 $3"
[root @test test]# sh test05-0123 pa1 pa2 pa3
This script's name => test05-0123
parameters pa1 pa2 pa3

这个东西在运用上也是相当的重要的,例如当您要取得 script 的名称时(因为有时候使用者会自行更改文件名),则这个功能变量就相当的重要了!了解乎!?


scripts 逻辑判断式与表达式:
再来的这个东西可就更重要了,尤其在 scripts 当中!那就如『如何判定某个档案或目录,或者是如何判定程序应该朝向那个方向行进』?这个东西需要有比较好一些的逻辑概念的说明才行!底下我们分别说明一下逻辑判断式与表达式之后,再来设定几个小 scripts 试试看不同的用法,以期得到大家的了解啰!

条件式判断:if...then...fi, case.....esac
OK!刚刚我们 建立了第一支 script ,在屏幕上面以 script 来输出问候语!好了,现在我们如果要让 scripts 加上『判断』要如何来工作呢?这就是所谓的条件式判断了!最常使用到的就是『 if ... then .... else if .... then ... end if 』的作法了!这个条件判断的语法为:
 
if [ 条件判断一 ] && (||) [ 条件判断二 ]; then       <== if 是起始的意思,后面可以接若干个判断式,使用 && 或 ||
    执行内容程序
elif [ 条件判断三 ] && (||) [ 条件判断四 ]; then     <==第二段的判断,如果第一段没有符合就来此搜寻条件
    执行第二段内容程序
else                                            <==当前两段都不符合时,就以这段内容来执行!
    执行第三段内容程序
fi                                              <==结束 if then 的条件判断!

上面的意思是这这样的:在中刮号『[]』里面的是条件式,如果是复合式的条件判断(如若A及B则C之类的逻辑判断),那么就需要在两个中刮号之间加上『 && (and)』或者是『 || (or)』这样的逻辑表达式才行!如果是多重选择的话,那么就需要以 elif (optional, 选择性的,若有需要才加上!)来新增另一个条件;如果所有的条件都不适用,则使用 else (optional)来进行最后的执行内容啰!

不过,这里有几个新手常犯的错误,我们需要来加强说明一下:
  1. 在 [ ] 当中,只能有一个判别式;
  2. 在 [ ] 与 [ ] 当中,可以使用 && 或 || 来组织判别式;
  3. 每一个独立的组件之间『都需要有空格键来隔开』!
尤其是最后一点,最容易犯的错啦!好了,我们来进行一个简单的判别式好了!
 
[root @test test]# vi test06-ifthen.sh
#!/bin/bash
# This program is used to study if then
# VBird 2002/06/27
echo "Press y to continue"
read yn
if [ "$yn" = "y" ]; then
        echo "script is running..."
else
        echo "STOP!"
fi
[root @test test]# sh test06-ifthen.sh
Press y to continue
y
script is running...
[root @test test]$ sh test06-ifthen.sh
Press y to continue
n
STOP!

很简单的一个例子吧!当输入为 y 的时候,就给他进行,若非为 y 则不予以进行!但是这里有个问题,就是如果我输入为 Y 时,程序还是停止了!怎么办?!这个时候就需要使用到 || 这个东西啦!我们可以这样做!
 
[root @test test]# cp test06-ifthen.sh test07-ifthen.sh
[root @test test]# vi test07-ifthen.sh
#!/bin/bash
# This program is used to study if then
# VBird 2002/06/27
echo "Press y to continue"
read yn
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
        echo "script is running..."
else
        echo "STOP!"
fi
[root @test test]# sh test07-ifthen.sh
Press y to continue
y
script is running...
[root @test test]$ sh test07-ifthen.sh
Press y to continue
Y
script is running...

好了!那么如果再加上前面提过的: parameter 的选用呢?呵呵!这个也蛮有趣的,我们再来试试看:
 
[root @test test]# vi test08-ifthen.sh
#!/bin/bash
# set parameters in the if then
# 需要加上 hello 这个参数才会显示正确的!
# VBird 2002/06/27
if [ "$1" = "hello" ]; then
        echo "Hello! How are you ?"
elif [ "$1" = "" ]; then
        echo "You MUST input parameters"
else
        echo "The only accept parameter is hello"
fi
[root @test test]# sh test08-ifthen.sh hello
Hello! How are you ?
[root @test test]$ sh test08-ifthen.sh
You MUST input parameters
[root @test test]$ sh test08-ifthen.sh djdkf
The only accept parameter is hello

呵呵!是不是不难呢?玩到这里应该对于 scripts 的认识有一定程度的了解了吧!嗯!好了,底下我们来玩一个大的!假设您已经知道 netstat 与 grep 这两个东西的用法,那么如果要来侦测你的主机上面的 port 是否有开启时,可以使用底下的范例来进行:
 
[test @test test]# vi port.sh                                  <==编辑一个档案为 test1.sh 的 script
#!/bin/bash                                                     <==宣告使用的 shell 类型
# program: Using to study the [if ... then ... fi] program
# Made by:  VBird
# date:  2002/05/20
# content: I will using this program to show your services
# 1. print the program's work in your screen
  echo "Now, the services of your Linux system will be detect!"
  echo "The www, ftp, ssh, and sendmail + pop3 will be detect!"
  echo " "
# 2. www
  www=`netstat -an|grep LISTEN|grep :80`                    <==这个就是变量啦!并使用了管线命令!
  if [ "$www" != "" ]; then                                     <==开始条件的判断啰!
      echo "WWW is running"                                <==若条件成立,那么就打印这一行的内容!
  else
      echo "WWW is NOT running"
  fi
# 3. ftp
  ftp=`netstat -an|grep LISTEN|grep :21`
  if [ "$ftp" != "" ]; then
      echo "FTP is running"
  else
      echo "FTP is NOT running"
  fi
# 4. ssh
  ssh=`netstat -an|grep LISTEN|grep :22`
  if [ "$ssh" != "" ]; then
      echo "SSH is running"
  else
      echo "SSH is NOT running"
  fi
# 5. sendmail + pop3
  smtp=`netstat -an|grep LISTEN|grep :25`
  pop3=`netstat -an|grep LISTEN|grep :110`
  if [ "$smtp" != "" ]   && [ "$pop3" != "" ]; then           <==有两个以上的条件式,就使用 && 或 || 来分隔!
      echo "sendmail is OK!"
  elif [ "$smtp" != "" ] && [ "$pop3"  = "" ]; then
      echo "sendmail have some problem of your pop3"
  elif [ "$smtp"  = "" ] && [ "$pop3" != "" ]; then
      echo "sendmail have some problem of your smtp"
  else
      echo "sendmail is NOT running"
  fi
[test @test test]# sh port.sh                                <==执行看看输出的结果!
Now, the services of your Linux system will be detect!
The www, ftp, ssh, and sendmail + pop3 will be detect!

WWW is running
FTP is running
SSH is running
sendmail is OK!

很简单吧!上面这样就可以将你主机上面的数据进行解析啰!

刚刚的『 if then fi 』的形式是以程序来自行判断,那么如果我已经规划好几个项目要来执行,只要选择执行的『种类方式』就可以正确的执行的话,要怎么做呢?最简单的例子就是我们常常使用到的 /etc/rc.d/init.d/ 里头的 scripts 啰!例如:重新启动 xinetd 是使用:
/etc/rc.d/init.d/xinetd restart
注意啰!那个是 restart 的项目,然后 script 就会自动的去搜寻 restart 项目里面的情况去执行!那个就是 case ... esac 的使用模式啰!有没有注意到,开始是『 case 』结束则是使用 case 的倒写『 esac 』喝!很有趣吧!那么这种格式是怎样呢?
 
case 种类方式(string) in          <==开始阶段,那个种类方式可分成两种类型,通常使用 $1 这一种直接下达类型!
    种类方式一)
       程序执行段
       ;;                     <==种类方式一的结束符号!
    种类方式二)
       程序执行段
       ;;
    *)
       echo "Usage: {种类方式一|种类方式二}"     <==列出可以利用的参数值!
       exit 1
esac                         <==这个 case 的设定结束处!

在种类方式(string)的格式主要有两种:

  1. 直接下达式:就是以『 执行档案 + string 』的方式来执行的(/etc/rc.d/init.d 里头的基本设定方式),则 string 可以直接写成『 $1 』(在执行档案后面直接加入参数的第一个参数!)
  2. 交互式:就是由屏幕输出可能的项目,然后让用户输入,这个通常必须配合『 read variable 』然后 string 则写成『 $variable 』的格式!
同样的,我们建立一个名为 test2.sh 的档案来试做看看。假如我们共可分三段格式来进行实作,分别为 one, two, three ,并假设使用直接下达式,则可以写成:
 
[test @test test]# vi test09-case.sh
#!/bin/bash
# program:      Using case mode
# Made by:      VBird
# date:         2002/05/20
# content:      I will use this program to study the case mode!
# 1. print this program
  echo "This program will print your selection!"

case $1 in                                   <==使用直接下达指令型态!
  one)
        echo "your choice is one"
        ;;
  two)
        echo "your choice is two"
        ;;
  three)
        echo "your choice is three"
        ;;
  *)
        echo "Usage {one|two|three}"       <==列出可以使用的参数(如果使用者下达错误的参数时)
        exit 1
esac
[test @test test]# sh test09-case.sh       <==执行结果!显示没有相对的参数!所以列出可以参数!
This program will print your selection!
Usage {one|two|three}
[test @test test]# sh test09-case.sh three
This program will print your selection!
your choice is three

那么对谈式的 case 又如何呢?嗯!我们利用上面的方式来修改一下内容啰!
 
[root @test test]# vi test10-case.sh
#!/bin/bash
# program:      Using case mode
# Made by:      VBird
# date:         2002/06/27
# content:      I will use this program to study the case mode!
# 1. print this program
echo "Press your select one, two, three"
read number

case $number in
  one)
        echo "your choice is one"
        ;;
  two)
        echo "your choice is two"
        ;;
  three)
        echo "your choice is three"
        ;;
  *)
        echo "Usage {one|two|three}"
        exit 1
esac
[root @test test]# sh test10-case.sh
Press your select one, two, three
two   <=这一行是您输入的呦!
your choice is two

如何!很简单吧!以后对于 /etc/rc.d/init.d/ 里头的 script 就应该看的懂了吧!此外,由于还有所谓的『函数(function)』也就是『先将需要执行的程序段写成一个小区块,而这个小区块则以一个名称来取代之』,简单的方式可以请您参考一下您系统中的 /etc/rc.d/init.d/sendmail !注意看一下 start() 底下的那些个咚咚!你就可以知道什么意思了!由于我们这里并不教你如何写程序(基本上, scripts 就是小程序啦!还好! VBird 以前就有程序的底子!不然还真怕 script 呢!),所以就只好点到为止啰!


循环:for....do....done, while...do...done, until...do...done,
在程序段当中,最常使用到的就是循环了!循环是很重要的一项工具,尤其是具有判断形式的循环,很常被使用来判断一些事项的可行性与否!但是程序怎么知道什么时候应该要停止这个程序呢?呵呵!就需要加入判断啰!好了,最简单的判断式可以是底下几种:
  • for (( 条件一; 条件二; 条件三 ))
  • for variable in variable1 variable2 .....
  • while [ condition1 ] && { || } [ condition2 ] ...
  • until [ condition1 ] && { || } [ condition2 ] ...
for 是已经知道有多少个 run 了,即是已经知道要跑几次了,至于 until 与 while 则分别是:
  • until:直到条件相同的时候才离开程序』;
  • while:当条件相同的时候,就继续做!
这两者不太相同的啦!
好了!我们先来谈一下最简单的循环,就是利用 for 这个东西来进行!好了,假设我们计算 1 + 2 + 3 .... + 100 ,以 script 要如何写呢?有很多的方式,我们来谈一谈 do...done 好了!
 
[test @test test]# vi test11-loop.sh
#!/bin/bash
# Using for and loop
# VBird 2002/06/27
declare -i s  # <==变量宣告
for (( i=1; i<=100; i=i+1 ))
do
        s=s+i
done
echo "The count is ==> $s"

[test @test test]# sh test11-loop.sh
The count is ==> 5050

请注意! for (( 条件一; 条件二; 条件三)) 这是必须要的!

  • 条件一:这可以看成是『初始值』,如上面的例子中,初始值是 i=1 啦!
  • 条件二:这可以看成是『符合值』,如上面的例子中,当 i<=100 的时候都是符合条件的!
  • 条件三:这可以看成是『步阶』!也就是说, i 每次都加一!
所以啦!上面的例子是说:由 i=1 开始到 i<= 100 ,每次 i 都加一来执行底下的程序段(就是 s=s+i ),当 i >100 (也就是 i=101 )就跳出这一段程序段!怎样!不难吧!
好了!那么使用 while 或者是 until 要怎样执行呢?其实都是差不多的情况!我们使用底下的两个 script 来进行 1 ~ 100 的累加动作!
 
1. 使用 while :
[test @test test]# vi test12-loop.sh
#!/bin/bash
# Using while and loop
# VBird 2002/06/27
declare -i i
declare -i s
while [ "$i" != "101" ]
do
        s=s+i
        i=i+1
done
echo "The count is ==> $s"

2. 使用 until :
[test @test test]# vi test13-loop.sh
#!/bin/bash
# Using until and loop
# VBird 2002/06/27
declare -i i
declare -i s
until [ "$i" = "101" ]
do
        s=s+i
        i=i+1
done
echo "The count is ==> $s"

[test @test test]# sh test12-loop.sh
The count is ==> 5050

嘻嘻嘻嘻!很简单吧!

第二个例子来自于网友 jony 的提供!这是另外一种循环的方式,可以用来判断非数字的类型呦!
 
[test @test test]# vi test14-for.sh
#!/bin/bash
# using for...do ....done
# VBird 2002/06/27

LIST="Tomy Jony Mary Geoge"

for i in $LIST
do
        echo $i
done

[test @test test]# sh test5.sh
Tomy
Jony
Mary
Geoge

这一种格式是以空格键当作 i 这个变量的选择项目!也就是说,上面的 $LIST 这个变量当中,以空格键来分隔的时候,共可以分离出来四个!所以啰!当以 do ..... done ... 就可以分别写出四个咚咚啦!好啦!那么有没有办法利用这个东西来将你的 Linux 主机上的账号 ( account ) 印出来呢?!很简单呀!我们利用 cut 跟 sort 以及 /etc/passwd 这个档案来完成这一支 script ,作法如下啰:
 
[test @test test]# vi test15-for.sh
#!/bin/bash
# Using for and loop to read the account of this linux server!
# VBird 2002/06/27
account=`cut -d ":" -f1 /etc/passwd|sort`
echo "The following is your linux server's account"

for i in $account
do
        echo $i
done

[test @test test]# sh test15-for.sh
The following is your linux server's account
adm
aerosol
alenchan
amanda
apache
....

OK!再来,我们来使用一下对谈式的循环作用吧!嗯!当我们输入 y 或 Y 时,程序就予以结束!该怎样做?!
 
[test @test test]# vi test16-loop.sh
#!/bin/bash
# Using until
# VBird 2002/06/27

echo "Press Y/y to stop"
until [ "$yn" = "Y" ] || [ "$yn" = "y" ]
do
        read yn
done
echo "Stop here"

[test @test test]# sh test16-for.sh
Press Y/y to stop

GDSG
A
Y
Stop here

上面说的是,当输入 Y 或者是 y 时才跳出 do...done 的循环之中!而去执行底下的东西!哈哈!很好玩吧!嗯!接着下来,我们来判断一下目录是否存在好了!这是常用的呦!

接下来我们判别一下所谓的『逻辑判断式』的使用方式啦!刚刚我们不是已经知道了吗,我们可以使用条件判断来断定到底有没有档案(用 -e )或者是该名称是属于目录或者是档案( -d -f ),接下来我们来判断一个流程好了:
  1. 先查看一下 /root/test/logical 这个名称是否存在;
  2. 若不存在,则建立一个档案,使用 touch 来建立,建立完成后离开;
  3. 如果存在的话,判断该名称是否为档案,若为档案则将之删除后建立一个档案,档名为 logical ,之后离开;
  4. 如果存在的话,而且该名称为目录,则移除此目录!
看起来似乎很复杂,其实很简单的啦!我们来试试看:
 
[test @test test]# vi test17-ifthen.sh
#!/bin/bash
# using if and then to select file or directory
# VBird 2002/06/27
if [ ! -e logical ]; then
        touch logical
        echo "Just make a file logical"
        exit 1
elif [ -e logical ] && [ -f logical ]; then
        rm logical
        mkdir logical
        echo "remove file ==> logical"
        echo "and make directory logical"
        exit 1
elif [ -e logical ] && [ -d logical ]; then
        rm -rf logical
        echo "remove directory ==> logical"
        exit 1
else
        echo "Does here have anything?"
fi

然后请你依序执行 sh test17-ifthen.sh ; ll 看看这个目录底下 logical 那个档案有什么变化状况!呵呵!了解了吗?就是这么简单!这个动作可以让我们很轻松的就判别到某个档案的存在与否!嗯!不错用!赶快来使用看看吧!


script 如何 debug
scripts 在执行之前,最怕的就是出现问题了!那么我们如何 debug 呢?有没有办法不需要透过直接执行该 scripts 就可以来判断是否有问题呢!?呵呵!当然是有的!我们就直接以 sh 来进行判断吧!
 
[test @test test]# sh [-nvx] scripts
-n :不要执行 scripts ,查询 scripts 内的语法,若有错误则予以列出!
-v :在执行 scripts 之前,先将 scripts 的内容显示在屏幕上;
-x :将有使用到的 scripts 内容显示在屏幕上,与 -v 稍微不同!
[test @test test]# sh -n test01-hello.sh
[test @test test]# sh -v test01-hello.sh
#!/bin/bash
# This program will print the "Hello! How are you" in your monitor
# Date: 2002/06/27
# User: VBird
hello="Hello! How are you"
echo $hello
Hello! How are you
[test @test test]# sh -x test01-hello.sh
+ hello=Hello! How are you
+ echo 'Hello!' How are you
Hello! How are you

熟悉 sh 的用法,将可以使您在管理 Linux 的过程中得心应手!
 
对于 Shell scripts 的学习方法上面,需要『多看、多模仿、并加以修改成自己的样式!』是最快的学习手段了!网络上有相当多的朋友在开发一些相当有用的 scripts ,若是您可以将对方的 scripts 拿来,并且改成适合自己主机的样子!那么学习的效果会是最快的呢!


2002/06/27:第一次完成
2003/02/10:重新编排与加入 FAQ


本网页主要以Firefox配合解析度 1024x768 作为设计依据     鸟哥自由软件整合应用研究室