第一个shell程序:Hello
- [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 ? <=输出的结果显示在屏幕上!
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 <==变成数字啰! ^_^
- [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 对话式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
传参数: [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
if then语句:
- [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!
- [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!
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 又如何呢?嗯!我们利用上面的方式来修改一下内容啰!
- [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
循环: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 :
- [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
- ss=s+i
- ii=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
- ss=s+i
- ii=i+1
- done
- echo "The count is ==> $s"
- [test @test test]#sh test12-loop.sh
- The count is ==> 5050
- [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
转载于:https://blog.51cto.com/huqianhao/955370