第一个shell程序:Hello

 
  
  1. [root @test /root]#mkdir test; cd test <==建立一个新的目录,所有的 scripts 都暂存在此! 
  2. [root @test test]#vi test01-hello.sh 
  3. #!/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 ?                  <=输出的结果显示在屏幕上!

 

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 <==变成数字啰! ^_^

 

 

 
  
  1. [root @test test]#vi test03-declare.sh 
  2. #!/bin/bash 
  3. # This program is used to "declare" variables 
  4. # VBird 2002/06/27 
  5. number1=2*3+5*13-32+25 
  6. declare -i number2=2*3+5*13-32+25 
  7. echo "Your result is ==> $number1" 
  8. echo "Your result is ==> $number2" 
  9. [root @test test]#sh test03-declare.sh 
  10. Your result is ==> 2*3+5*13-32+25 
  11. Your result is ==> 64 对话式script: 
  12. [root @test test]# vi test04-read.sh 
  13. #!/bin/bash 
  14. # This program is used to "read" variables 
  15. # VBird 2002/06/27 
  16. echo "Please keyin your name, and press Enter to start." 
  17. read name 
  18. echo "This is your keyin data ==> $name" 
  19. [root @test test]# sh test04-read.sh 
  20. Please keyin your name, and press Enter to start. 
  21. VBird Tsai 
  22. This is your keyin data ==> VBird Tsai 

 

传参数:  [root @test test]#vi test05-0123
 

 
  
  1. #!/bin/bash 
  2. # This program will define what is the parameters 
  3. # VBird 2002/06/27 
  4. echo "This script's name => $0" 
  5. echo "parameters $1 $2 $3" 
  6. [root @test test]#sh test05-0123 pa1 pa2 pa3 
  7. This script's name => test05-0123 
  8. parameters pa1 pa2 pa3 

 

if then语句: 

 

 
  
  1. [root @test test]#vi test06-ifthen.sh 
  2. #!/bin/bash 
  3. # This program is used to study if then 
  4. # VBird 2002/06/27 
  5. echo "Press y to continue" 
  6. read yn 
  7. if [ "$yn" = "y" ]; then 
  8.         echo "script is running..." 
  9. else 
  10.         echo "STOP!" 
  11. fi 
  12. [root @test test]#sh test06-ifthen.sh 
  13. Press y to continue 
  14. script is running... 
  15. [root @test test]$sh test06-ifthen.sh 
  16. Press y to continue 
  17. STOP! 
  18.   
  19. [root @test test]#vi test08-ifthen.sh 
  20. #!/bin/bash 
  21. # set parameters in the if then 

# 需要加上 hello 这个参数才会显示正确的!
 

 
  
  1. # VBird 2002/06/27 
  2. if [ "$1" = "hello" ]; then 
  3.         echo "Hello! How are you ?" 
  4. elif [ "$1" = "" ]; then 
  5.         echo "You MUST input parameters" 
  6. else 
  7.         echo "The only accept parameter is hello" 
  8. fi 
  9. [root @test test]#sh test08-ifthen.sh hello 
  10. Hello! How are you ? 
  11. [root @test test]$sh test08-ifthen.sh 
  12. You MUST input parameters 
  13. [root @test test]$sh test08-ifthen.sh djdkf 
  14. 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!

 

 

case语句:

case 种类方式(string) in         <==开始阶段,那个种类方式可分成两种类型,通常使用 $1 这一种直接下达类型!
    种类方式一)
       程序执行段
       ;;                    <==种类方式一的结束符号!
    种类方式二)
       程序执行段
       ;;
    *)
       echo "Usage: {种类方式一|种类方式二}"    <==列出可以利用的参数值!
       exit 1
esac                        <==这个 case 的设定结束处!

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

直接下达式:就是以『 执行档案 + string 』的方式来执行的(/etc/rc.d/init.d 里头的基本设定方式),则 string 可以直接写成『 $1 』(在执行档案后面直接加入参数的第一个参数!) 交互式:就是由屏幕输出可能的项目,然后让使用者输入,这个通常必须配合『 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 又如何呢?嗯!我们利用上面的方式来修改一下内容啰! 

 
  
  1.   [root @test test]#vi test10-case.sh 
  2. #!/bin/bash 
  3. # program:      Using case mode 
  4. # Made by:      VBird 
  5. # date:         2002/06/27 
  6. # content:      I will use this program to study the case mode! 
  7. # 1. print this program 
  8. echo "Press your select one, two, three" 
  9. read number 
  10. case $number in 
  11.   one) 
  12.         echo "your choice is one" 
  13.         ;; 
  14.   two) 
  15.         echo "your choice is two" 
  16.         ;; 
  17.   three) 
  18.         echo "your choice is three" 
  19.         ;; 
  20.   *) 
  21.         echo "Usage {one|two|three}" 
  22.         exit 1 
  23. esac 

[root @test test]#sh test10-case.sh
Press your select one, two, three
two  <=这一行是您输入的呦!
your choice is two

 

循环: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:当条件相同的时候,就继续做!

[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

 

1. 使用 while :
 

 
  
  1. [test @test test]#vi test12-loop.sh 
  2. #!/bin/bash 
  3. # Using while and loop 
  4. # VBird 2002/06/27 
  5. declare -i i 
  6. declare -i s 
  7. while [ "$i" != "101" ] 
  8. do 
  9.         ss=s+i 
  10.         ii=i+1 
  11. done 
  12. echo "The count is ==> $s" 
  13. 2. 使用 until : 
  14. [test @test test]#vi test13-loop.sh 
  15. #!/bin/bash 
  16. # Using until and loop 
  17. # VBird 2002/06/27 
  18. declare -i i 
  19. declare -i s 
  20. until [ "$i" = "101" ] 
  21. do 
  22.         ss=s+i 
  23.         ii=i+1 
  24. done 
  25. echo "The count is ==> $s" 
  26. [test @test test]#sh test12-loop.sh 
  27. The count is ==> 5050 
  28. [test @test test]#vi test14-for.sh 
  29. #!/bin/bash 
  30. # using for...do ....done 
  31. # VBird 2002/06/27 
  32. LIST="Tomy Jony Mary Geoge" 
  33. for i in $LIST 
  34. do 
  35.         echo $i 
  36. done 
  37. [test @test test]#sh test5.sh 
  38. Tomy 
  39. Jony 
  40. Mary 
  41. Geoge