UNIX Shell Programming

(in Borne Shell)

 

 

 

 

 

 

 

 

 

中華民國八十八年三月十九日

國立中興大學電子計算機中心

吳賢明

woody@nchu.edu.tw

楊崇誠 ccyang@nchu.edu.tw 黃仲永 hjy@ nchu.edu.tw

 

UNIX Shell Programming (Borne Shell or /bin/sh)

 

 

#!/bin/sh

echo " This is a Borne Shell Script "

 

(為使你的Shell Script可以執行,你必須將這個Shell Script的執行權限“x”打開 : chmod +x shell_script)

 

var=value

$var

Exmaple 1:

#!/bin/sh

woody="吳賢明 的小名"

echo $woody

Example 2:

    1. echo "woody就是 $woody"
    2. echo 'woody就是 $woody '

 

結果:

1.woody 就是 吳賢明 的小名

2.woody 就是 $woody


 

變數

變數意義

$#

Number of arguments on the command line

$-

Shell 的選項

$?

Exit value of the last command executed ( in general, 0代表執行無誤,1代表指令有誤)

$$

Process number of the current process

$n

Command line 中的參數,$0代表指令名稱,$1代表第一個參數,$2代表第二個參數….

$*

Command line 中的所有參數

 

Example 3:

Shell Script : var_test

#!/bin/sh

echo $#

echo $-

echo $?

echo $$

echo $0

echo $1

echo $2

echo $*

 

執行 var_test 1 2 3 4 5

結果? (Try it !!)

 

Example 4:

#!/bin/sh

var1="變數一"

var2="變數二"

cat << SEGMENT1

Strings between the two "SEGMENT1"s will be treated as

constantand printed out from the monitorVariables between

these two SEGMENT1's will be calculated before any processing

Yo umay try the following

var1=$var1

var2=$var2

SEGMENT1

Another Example:

cat << \SEGMENT2 ##ß 請注意這行中的符號『\』

這是避免變數被解釋的方法,請注意義以下字串被列印

出來的結果:

var1=$var1

var2=$var2

SEGMENT2

 

這個程式執行的結果為:

Strings between the two "SEGMENT1"s will be treated as

constantand printed out from the monitorVariables between

these two SEGMENT1's will be calculated before any processing

Yo umay try the following

var1=變數一

var2=變數二

Another Example:

這是避免變數被解釋的方法,請注意義以下字串被列印

出來的結果:

var1=$var1

var2=$var2

 

Example 5:

#!/bin/sh

echo –e "Please input your name: \c" #(\c迫使游標不換行)

read name

echo "Hello, $name"

 

if condition

then

command(s)

[elif condition

then command(s)]

[else

command(s)]

fi

中括號([])表示可有可無。

#!/bin/sh

ls -l /etc/host.conf

if [$? -eq 0]

then

echo "/etc/host.conf is there !!"

else

echo "/etc/host.conf is not there !!"

fi

[$var -op value]可以比對$varvalue ($var為數值變數)op的值有gt (greater then)eq(equal to )lt(less than)ge (greater than or equal to )ne(not equal)le(less than or qual to)

 

test -options file_name or [-option file_name]

常用option與其所代表的意義如下:

-r

True if file exists and readble

-w

True if file exists and writadble

-x

True if file exists and executadble

-f

True if file exists and is a regular file

-d

True if file exists and is a directory

-u

True if file exists and is setuid

-s

True if file exists and is greater than zero in size

test -option string or [-option string]

常用option與其所代表的意義如下:

-z

True if string length is zero

-n

True if string length is non-zero

test string1 = string2 or [string1 = string2]

test string1 != string2 or [string1 != string2]

à True if string1 is not identical to string2

test n1 -op n2 or [n1 -op n2]

常用op(運算元)與其所代表的意義如下

-eq

True if n1and n2 are equal

-ne

True if n1and n2 are not equal

-gt

True if n1 is greater than n2

-ge

True if n1 is greater than or equal tp n2

-lt

True if n1 is less than n2

-le

True if n1 is less than or equal to n2

** Both n1 & n2 are intergers

 

case variable in

pattern1[|pattern1a]) commands ;;

pattern2) commands;;

….

*) commands ;;

esac

#!/bin/sh

cat << EOF

****************************************

a.I need a banana.

b.I need an orange.

c.I need an apple.

****************************************

EOF

read choice

case $choice in

a|A) echo "You need a banana !!" ;;

b|B) echo "You need an orange !!" ;;

c|C) echo "You need an apple !!" ;;

*) echo "Bad choice, see yo next time !!";;

esac

 

while condition

do

commands

done

##迴圈中commands會一直被重複執行直到condition 的值為偽為止。

#!/bin/sh

NO=11

START=1

SUM=0

while [ $NO -gt 0 ]

do

SUM=`expr $START + $SUM`

START=`expr $START + 1`

NO=`expr $NO - 1`

done

echo "The answer is $SUM"

說明:

  1. Shell中把所有變數均當成字串,因此進行整數運算時,必須特別註明。內建function “expr”可以幫我們進行這樣的數值計算。
  2. 符號 ` 代表在變數給定的運算式中,帶有指令或函數。設定變數必須先行運算,再做設定。例如

HOSTNAME=`/bin/hostname`

$HOSTNAME的值會是指令/bin/hostname執行的結果

 

until condition

do

commands

done

##until指令用法與while恰巧相反;迴圈中commands會一直被重複執行直

condition 的值為真。

 

Exercise:

  1. 請將講義Example 1~ 8親自做一次,並確實了解其語意(Know HOW)
  2. token=super
  3. 請問下面指令會得到什麼結果?你知道為何如此嗎?

    echo $tokenman

    echo ‘$token’man

    echo “$token”man

  4. 請寫一shell script
  5. (1).提示使用者選擇1.可連(telnet)ccsun, 2.可連(telnet)bbs, 3. 可連(telnet)nmc.nchu.edu.tw

    (2).從鍵盤(keyboard)讀取變數『Choice

    (3).IF Choice=1 à telnet to ccsun

    IF Choice=2 à telnet to bbs

    IF Cjoice=3 à telent to nmc.nchu.edu.tw

    Otherwise Say something to user, and terminate the program

  6. 請重新改寫Example 8 with command “until” !
  7. 請寫一個shell script:

(1).提示使用者輸入username

(2).若此user存在,輸出『此使用者存在,請輸入下一筆資料』

(3).若此user不存在,輸出『此使用者不存在,請重新輸入』

(4).若輸入為Esc鍵,則跳離此一程式。

提示:

(1).grep username /etc/passwd可檢查該user是否存在。

(2).echo $?可以檢查上一個指令是否執行成功。

(3).輸入特殊符號(Hot Key)的方式為^v + Hot-key