Dz shell scripts
HA@As峹аѾ\ B
̪sG2002/06/27
FDz Scripts
scripts PĤ@ scriptsG
һPBŸGdeclare
ͦ scripts G read
scripts ޿P_PB⦡G
󦡧P_Gif...then...fi, case.....esac
jGfor....do....done, while...do...done, until...do...done,
script p debug G
Dm

FDz Scripts
oӰDi쪺ܤFAڬnDz scripts OHnǥiiHrIHIpGzuQny|z Linux NnܡAo@ӳTiHLhݤ]SYALApGzQn[AѻP Linux A Linux B@󶶺Z~A٥iHEL~A Linux Server b Internet WѬAȪܡA scripts ٬Oh־Ǥ@ǧaIOH] scripts iH]pyoD󤧤Uݭni椰ʧ@Iznpݳoӥ\IzbqeɭԡAMADQ@Ǥʥ]չϤJIFAoӮɭԦpGA Linux DiHzLyӫʥ]մX٬OsuѤANHצ IP zApGiH]pp\઺ܡAIiNoFFIz Linux DNiHOynorI smart!z

FwD~A scripts ٦۷hBΥ\IҦpzQnb Linux W@A@~Aӳoӧ@~O@ǼƭȤ譱pAoӮɭ Scripts ]iHzpI٤ݭnΨ fortran, c o{yOI scripts NO@ӥiHΪ program oI۷ΧaIA scripts ̶}lQsϥΪ\NOyJ@Ǧb command line UFsOANLgJ scripts AӥѪ scripts ӱҰʤ@sꪺ command line OXJIzҦpG sWh ( iptables )A}J{Ǫ ( NOb /etc/rc.d/rc.local Y ) AOۦ\աI AFApGҼ{ program A scripts ]iHQAȬOڭ̧@jꪺOJb@ɮ׸̭AӪɮ״NiH桦@SSOqINOo²աI

t~t~Absg scripts ɡAb Linux A̱`ϥΪNO vi o@Mѽs边FAҥHoAF²ާڭ̪ Linux AI٬ODzߤ@UnFաInnrI ^_^


scripts PĤ@ scripts
IbW@ bash shell F@ܼưաI޽uOաIAOFۦӨӪ scripts NNաIO script ڡHѦrWNӻA script NOy}B@zNAMFaINONڭ̭n檺eg@ӡy}zAtΨ̾ڳoӡy}zӰڭ̷QnFInFI{bڭ̨ӰQפ@Ug scripts nFI򥻤WA@ script Q檺ɭԡA bash |ڥHP_檺BJG
  1. pGŪ@ Enter Ÿ] CR ^ANն}lӦROF
  2. pPe bash command 쪺AOhӪťշ|QF
  3. Ӫťզ]NQIAåB tab ]O|Qz|I
  4. ܩpG@檺eӦhAhiHϥ \ өܤU@F
  5. ~Aϥγ̦h # iѡI[b # ᭱rANQѤrӳQI
MAbg@ scripts ɭԡA̦ni}nߺDG
  1. ŧiϥΪ shell H]SOdNo@IAbYǪpAҦp /etc/crontab pUApGSŧiϥΪ shell A``|X{~TӾɭP scripts LkQI^
  2. script e\BTB@̡Bɤ
  3. C@ӤjBJDn\]]KѦۤvӭק蠟ΡI^
scripts oɮ׭npӰOH檺kءG jPWNOo˰աIOKIٰOoڭ̦be@wgLFܼƪ]w觋FaHnFAڭ̴NHĤ@ scripts ӱNڭ̳]wܼƪL show XӡIIӳ]p@Uo script aI
nFAڭ̨ӫإ߲Ĥ@²檺 script aI²檺@ӨҤlANObùWCXy Hello ! How are you ?zAiHo˼gG]G̱`ϥΨӧ@ shell scripts g@nANO vi աIůunhx@U vi I^
 
[root @test /root]# mkdir test; cd test  <==إߤ@ӷsؿAҦ scripts ȦsbI
[root @test test]# vi test01-hello.sh
#!/bin/bash                        <==b # [W ! P shell W١AΨӫŧiϥΪ shell
# oӸ}γ~bCX Hello ! How are you bùW
# ɤG 2002/05/20
# Made by VBird
hello=Hello\ \!\ How\ are\ you\ \?      <==oNOܼưաI
echo $hello

[root @test test]# sh test01-hello.sh
Hello ! How are you ?                   <=XGܦbùWI

o̵L`N@UG

IܿħaIAwg|g scripts FIwg㦳춥{]pOoIIOıoI^_^...InFAۤUӧڭ̼gyL@IAY@ script YӥHWܼƭnۤޥΩOHIoӮɭԶKӤ@U " P ' PaI
 
[root @test test]# vi test02-2var.sh
#!/bin/bash 
# oӸ}γ~bޥΨܼơAK@U " P ' P
# 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

ݨXGFaIISI " P ' ̤jPNObणOyܼƤezաIA@Aӳ޸y ' zYƳNܦyªrzӤOSrI


һPBŸGdeclare
BAѤFaIHnFA{bڭ̨ӪݡApGzp⵲GAݭnJ 2*3+5*13-32+25 ɡAåBb̫Xy Your result is ==> zӫ˼go@²檺 script OHiHo˸ոլݡG
 
[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

ˡHܦaIn٦bYIAUݤ@UaI


ͦ scripts G
Oͦ scripts OH²աIҦpAb Windows w˵{ɡAtΤO``|X@ӵAݧAyU@BzByW@BzΡyzܡHNOͰաI{|̾ڱzJƨӶiP_AOKI²檺ͦOOOHINO read oӫOաI read \NOy̾ڱzbLJG input ܼƤezAҦpG
 
[root @test test]# read name
VBird <==oOLJG
[root @test test]# echo $name
VBird

nFIڭ̨ӳ]w@UAz script b檺ɭԡANzLJƦCXӡIp󰵩OH
 
[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

NOo²Aڭ̫᭱ٷ|~򪺽ͨPOAӮɭԿJƥiN󭫭nFI

nFIU@Bڭ̦Aӻ@˩wq@ script ѼƪNIHHUڭ̪ҡG
 
[root @test test]# myscript opt1 opt2 opt3 opt4
@@@@@@@@@@$0@@@ $1@ $2@ $3 @$4

oONOHKKINOAbo script ] myscript ^̭AunܼƦW٬ $0 Nܬ myscript oөNNA]NOG

$0 : myscript YO script ɦW
$1 : opt1 YOĤ@Ӫ[Ѽ (parameter)
$2 : opt2
$3 : opt3
o˻γ\OܲMAڭ̨Ӫ@Uo script ݬݴNoLNաI
 
[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

oӪFbBΤW]O۷nAҦpzno script Wٮɡ]]ɭԨϥΪ̷|ۦɮצW١^Ahoӥ\ܼƴN۷nFIAѥGIH


scripts ޿P_PB⦡G
AӪoӪFiN󭫭nFAרb scripts INpypPwYɮשΥؿAΪ̬OpPw{Ӵ¦VӤVizHoӪFݭnn@Ǫ޿跧~IUڭ̤O@U޿P_PB⦡AAӳ]wXӤp scripts ոլݤPΪkAHojaAoI

󦡧P_Gif...then...fi, case.....esac
OKIڭ إߤFĤ@ script AbùWH script ӿXݭԻyInFA{bڭ̦pGn scripts [WyP_znpӤu@OHoNOҿת󦡧P_FI̱`ϥΨ쪺NOy if ... then .... else if .... then ... end if z@kFIoӱP_ykG
 
if [ P_@ ] && (||) [ P_G ]; then       <== if O_lNA᭱iHYzӧP_Aϥ && ||
    椺e{
elif [ P_T ] && (||) [ P_| ]; then     <==ĤGqP_ApGĤ@qSŦXNӦjM
    ĤGqe{
else                                            <==eqŦXɡANHoqeӰI
    ĤTqe{
fi                                              <== if then P_I

WNOoo˪Gby[]z̭O󦡡ApGOƦXP_]pYABhC޿P_^ANݭnbӤ[Wy && (and)zΪ̬Oy || (or)zo˪޿B⦡~IpGOhܪܡANݭnH elif ]optional, ܩʪAYݭn~[WI^ӷsWt@ӱFpGҦ󳣤AΡAhϥ else ]optional^Ӷi̫᪺椺eoI

LAo̦Xӷs`Ǫ~Aڭ̻ݭnӥ[j@UG
  1. b [ ] Au঳@ӧPOF
  2. b [ ] P [ ] AiHϥ && || Ӳ´POF
  3. C@ӿWߪ󤧶yݭnťӹj}zI
רO̫@IA̮eǪաInFAڭ̨Ӷi@²檺POnFI
 
[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!

²檺@ӨҤlaIJ y ɭԡANLiAYD y hHiIOo̦ӰDANOpGڿJ Y ɡA{٬OFIHIoӮɭԴNݭnϥΨ || oӪFաIڭ̥iHo˰I
 
[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...

nFIpGA[WeLG parameter ΩOHIoӤ]Z쪺Aڭ̦AӸոլݡG
 
[root @test test]# vi test08-ifthen.sh
#!/bin/bash
# set parameters in the if then
# ݭn[W hello oӰѼƤ~|ܥTI
# 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

IOOOHoӹ scripts {Ѧ@w{תAѤFaIInFAUڭ̨Ӫ@ӤjI]zwgD netstat P grep oӪF𫍧ΪkApGnӰADW port O_}ҮɡAiHϥΩUdҨӶiG
 
[test @test test]# vi port.sh                                  <==s@ɮ׬ test1.sh script
#!/bin/bash                                                     <==ŧiϥΪ 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`                    <==oӴNOܼưաIèϥΤF޽uROI
  if [ "$www" != "" ]; then                                     <==}l󪺧P_oI
      echo "WWW is running"                                <==Y󦨥ߡANCLo@檺eI
  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           <==ӥHW󦡡ANϥ && || ӤjI
      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                                <==ݬݿXGI
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!

²aIWo˴NiHNADWƶiѪRoI

𫍧y if then fi zΦOH{ӦۦP_ApGڤwgWnXӶحnӰAunܰ檺y觋zNiHT檺An򰵩OH²檺ҤlNOڭ̱``ϥΨ쪺 /etc/rc.d/init.d/ Y scripts oIҦpGsҰ xinetd OϥΡG
/etc/rc.d/init.d/xinetd restart
`NoIӬO restart ءAM script N|۰ʪhjM restart ظ̭phIӴNO case ... esac ϥμҦoIS`NA}lOy case zhOϥ case ˼gy esac zܡIܦaIoخ榡O˩OH
 
case 觋(string) in          <==}lqAӺ觋iAq`ϥ $1 o@تUFI
    觋@)
       {q
       ;;                     <==觋@ŸI
    觋G)
       {q
       ;;
    *)
       echo "Usage: {觋@|觋G}"     <==CXiHQΪѼƭȡI
       exit 1
esac                         <==o case ]wBI

b觋(string)榡DnءG

  1. UFGNOHy ɮ + string z觋Ӱ檺(/etc/rc.d/init.d Y򥻳]w觋^Ah string iHgy $1 z]bɮ׫᭱[JѼƪĤ@ӰѼơI^
  2. ʦGNOѿùXi઺ءAMϥΪ̿JAoӳq`tXy read variable zM string hgy $variable z榡I
P˪Aڭ̫إߤ@ӦW test2.sh ɮרӸհݬݡCpڭ̦@iTq榡Ӷi@AO one, two, three Að]ϥΪUFAhiHgG
 
[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                                   <==ϥΪUFOAI
  one)
        echo "your choice is one"
        ;;
  two)
        echo "your choice is two"
        ;;
  three)
        echo "your choice is three"
        ;;
  *)
        echo "Usage {one|two|three}"       <==CXiHϥΪѼơ]pGϥΪ̤UF~ѼƮɡ^
        exit 1
esac
[test @test test]# sh test09-case.sh       <==浲GIܨS۹諸ѼơIҥHCXiHѼơI
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 SpOHIڭ̧QΤW觋ӭק@UeoI
 
[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   <=o@OzJI
your choice is two

pI²aIH /etc/rc.d/init.d/ Y script NӬݪFaI~Aѩ٦ҿתy(function)z]NOyNݭn檺{qg@Ӥp϶AӳoӤp϶hH@ӦW٨ӨNzA²檺觋iHбzѦҤ@UztΤ /etc/rc.d/init.d/sendmail I`Nݤ@U start() UǭөNNIANiHDNFIѩڭ̳ǫäЧApg{]򥻤WA scripts NOp{աI٦nI VBird HeN{lIMٯu script OI^AҥHNunI쬰oI


jGfor....do....done, while...do...done, until...do...done,
b{qA̱`ϥΨ쪺NOjFIjOܭn@uAרO㦳P_ΦjAܱ`QϥΨӧP_@ǨƶiʻP_IO{򪾹Dɭӭnoӵ{OHINݭn[JP_oInFA²檺P_iHOUXءG for OwgDh֭ run FAYOwgDn]XFAܩ until P while hOOG o̤ӬۦPաI
nFIڭ̥ӽͤ@U²檺jANOQ for oӪFӶiInFA]ڭ̭p 1 + 2 + 3 .... + 100 AH script npgOHܦh觋Aڭ̨ӽͤ@ do...done nFI
 
[test @test test]# vi test11-loop.sh
#!/bin/bash
# Using for and loop
# VBird 2002/06/27
declare -i s  # <==ܼƫŧi
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

Ъ`NI for (( @; G; T)) oOnI

ҥHաIWҤlOG i=1 }l i<= 100 AC i [@Ӱ橳U{q]NO s=s+i ^A i >100 ]]NO i=101 ^NXo@q{qIˡIaI
nFIϥ while Ϊ̬O until n˰OH곣OthpIڭ̨ϥΩU script Ӷi 1 ~ 100 ֥[ʧ@I
 
1. ϥ while G
[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 G
[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

HHHHI²aI

ĤGӨҤlӦ۩ jony ѡIoOt~@ذj骺觋AiHΨӧP_DƦrI
 
[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

o@خ榡OHť@ i oܼƪܶءI]NOAW $LIST oܼƷAHťӤjɭԡA@iHXӥ|ӡIҥHoIH do ..... done ... NiHOgX|өNNաInաI򦳨SkQγoӪFӱNA Linux DWb ( account ) LXөOHI²rIڭ̧Q cut sort H /etc/passwd oɮרӧo@ script A@kpUoG
 
[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
....

OKIAӡAڭ̨ӨϥΤ@Uͦj@ΧaIIڭ̿J y Y ɡA{NHIӫ˰HI
 
[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

WOAJ Y Ϊ̬O y ɤ~X do...done j𫗪Iӥh橳UFIIܦnaIIۤUӡAڭ̨ӧP_@UؿO_sbnFIoO`ΪI

Uӧڭ̧PO@Uҿתy޿P_zϥΤ觋աIڭ̤OwgDFܡAڭ̥iHϥαP__w쩳Sɮס] -e ^Ϊ̬OӦW٬OݩؿΪ̬Oɮס] -d -f ^AUӧڭ̨ӧP_@Ӭy{nFG
  1. dݤ@U /root/test/logical oӦW٬O_sbF
  2. YsbAhإߤ@ɮסAϥ touch ӫإߡAإߧ}F
  3. pGsbܡAP_ӦW٬O_ɮסAYɮ׫hNRإߤ@ɮסAɦW logical A}F
  4. pGsbܡAӥBӦW٬ؿAhؿI
ݰ_ӦGܽA²檺աIڭ̨ӸոլݡG
 
[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

MЧA̧ǰ sh test17-ifthen.sh ; ll ݬݳoӥؿU logical ɮצܤƪpIIAѤFܡHNOo²IoӰʧ@iHڭ̫ܻPNPOYɮתsbP_IIΡI֨ӨϥάݬݧaI


script p debug G
scripts b椧eA̩ȪNOX{DFIڭ̦p debug OHSkݭnzL scripts NiHӧP_O_DOIHIMOIڭ̴NH sh ӶiP_aI
 
[test @test test]# sh [-nvx] scripts
-n Gn scripts Ad scripts ykAY~hHCXI
-v Gb scripts eAN scripts eܦbùWF
-x GNϥΨ쪺 scripts eܦbùWAP -v yLPI
[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

x sh ΪkANiHϱzb޲z Linux L{oI
@
Shell scripts DzߤkWAݭnyhݡBhҥBå[Hק令ۤv˦IzO̧֪DzߤqFIW۷hBͦb}o@Ǭ۷Ϊ scripts AYOziHN𫍧 scripts ӡAåB令AXۤvDˤlIDzߪĪG|O̧֪OI


Dm ( nݵ׽бNƹʨyGzUťճBAUťճBYi )
 


2002/06/27GĤ@
2003/02/10GssƻP[J FAQ