You are on page 1of 3

Bi 1

Mt s bi tp lp trnh shell
Nguyn Hng Phng Email: phuongnh@it-hut.edu.vn Site: http://is.hut.vn/~phuongnh B mn Cc h thng thng tin Khoa Cng ngh thng tin i hc Bch Khoa H Ni
1

bi: Vit file cho hin th li cho thch hp:


t < 12h: Good morning 12h t 18h: Good afternoon t > 18h: Good evening

phuongnh@it-hut.edu.vn

Bi 1
Li gii: file chao.sh
t=`date + %H` if [ $t -lt 12 ] then echo "Good morning" elif [ $t -lt 18 ] then echo "Good afternoon" else echo "Good evening" fi
phuongnh@it-hut.edu.vn 3

Bi 2
bi: dng lnh case vit li file chao.sh trn y.

phuongnh@it-hut.edu.vn

Bi 2
Nhc li:
$date Web Feb 18 11:10:20 2004

Bi 3
bi: To mt menu n gin
1. 2. 3. 4. Hin th ngy thng v thi gian Hin th mc hin ti Cho bit ai ang trong h thng Thot

Li gii:
set `date` case $4 in 0[0-9]*|1[0-1]*) echo "Good morning";; 1[2-7]*) echo "Good afternoon";; *) echo "Good evening";; esac
phuongnh@it-hut.edu.vn 5

phuongnh@it-hut.edu.vn

Bi 3
Li gii:
echo "1. Hien thi ngay thang va thoi gian" echo "2. Hien thu muc hien tai" echo "3. Cho biet ai dang trong he thong" echo "4. Thoat" while : do echo "An 1, 2, 3, 4 de chon" read ch case $ch in 1) date;; 2) pwd;; 3) who;; 4) exit;; *) echo "Chon sai";; esac done

Bi 4
bi: Hin cc file vn bn trong mt th mc m tn th mc a vo t dng lnh, nu khng a vo th c tn th mc t bn phm

phuongnh@it-hut.edu.vn

phuongnh@it-hut.edu.vn

Bi 4
Li gii:
if [ "$1"="" ] then echo "Nhap vao ten thu muc:" read ten else ten=$1 C th thay fi bng * cd $ten for b in `ls` do file $b|grep text done
phuongnh@it-hut.edu.vn 9

Bi 5
bi: To mt menu gm cc mc:
1. Xa file 2. Hin ngy thng 3. Thot

Nu chn 1 th nhp tn file cn xa t bn phm, kim tra nu tn ti file th xa (nn vit chng trnh con)

phuongnh@it-hut.edu.vn

10

Bi 5
Li gii: file menu.sh
xoafile() { echo "Cho ten file" read ten if [ -f $ten ] then rm $ten else echo "$ten khong ton tai" fi } while true do echo "1. Xoa file" echo "2. Hien ngay thang" echo "3. Thoat" echo "An 1, 2, 3 de chon" read ch case $ch in 1)xoafile;; 2)date;; 3)exit;; esac done
11

Bi tp v nh
Bi 1:Vit chng trnh shell nhp vo mt s nguyn n t bn phm v a ra tng cc s t 1 n n. Bi 2: Vit chng trnh shell tnh v hin th tng cc s chn t 1 n 200. Bi 3: Vit chng trnh shell nhp cc s nguyn vo t bn phm v a ra s ln xut hin s l. Vic nhp kt thc khi nhp vo xu rng
phuongnh@it-hut.edu.vn 12

phuongnh@it-hut.edu.vn

Hi - p

Li hay p

phuongnh@it-hut.edu.vn

13

phuongnh@it-hut.edu.vn

14