M ụ c tiêuM ụ c tiêu Vai trò của Linux Shell Quá trình thông dịch lệnh Shell Khởi tạo biến Shell Biến môi trường Shell Biến môi trường Shell Viết Shell Script... $ echo "This is an examp
Trang 1Linux Shell Script
Âu B ửu Long
Trang 2M ụ c tiêu
M ụ c tiêu
Vai trò của Linux Shell
Quá trình thông dịch lệnh Shell
Khởi tạo biến Shell
Biến môi trường Shell
Biến môi trường Shell
Viết Shell Script
Trang 6$ echo "This is an example of the echo command" ↵
This is an example of the echo command
$ _
Trang 7Th ự c thi Shell Script
Th ự c thi Shell Script
File script cần có quyền thực thi.
Ví d ụ với file script.sh
chmod u+x script.sh script.sh
Trang 8Kh ở i t ạ o bi ế n
Kh ở i t ạ o bi ế n
Biến Shell Script:
◦ Không c ần khai báo kiểu.
◦ Được xem nghiêm ngặt là chuỗi
◦ Có th ể tính toán được.
◦ Có th ể tính toán được.
◦ Không c ần khai báo tường minh.
◦ T ự cấp phát sau lần truy xuất đầu tiên.
Phép gán: <Tên Biến>=GiáTrị
Khi script kết thúc: Biến được hủy
Trang 10$ bash [Creates a new shell]
$ echo "$continent" [There is no response]
$ continent=Asia [Gives new value Asia to continent]
$ echo "$continent"
Asia
Press <Ctrl> d
$ exit
Trang 11Các bi ế n môi tr ườ ng
Các bi ế n môi tr ườ ng
HOME: Thư mục home của user
PATH: Chứa các biến đường dẫnPS1: Ký hiệu dấu nhắc ($)
Trang 12L ệnh test và [] dùng để ước lượng biểu
L ệnh test và [] dùng để ước lượng biểu
Trang 13L ệnh if…elif
if condition(s)
then command(s) elif condition
then command(s) else command(s) fi
Trang 14fi
Trang 15… command;;
*) command;;
esac
Trang 16echo –n "Enter file name?"
read fname cat ${fname}
echo –n "wish to see more files :" read reply
done
Trang 18Câu l ệ nh break và continue
Câu l ệ nh break và continue
Ví d ụ :
while true do
echo "Enter choice"
echo "(press 'q' to exit)"
echo "1 date 2 who"
echo "3 ls 4 pwd"
read choice case $choice in
Trang 19Câu l ệ nh test
Câu l ệ nh test
• Câu lệnh test dùng kiểm tra file và thư mục
echo –n "Enter file name: "
read fname
if test -f $fname
then echo "$fname is an ordinary file"
elif test -d $fname
then echo "$fname is a directory file"
elif test -s $fname
then echo "$fname is a not an empty file"
elif test ! –r "$fname"
then echo "No readable file called
$fname exists"
fi
Trang 20• An example of a function is shown below:
function ftype {
if test -f $fname then echo "$fname is an ordinary file."
elif test -d $fname then echo "$fname is a directory file."
elif test ! -r "$fname"
then echo "No readable file called $fname
Hàm trong Shell Script
then echo "No readable file called $fname
exists"
fi }
echo "Enter a file name"
read fname ftype
function
<function name> [ arg1 arg2 ]
Trang 21Kiểm tra chuỗi đối xứng
echo "Enter string"
break fi
ctr=`expr $ctr + 1`
len=`expr $len - 1`
done
if test $flag -eq 0
then echo "String is palindrome" else echo "String not a palindrome" fi
Trang 23Bài t ậ p th ự c hành
Bài t ậ p th ự c hành
1. Viết shellscript liệt kê quyền hạn và
tên tất cả các file trong một thư mục
Đường dẫn thư mục được nhập và
thông qua tham số script
thông qua tham số script
2. Viết shellscript xuất tất cả các số
nguyên tố trong một đoạn ra màn
hình