Lập trình shell
Ngô Văn Công
Overview
Gọi thực hiện lệnh trong Shell Biến trong Shell Câu lệnh alias Tùy biến môi trường Điều khiển công việc Viết chương trình Shell
Invoking the Shell
bash [options] [arguments] Bash related special files
/etc/profile – system initialization file ~/.bash_profile(.bashrc) - personal initialization
file
~/.bash_history - record commands history
Biến trong shell
Sử dụng trong lập trình shell và điều khiển môi
trường
Gán giá trị cho biến: variable_name=value Truy cập vào giá trị của biến: $variable_name
$ foo="hello there" $ echo $foo hello there
export: export variables to the environment or
sub-shells
Environment Variables
HOME - your home directory SHELL - executable program for the current
shell
PATH - paths to locate executable files USER - your username TERM - type of current terminal DISPLAY - your X-Window display PS1 - command prompt
Aliasing Commands
Substituing a string by a word Making commands with simple, short, useful options alias - create or list aliases unalias - remove aliases $ alias
alias cp=’cp -i’ alias ll=’ls -l --color=tty’ alias ls=’ls --color=tty’ alias mv=’mv -i’ alias rm=’rm -i’
$ unalias ll
Customizing Environment
Setting the environment variables Putting them in ~/.bash_profile(.bashrc) for later use
Lập trình shell
Một số thuộc tính Shell Shell script Các biến Biểu thức so sánh Câu lệnh điều khiển Hàm(Functions)
Một số thuộc tính khác
Biểu thức toán học
$ let "a = 1 + 1" $ echo $a 2
Shell Script
Là tập tin văn bản chứa các cấu trúc điều
khiển và các lệnh của Shell
Thông dịch từng hàng trong Shell Cấp quyền chạy(execute) Có thể gọi từ shell script khác Tham số được truyền từ dòng lệnh
First Program
$ cat > myscript.sh
save
chmod +x myscript.sh
myscript.sh
#!/bin/sh echo "Hello, world."
./myscript.sh ./myscript.sh
bash: ./hello.sh: Permission denied Hello, world
"Hello" program $ cat > hello.sh #!/bin/bash # This is a comment: simple hello shell script echo "Enter your name:" read name echo "Hello $name, have a nice day!" ^D
$ ./hello.sh bash: ./hello.sh: Permission denied
$ chmod +x hello.sh $ ./hello.sh Enter your name: Cong Hello Cong, have a nice day!
Các biến
Các biến môi trường Các biến xây dựng sẵn(Built-in variables) Các biến người dùng(User variables) Biến có thể lưu trữ số hay các ký tự Chuỗi kỹ tự phải đặt trong dấu nháy kép hay
nháy đơn
Vị trí các tham số
Các tham số được tham chiếu theo vị trí của nó Các biến có sẵn trong 1 chương trình shell
$# - Số lượng tham số truyền vào $0 - Tên của shell script $* - Trả về tất cả tham số truyền vào $n - Tham chiếu đến tham số thứ n(1-9)
$./myscript source dest
$0 = ./myscript $1 = source $2 = dest
Các biến đặc biệt
$$ - ID của tiến trình hiện tại $? - Trạng thái kết thúc của tiến trình cuối
cùng
Các hàm toán học
Các phép toán: +, -, *, /, % Sử dụng Let khi thực hiện các hàm toán học
let "sum = 4 + 3" let "area = $len * $width" let "percent = $num / 100" let "remain = $n % $d"
Biểu thức so sánh
[ expression ] So sánh chuỗi ký tự: =, !=, -n, -z So sánh số: -eq, -lt, -gt, -ne Phép toán trên tập tin: -d, -f, , -e, -x Phép toán logic: !, -o, -a
[ s1 = s2 ]
[ $num -lt 100 ]
[ -d mydir ]
[ ! -f myfile ]
Câu lệnh điều kiện
IF statement
Example
if [ expression ]; then if [ $1 ="" ]; then
statements
elif [ expression ]; then echo "Enter value:" read num
statements else
else let "num = $1"
statements fi
fi
Câu lệnh điều kiện (cont)
Example
CASE statement case $var in val1)
case $1 in 1)
statements;; echo "One";;
val2) 2)
statements;; echo "Two";;
*) *)
statements;; echo "Unknown";;
esac esac
Câu lệnh lặp
FOR statement for var [in list]; do statements
Example
done
let "sum = 0" for num in 1 2 3 4 5; do let "sum = $sum + $num" done echo $sum
Câu lệnh lặp(cont)
WHILE statement
while [ expression ]; do
statements
done Example
let "num = 0" while [ $num -lt 10 ]; do
echo $num let "num= $num + 2"
done
Một số câu lệnh khác
BREAK - exit a loop CONTINUE - skip to end of current while or for loop EXIT - end the shell script RETURN - return from funtion or shell script
if [ $# -lt 2 ]; then
echo "Usage: ‘basename $0‘ source dest" exit 1
fi
Hàm
Function format
function func_name {
statements
}
Function called
func_name param1 param2 ... Parameters access $1, $2, $3, ...
Gở lỗi (Debugging)
Hiển thị các câu lệnh và tham số khi chạy câu