1. Giới thiệu
Shell script là gì
Câu lệnh shell là một chương trình máy tính được thiết kế để chạy hoặc thực thi bởi Unix shell bởi một trình thông dịch theo dòng lệnh.
1 2 | <span class="token keyword">echo</span> Hello World |
Tập hợp các câu lệnh shell được viết trong 1 file có đuôi .sh
là Shell Scripts, chẳng hạn với nội dung file có tên là first.sh
:
1 2 3 4 | <span class="token shebang important">#!/bin/sh</span> <span class="token comment"># This is a comment!</span> <span class="token keyword">echo</span> Hello World <span class="token comment"># This is a comment, too!</span> |
#!/bin/sh
luôn được viết ở dòng đầu tiên của file, không phải comment, mà là khai báo cho trình tương tác shell của hệ điều hành biết được đây là 1 chương trình shell.- Trong một số trường hợp để thực thi file
first.sh
cần phải cấp quyền thực thi nội dung cho nóchmod 755 first.sh
(chmod là lệnh của unix) - chuỗi
"Hello World"
chính là đối số, câu lệnh shellecho
được dùng để in ra 1 chuỗi truyền vào.
=> Thay vì chạy dòng lệnh shell ngay trên trình terminal(unix) :
1 2 3 4 | $ <span class="token keyword">echo</span> Hello World Hello World $ |
=> Ta có thể thực thi các dòng lệnh bên trong của cả 1 file first.sh
:
1 2 3 4 5 | $ chmod 755 first.sh $ ./first.sh Hello World $ |
Ưu Điểm
- Tự động hóa các hoạt động được thực hiện thường xuyên.
- Chạy chuỗi lệnh dưới dạng một lệnh.
- Nó có thể được thực thi trong mọi hệ điều hành giống với Unix mà không cần sửa đổi gì.
Nhược điểm
- Tốc độ thực thi chậm so với bất kỳ ngôn ngữ lập trình nào
- Một tiến trình mới được khởi chạy cho hầu hết mọi lệnh shell được thực thi.
2. Sử dụng biến
chuỗi truyền vào trong ví dụ trên có thể được thay bởi biến được khai báo:
1 2 3 4 | <span class="token shebang important">#!/bin/sh</span> MY_MESSAGE<span class="token operator">=</span><span class="token string">"Hello World"</span> <span class="token keyword">echo</span> <span class="token variable">$MY_MESSAGE</span> |
MY_MESSAGE
là tên biến tùy bạn đặt, miễn là format VIẾT_HOA kết hợp với gạch dưới- Trong khi echo chấp nhận nhiều đối số truyền vào (không cần dùng
"
), một biến chỉ có thể lưu 1 giá trị chuỗi, do đó cần bao bên trong cặp dấu nháy kép"Hello World"
.
Cũng có thể tương tác đặt giá trị biến nhập vào từ bàn phím bằng lệnh read
:
1 2 3 4 5 | <span class="token shebang important">#!/bin/sh</span> <span class="token keyword">echo</span> What is your name? <span class="token function">read</span> MY_NAME <span class="token keyword">echo</span> <span class="token string">"Hello <span class="token variable">$MY_NAME</span> - hope you're well."</span> |
Một số tham số dòng lệnh hay dùng:
$0
: Tên của file script.$1
->$9
: Các tham số truyền vào$#
: Số lượng của tham số truyền vào$*
: Danh sách các tham số được truyền vào
(các trường hợp$#
và$*
sẽ không bao gồm$0
)
1 2 3 4 5 6 7 | <span class="token shebang important">#!/bin/sh</span> <span class="token keyword">echo</span> <span class="token string">"Hello! I have <span class="token variable">$#</span> parameters"</span> <span class="token keyword">echo</span> <span class="token string">"My file name is <span class="token variable">$0</span>"</span> <span class="token keyword">echo</span> <span class="token string">"First parameter is <span class="token variable">$1</span>"</span> <span class="token keyword">echo</span> <span class="token string">"Second parameter is <span class="token variable">$2</span>"</span> <span class="token keyword">echo</span> <span class="token string">"All parameters are <span class="token variable"><a href="/cdn-cgi/l/email-protection" class="__cf_email__">[email protected]</a></span>"</span> |
Phạm vi sử dụng biến cũng cần được quan tâm:
Biến chưa được khai báo sẽ có giá trị rỗng (vd: myvar2.sh
):
1 2 3 4 5 6 7 | <span class="token shebang important">#!/bin/sh</span> <span class="token keyword">echo</span> <span class="token string">"MYVAR is: <span class="token variable">$MYVAR</span> MYVAR="</span>hi there<span class="token string">" echo "</span>MYVAR is: <span class="token variable">$MYVAR</span>" |
=> kết quả là:
1 2 3 4 | ./myvar2.sh MYVAR is: MYVAR is: hi there |
Sẽ không thể trực tiếp sử dụng biến khai bao từ bên ngoài file:
1 2 3 4 5 | $ MYVAR<span class="token operator">=</span>hello $ ./myvar2.sh MYVAR is: MYVAR is: hi there |
Trừ phi sử dụng export
1 2 3 4 5 | $ <span class="token function">export</span> MYVAR $ ./myvar2.sh MYVAR is: hello MYVAR is: hi there |
Cũng không thể trực tiếp sử dụng biến bên trong file ở bên ngoài:
1 2 3 4 5 6 7 | $ <span class="token function">bash</span> myvar2.sh MYVAR is: MYVAR is: hi there $ <span class="token keyword">echo</span> <span class="token variable">$MYVAR</span> $ |
=> khi shell script được chạy xong, môi trường của nó sẽ bị phá hủy (có nghĩa là khi gọi biến trong tập lệnh shell ra bên ngoài ví dụ như terminal, biến trong shell đó sẽ là rỗng)
Vì vậy cũng cần sử dụng export MYVAR
bên trong file, tuy nhiên ta còn có thể sử dụng .
:
1 2 3 4 5 6 7 8 9 | $ MYVAR<span class="token operator">=</span>hello $ <span class="token keyword">echo</span> <span class="token variable">$MYVAR</span> hello $ <span class="token keyword">.</span> ./myvar2.sh MYVAR is: hello MYVAR is: hi there $ <span class="token keyword">echo</span> <span class="token variable">$MYVAR</span> hi there |
sử dụng
.
ta sẽ không cầnexport
biến ở cả trong và ngoài file
Từ đây bạn có thể hiểu nguyên lý sử dụng biến môi trường trong các file.profile
hay.bash_profile
- Bài viết được tham khảo từ Shell Scripting Tutorial.