You are on page 1of 15

C bn v Shell

A. Gii thiu chung


1. Gii thiu v shell
Khi chng ta mun thc hin mt lnh no trong h iu hnh Unix chng ta cn phi ra lnh Unix hiu c chng ta mun lm g. Vic ra lnh ny c thc hin qua shell. Nh vy chng ta c th hiu mt cch n gin shell l giao din giao tip gia ngi s dng v Unix. Shell nhn lnh t ngi s dng sau dch v chuyn n h thng nhng hot ng cn thc hin p ng yu cu. Hin nay c mt s loi shell trong cc h thng Unix, trong mt s trng hp trong mt h thng no c th c mt hoc nhiu shell cng tn ti. Mt s loi ph bin ang tn ti nh: Bourne shell, Korn shell, C shell, ... Mi loi c s khc nhau nhng tt c u cung cp y cng c thit lp mi trng giao tip gia ngi s dng v Unix.

2. Mc ch ca shell
Shell c 3 mc ch chnh nh sau: - Tng tc (interactive use) - t bin mi trng i vi mi ngi s dng - Lp trnh

Tng tc
Trng hp c coi l n gin khi s dng shell, shell i ngi s dng g cc lnh ti du nhc, sau gi ti h thng yu cu t lnh nhn c.

t bin mi trng i vi mi ngi s dng


Unix shell xc nh cc bin iu khin mi trng ca ngi s dng i vi mi phin s dng. Vic t cc bin ny s xc nh vi h thng nhng tham s nh th mc no s c s dng lm th mc chnh, ni t mail, nhng th mc no c s dng mc nh khi bn gi n cc lnh Unix, ... Mt s bin h thng c th c t trong tp khi ng (start-up file) v c c khi bn login (ng nhp). Trong tp khi ng bn c th t cc lnh ca Unix, nhng ch l nhng lnh ny s c thc hin mi khi bn login.

Lp trnh
Shell cung cp tp hp cc lnh c bit m t c th to nn nhng chng trnh, khi c gi l shell script. Trong thc t hu ht cc lnh ny c th s dng trong ca s lnh ca Unix v ngc li, cc lnh ca Unix u c th vit trong shell script. Shell script rt tin li trong vic gp nhiu lnh c lp vo mt v thc hin nhiu ln. Ngoi nhng lnh n gin ca h thng Unix, shell cn c b sung thm cc cu trc phc tp nh iu khin r nhnh (if/case), vng lp (for/while).

VietHung soft group -

viethung_group@email.com

Mt tp chng trnh ca shell khng quan trng n tn v ui, khng cn dch cng nh khng c mi trng pht trin. Vic son tho mt tp shell c th s dng bt k mt cng c son tho no, ch cn ghi tp vi dng text, sau i thnh dng tp c th chy c. Xem v d 1 trong phn sau hiu thm chi tit. Shell lun gn lin vi h iu hnh Unix, nhng hiu v hc shell khng nht thit bn cn hiu su v h thng cng nh cc lnh ca Unix. Tuy nhin thng qua nhng v d chng ti nu y cc bn c th hiu thm cch s dng lnh v bin h thng. Ch : Chng ti ch nhc n h iu hnh Unix khi gii thiu cng nh hng dn s dng, nhng thc t hin nay Linux l mt h iu hnh k tha ca Unix. Linux cng c nhng shell tng t v bn c th s dng nhng gii thiu v shell y vi cc h thng Linux.

3. Nhng loi shell hin thi


Hin nay c kh nhiu loi shell c s dng trong cc h thng Unix, nhng y chng ti ch cp n 3 loi c bn v ph bin, l: - Bourne shell, c coi l shell chun, c ng v l loi n gin nht. - Korn shell, cao cp hn Bourne shell v cho php son dng lnh. - C shell, s dng c php ca ngn ng lp trnh C v c thm nhiu chc nng tin li. Thng thng cc h thng c t nht l mt loi shell v thng thng Bourne shell c s dng vit shell script, cn s dng mt loi khc cho vic tng tc. Tp /etc/passwd s xc nh loi shell no s c s dng mc nh trong h thng cho mi phin lm vic ca bn. Trong phn cui ca dng cha tn bn, bn c th tm thy thng tin v loi shell no c s dng. Mi khi bn login, h thng s c tp ny ly thng tin khi to cho shell. Thng tin c th gm mt trong nhng dng sau: /bin/sh /bin/jsh /bin/ksh /bin/csh Bourne shell Bourne shell, c thm phn iu khin tc v (job control) Korn shell C shell

Bn c th thay i shell mc nh sang mt loi khc bng cch s dng lnh: V d chuyn t Bourne shell sang C shell: exec csh hoc c th i shell mi bng lnh: chsh [<tn shell>] Cu trc lnh chsh nh sau: chsh [-s <tn shell>] [ -l ] [<tn ngi>] chsh -l lit k cc loi shell hin c (thng tin cha trong tp /ect/shells). Trong phn tip theo, chng ti s gii thiu v Bourne shell, loi tiu chun, n gin v ph dng nht hin nay trong cc h thng Unix.

VietHung soft group -

viethung_group@email.com

B. Bourne shell
1. In mt dng ch ra mn hnh
V d 1: Bn to ra mt tp vi tn vidu1, sau g vo nhng dng sau: #!/bin/sh #vi du dau tien echo Vi du dau tien voi shell. Bn c th s dng vi, emacs, .. son tho tp trn. Sau dng lnh chmod chuyn tp vidu1 thnh tp c th chy c, lnh nh sau: chmod +x thidu1 chy th bn ch vic g: vidu1 <enter> Vic to v dng chmod u cn thc hin i vi mi tp sau khi to ra v cn chuyn thnh tp chy c, chng ti s khng nhc li v sau na. Nhng i tp c chuyn mod mt ln th khng cn lm li khi thay i ni dung hay i tn.

Gii thch:
- Dng u tin l dng c bit, dng xc nh loi shell c s dng v gi chng trnh thng dch shell tng ng. - Dng th hai bt u bng du # ch mt dng ch thch. - Lnh echo dng in ra mn hnh xu k t hay cc bin, echo c cu trc nh sau: echo [-n] [xu k t] Nu c chc nng -n, con tr khng b ngt xung dng sau khi in xu k t. Ngoi ra, bn trong xu xu k t cc bn cn c th s dng mt s chc nng khc nh: \b li li mt k t (backspace). \c khng xung dng (nh -n). \n xung dng. \t in ra k t tab. \\ in ra k t \. \0n in ra k t c s n (s thp phn) trong bng m ASCII. Cc bn c th in ra nhng k t c bit bng cch t sau k t \, v d: \ in k t nhy kp () ra mn hnh. V d: echo \Thong bao co loi! \, \c \007

2. Thc hin cc lnh h thng


V d 2: V d thc hin mt lnh ca h thng. #!/bin/sh #vi du 2 echo

VietHung soft group -

viethung_group@email.com

echo Danh sach cac thu muc va tep: ls l echo echo Vi tri hien thoi: `pwd` Trong lnh ls -l l mt lnh ca h thng c thc hin m khng cn g t du nhc. Ngoi ra tt c cc lnh v tham s khc ca h thng u c th c thc hin mt cch tng t, v d nh: cd, cp, mkdir, chmod, cat, ...

3. Bin v tham s h thng


Cng nh cc ngn ng lp trnh, shell c th s dng bin nhng khng cn khai bo v nh ngha kiu. Cc tham s ca mi trng v h thng c th s dng trc tip bng tn. Tn ca cc tham s thng l mt ci tn, mt k t, s hay mt trong cc k hiu *, @, #, ?, -, $, !\^. V d 3: V d v d sng tham s h thng. #!/bin/sh #Vi du 3 echo "Ten tep [$0]" echo "Bien vao thu nhat [$1]" echo "Bien vao thu hai [$2]" echo "Chi so cua process [$$]" echo "So bien dau vao [$#]" echo "Tat ca cac bien dau vao [$@]" echo "Co cua process [$-]" Cc bn c th hiu thm khi thc hin lnh: vidu3 vi du 3

Gii thch:
Trong , $0 l bin cha tn ca tp va chy. $n, n=1,..9 l cc tham s dng lnh c a v khi chy. $$ l ch s ca tp va chy (ID process). $# l s tham s dng lnh c a vo. $@ lit k tt c cc tham s dng lnh. @- c ca process. Tng t nh cc ngn ng lp trnh khc, shell script cung cp cc php gn v ly gi tr ca bin. V d c bin vi tn var, vic gn v ly gi tr c hiu nh sau: var = <gi tr> gi tr y c th l mt s, mt xu k t hay t mt bin khc. $var dng ly gi ca bin var.

VietHung soft group -

viethung_group@email.com

4. Lnh vo ra
Lnh in ra echo nh c gii thiu trong cc v d trc, lnh read c dng c vo t bn phm. V d 4: V d v lnh c vo v in ra d liu. #!/bin/sh #Vi du 4 echo Ban ten gi: read ten echo Chao ban $ten

Gii thch:
- read dng nhn gi tr t bn phm sau gn vo bin ten. - $ten tr ra gi tr m n lu tr. Lnh read cn c th nhn nhiu bin cng mt lc v c cu trc nh sau: read <bin 1> [bin 2] [bin 3] ... V d: read ten dienthoai diachi Khi bn g vo: Hung 0123456 334 Nguyen Trai Bn dng cc lnh sau in th cc bin ra mn hnh: echo Ten : $ten echo Dien thoai : $dienthoai echo Dia chi : $diachi Trn mn hnh s in ra: Ten : Hung Dien thoai: 0123456 Dia chi: 334 Nguyen Trai Nh vy c th hiu nh sau: Cc bin c nhn gi tr ln lt cho n du cch, bin cui cng s c nhn ton b phn cn li. i vi bin cui cng, nu khng cn d liu th s nhn gi tr rng (null).

5. Phn bit du huyn ( ` ), nhy n ( ' ) v nhy kp ( " )


Trong shell c ba du `, , c dng trong cc lnh in ra mn hnh hay lnh gn, nhng ngha v phng thc thc hin i vi nhng du ny l khc nhau.

5.1 Du ( ` )
V d trong mt tp shell c nhng lnh sau: currentdir = `pwd`

VietHung soft group -

viethung_group@email.com

linecount

= `wc l $filename`

Lnh th nht s c thc hin v gn ng dn hin thi vo bin currentdir, lnh th hai c thc hin v m s dng trong tp c tn trong $filename ri gn vo bin linecount. Nh vy bn c th hiu mt cch n gin l nhng g vit trong gia hai du ` ` s c coi l lnh ca h thng v c thc hin, nhng tham s sau cc lnh h thng cng c t ng gn trong phn ny.

5.2 Du ( ' ) v ( " )


Khc vi du huyn ( ` ), nhng thng tin gia hai du nhy (nhy n hoc nhy kp) c coi l thng tin c s dng trong lnh echo v s c in ra mn hnh hay c gn vo bin dng xu. Nh vy, khng th vit cc lnh h thng gia hai du nhy m ch cc cc xu k t hay cc bin. Chng ta xem xt nhng dng v d sau: myname = "Viet Hung" echo "$myname" echo '$myname' # gn gi tr cho mt bin # kt qu ra mn hnh: Viet Hung # kt qu ra mn hnh: $myname

Bn c th d thy s khc bit ca hai du nhy n v nhy kp qua nhng dng lnh trn, i vi nhy kp (), khi in ra s c thc hin vi gi tr ca bin sau du $. i vi du nhy n () th s in ra y nguyn nh trong dng vn bn. Thng thng du nhy n t c s dng nhng li rt tin li khi mun in y nguyn mt dng vn bn, c bit l khi c cc k t c bit nh $, \.

6. Cc cu trc phc tp
Nh gii thiu, ngoi nhng lnh n gin nh c, in ra mn hnh v thc hin cc lnh h thng, shell cn h tr vic s dng cc lnh phc tp hn nh if-then-else, for/while. Phn di y chng ti s gii thiu n nhng cu trc ny.

6.1 Cu trc vng lp: for


Cu trc vng lp for c xy dng nh sau: for <bin k t> in <xu> do <cc lnh cn thc hin> done Qua v d di y bn c th hiu r hn cu trc vng lp for. V d 5: V d v vng lp for. #!/bin/sh #Vi du 5

VietHung soft group -

viethung_group@email.com

word= "abcde" # dem = 0 # for letter in $word # do count=`expr $count + 1` echo "Letter $count is [$letter]" done

khi to mt xu khi to bin count vng lp vi bin letter # lnh bt u vng lp # tng bin count ln 1 # in ra bin letter # lnh kt thc vng lp

C th gii thch nh sau: vi mi letter trong word th thc hin nhng lnh nm trong dodone, trong v d trn bao gm tng bin count v in bin letter ra mn hnh. Ch : Trong v d trn c dng lnh expr gi lnh thc hin tnh ton ca h thng tnh php cng trc khi gn vo bin count.

6.2 Cu trc vng lp: while


Cu trc ca vng lp while c th hin nh sau: while [ <iu kin> ] do <cc lnh> done V d 6: V d v vng lp while. #!/bin/sh #Vi du 6 word= "abcde" # dem = 0 # while [ $count lt 5 ] # do count=`expr $count + 1` echo "Letter $count is [$letter]" done

khi to mt xu khi to bin count vng lp vi bin letter # lnh bt u vng lp # tng bin count ln 1 # in ra bin letter # lnh kt thc vng lp

Cc bn c th thy ngay cu trc hai vng lp trn gn hon ton ging nhau, ch khc dng for/while. Trong v d trn, $count -lt 5 c coi l iu kin ca vng lp. Php so snh -lt l php so snh nh hn hoc bng (less-than) trong lnh test ca Unix/Linux. Lnh kim tra iu kin trn s tr ra gi ng (1) tr hoc sai (0) thc hin tip hay thot khi vng lp. Ngoi php so snh -lt" cn c -gt-ln hn, -eq-bng, -ne-khng bng. Trong phn ph lc chng ti c lit k li cc lnh, tham s v php ton ca shell, cc bn c th c bit thm cc php ton khc.

VietHung soft group -

viethung_group@email.com

Ch : Trong phn iu kin ca vng lp while cng nh trong nhng iu kin khc, sau du [ v trc du ] bt buc phi c du trng (du cch).

6.3 Cu trc vng lp: until


Chng ta c th hiu vng lp until tng t nh while. Cu trc ca vong lp until nh sau: until [ <iu kin> ] do <cc lnh> done

6.4 Cu trc r nhnh: if - else


Cu trc r nhnh c th c hiu qua cc t kha c cu trc nh sau: if [ <iu kin> ] then <lnh> [ elif <lnh> then <lnh> ] ... [ else <lnh> ] fi i vi cu trc ny c hai dng, n gin v phc tp. Sau y chng ti s gii thiu hai v d cc bn hiu cch s dng. V d 7: Cu trc if n gin. #!/bin/sh #Vi du 7a echo Nhap so a: read a echo Nhap so b: read b if [ $a lt $b ] then echo so a nho hon so b. elif [ $a eq $b ] then echo so a bang so b. else echo so a lon hon so b. fi

#kim tra a nh hn b khng

#kim tra nu a bng b

#trng hp cn li #kt thc

V d trn thc hin i vi cc s, di y l v d i vi ng dn v tp trn a.

VietHung soft group -

viethung_group@email.com

#!/bin/sh #Vi du 7b if [ -f $dirname/$filename ] then echo "Tep [$filename] da ton tai." elif [ -d $dirname ] then echo "Duong dan [$dirname] da ton tai." else echo "Ca duong dan [$dirname] va tep [$filename] khong ton tai." fi Trong v d trn, f l c kim tra s tn ti ca mt tp, d l c kim tra s tn ti mt th mc. V d 8: Cu trc if phc tp. Trong v d ny bn s hiu cch s dng iu kin kp; && (v), || (hoc). #!/bin/sh #Vi du 8a echo Nhap so a: read a echo Nhap so b: read b echo Nhap so c: read c if [ $a lt $c ] && [ $b lt $c ] then echo so c l so lon nhat. fi #xem c c ln nht khng

Trong v d trn, && (v) l du kim tra kp kt hp hai iu kin. Ta c th hiu iu kin sau if nh sau: Nu a nh hn c v b nh hn c th ... Ngoi ra cn c || (... hoc ...). #!/bin/sh #Vi du 8b if [ -f $dir/$file ] || [ -f $dir/$newfile ] then echo "Hoac tep [$file] " echo "hoac tep moi [$newfile] da ton tai"

VietHung soft group -

viethung_group@email.com

elif [ -d $dir ] then echo "Duong dan [$dir] da ton tai" else echo "Ca duong dan [$dir], tep [$file va tep moi $newfile] deu khong ton tai" fi

6.5 Cu trc r nhnh nhiu trng hp: case


Cu trc case c th c hiu qua tp cc t kha sau: case <bin> in bin-1) <lnh> bin-2) <lnh> bin-3) <lnh> ... *) exit esac

#cn li

V d 9: V d v case i vi tham s u vo ca mt tp chng trnh. #!/bin/sh #Vi du 9 size=0 page=200 option = while [ "$1" != "" ] do case $1 in -?) echo Su dung cac tham so l,-p,-s. exit;; -l) line = 50 page = 500 option = $option page[$page] line[$line] shift;; -p) line = 40

VietHung soft group -

viethung_group@email.com

option = $option page[$page] line[$line] shift;; -s) size = 10; shift 2;; *) echo "Tham so vao khong co trong [p, l, s]"; exit;; esac if [ $size = 0 ] then size=`echo "$page / $lines" | bc` else lines=`echo "$page / $size" | bc` fi done echo $option $lines $size Trong v d trn a ra cch s dng cu trc case, ng thi gii thiu cch nhn v x l tham s u vo khi chy mt tp shell script. Lnh shift l lnh dch tham s u vo sang tri 1 thc hin cc tham s tip theo. c bit trong cn ch cch gi my tnh s vi s cn tnh c a vo trc.

6.6 Hm
Shell h tr khai bo v s dng hm con, y l mt h tr rt hu ch nhng cn phi khai bo trong phn khi ng ca ngi s dng, c th l trong tp .profile. Mt v d n gin: uppercase() { echo $1 | tr 'abcdefghijklmnopqrstuvwxyz' \ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' } Hm trn dng s dng lnh tr ca h thng nhm chuyn cc ch thng thnh ch hoa. Vic gi hm trn ch cn vit: uppercase thu chuyen doi hoc smallword = thu chuyen doi uppercase $smallword Nhng nh ni v s khc bit ca kt qu trong du nhy kp, kt qu ca hai dng lnh sau: largeword = `uppercase $smallword`

VietHung soft group -

viethung_group@email.com

largeword = `uppercase $smallword` l khc nhau. Dng th nht tr ra: THU CHUYEN DOI, cn dong th hai se tra ra: THU.

7. Lm vic vi cc tp
Trong v d 7b chng ti gii thiu cch kim tra s tn ti ca mt tp hay mt ng dn nn trong phn ny s khng cp n na m ch a ra cch c v ghi mt tp. #!/bin/sh #Vi du dem so tep trong thu muc count=1 for file in `ls -1 *` do echo "$count: $file" >> $mnu0 count=`expr $count + 1` done #Vi du doc va ghi tu tep inputfile sang tep outputfile filelength=`wc -l $inputfile | cut -c1-8` filelength=`expr $filelength + 0` while $filelength do line=`tail -$filelength $inputfile | head -1` words=`s_count_args $line` echo "$line = $words words" >> $outputfile file_length=`expr $filelength - 1` done Trong v d trn c s dng mt vi k thut nh c phn cui ca tp u vo (vi lnh tail)sau li ly dng u ca on (vi lnh head -1) ly ra c tng dng t trn xung ca tp u vo. Hai tham s -c1-8 ca lnh wc dng b phn tn tp trong kt qu lit k s dng trong tp . Ngoi ra, cc bn c th dng lnh: Cat >> <tn tp> <<-EOA to ra mt tp trc vi tn trong <tn tp>.

8. Tm hiu lnh test


test l l lnh kim tra s tn ti ca cc tp, th mc v so snh bin s. Cu trc ca lnh test nh sau: test <iu kin> hoc [<iu kin>]

VietHung soft group -

viethung_group@email.com

C cc tham s nh sau: iu kin vi cc tp, th mc: -f <tn> s tn ti ca tp thng thng. -d <tn> s tn ti ca th mc. -c <tn> s tn ti tp dng k t. -r <tn> s tn ti v c th c c. -s <tn> s tn ti v c kch thc ln hn 0. -w <tn> s tn ti v c th ghi c. -x <tn> s tn ti v c th chy c. iu kin vi cc xu k t: -n s1 xu s1 c di ln hn 0. -z s1 xu s1 c di bng 0. s1 = s2 hai xu s1 v s2 ging nhau. s1 != s2 hai xu s1 v s2 khng ging nhau. s1 < s2 xu s1 ng trc xu s2 theo th t ca bng m ASCII. s1 > s2 xu s1 ng sau xu s2 theo th t ca bng m ASCII. string bin string khng rng (not null). iu kin vi cc s: n1 -eq n2 so snh bng. n1 -ge n2 so snh ln hn hoc bng. n1 -gt n2 so snh ln hn. n1 -le n2 so snh nh hn hoc bng. n1 -lt n2 so snh nh hn. n1 -ne n2 so snh khng bng. V d: if test $# -gt 0 if [ -n $1 ] if [ $count -lt 5 ]

nu c tham s nu tham s khc trng nu gi tr ca bin count nh hn 5

Ph lc
Danh sch php ton, lnh v tham s m trng
$0 $1 $2 ... $# $@ $$ tn tp ang c thc hin tham s th nht tham s th hai s tham s lit k tt c cc tham s ch s ca process

VietHung soft group -

viethung_group@email.com

$+ * / % == != < > >= <= || &&

c (flag) php cng php tr php nhn php chia php ly phn d so snh bng so snh khng bng so snh nh hn so snh ln hn so snh ln hn hoc bng so snh nh hn hoc bng hoc v

$USER tn ngi s dng hin ti echo read in ra mn hnh c t bn phm th mc chnh ca ngi dng hin ti

$HOME/$home $dir $path

VietHung soft group -

viethung_group@email.com