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.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
constant
,and printed out from the monitor。Variables betweenthese 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
constant
,and printed out from the monitor。Variables betweenthese 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]可以比對$var與value ($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"
說明:
HOSTNAME=`/bin/hostname`
則
$HOSTNAME的值會是指令/bin/hostname執行的結果
until condition
do
commands
done
##until指令用法與while恰巧相反;迴圈中commands會一直被重複執行直
到condition 的值為真。
Exercise:
請問下面指令會得到什麼結果?你知道為何如此嗎?
echo $tokenman
echo ‘$token’man
echo “$token”man
(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
(1).提示使用者輸入username
(2).若此user存在,輸出『此使用者存在,請輸入下一筆資料』
(3).若此user不存在,輸出『此使用者不存在,請重新輸入』
(4).若輸入為Esc鍵,則跳離此一程式。
提示:
(1).grep username /etc/passwd可檢查該user是否存在。
(2).echo $?可以檢查上一個指令是否執行成功。
(3).輸入特殊符號(或Hot Key)的方式為^v + Hot-key