Lap Trình Shells
Transcript of Lap Trình Shells
-
8/17/2019 Lap Trình Shells
1/114
LẬP TRÌNH SHELL
-
8/17/2019 Lap Trình Shells
2/114
Nội dung
-
8/17/2019 Lap Trình Shells
3/114
-
8/17/2019 Lap Trình Shells
4/114
Đặc trưng của script
-
8/17/2019 Lap Trình Shells
5/114
Cấu trúc shell-script
-
8/17/2019 Lap Trình Shells
6/114
-
8/17/2019 Lap Trình Shells
7/114
Review commands
-
8/17/2019 Lap Trình Shells
8/114
Parameter & Variables
ParameterEntity that stores values
3 typepositional parameters
arguments present on command linereferenced by number
special parametersset by shell to store info about aspects of current state
Variableidentified by a name .Contain values
-
8/17/2019 Lap Trình Shells
9/114
-
8/17/2019 Lap Trình Shells
10/114
Giá trị default của tham số
-
8/17/2019 Lap Trình Shells
11/114
Shift
-
8/17/2019 Lap Trình Shells
12/114
Shift
-
8/17/2019 Lap Trình Shells
13/114
Khác nhau giữa * và @
-
8/17/2019 Lap Trình Shells
14/114
Biến tiền định nghĩa
-
8/17/2019 Lap Trình Shells
15/114
Lệnh nội trú
-
8/17/2019 Lap Trình Shells
16/114
Echo
-
8/17/2019 Lap Trình Shells
17/114
Echo – ví dụ
-
8/17/2019 Lap Trình Shells
18/114
Example for ouput’s format
-
8/17/2019 Lap Trình Shells
19/114
Result
-
8/17/2019 Lap Trình Shells
20/114
Read
-
8/17/2019 Lap Trình Shells
21/114
Command Substitution
-
8/17/2019 Lap Trình Shells
22/114
Exit Status
-
8/17/2019 Lap Trình Shells
23/114
Cấu trúc If
-
8/17/2019 Lap Trình Shells
24/114
If – ví dụ
-
8/17/2019 Lap Trình Shells
25/114
If ví dụ
-
8/17/2019 Lap Trình Shells
26/114
Cấu trúc case
-
8/17/2019 Lap Trình Shells
27/114
Test
-
8/17/2019 Lap Trình Shells
28/114
Example
-
8/17/2019 Lap Trình Shells
29/114
Test
-
8/17/2019 Lap Trình Shells
30/114
Example
-
8/17/2019 Lap Trình Shells
31/114
Test
-
8/17/2019 Lap Trình Shells
32/114
Test
-
8/17/2019 Lap Trình Shells
33/114
Test ví dụ
-
8/17/2019 Lap Trình Shells
34/114
Cấu trúc lặp
-
8/17/2019 Lap Trình Shells
35/114
Cấu trúc lặp – ví dụ
-
8/17/2019 Lap Trình Shells
36/114
Cấu trúc lặp – ví dụ
-
8/17/2019 Lap Trình Shells
37/114
Cấu trúc lặp – ví dụ
-
8/17/2019 Lap Trình Shells
38/114
Break
-
8/17/2019 Lap Trình Shells
39/114
continue
-
8/17/2019 Lap Trình Shells
40/114
Định hướng với if
-
8/17/2019 Lap Trình Shells
41/114
Arrays
Integer-Indexed Arraysmembers of an array variable are assigned
and accessed with a subscript of form [N]first element has an index of 0
Displaying Arrays
-
8/17/2019 Lap Trình Shells
42/114
Arrays
* expands to a single parameter ifquoted@ as subscript and quoting expansion
-
8/17/2019 Lap Trình Shells
43/114
Array
-
8/17/2019 Lap Trình Shells
44/114
Assigning Array Elements
-
8/17/2019 Lap Trình Shells
45/114
next unassigned element
-
8/17/2019 Lap Trình Shells
46/114
entire array with a single cmd
-
8/17/2019 Lap Trình Shells
47/114
+= used to append values toend of indexed array
-
8/17/2019 Lap Trình Shells
48/114
Associative Arrays
use strings as subscriptsmust be declared before being used
-
8/17/2019 Lap Trình Shells
49/114
Hàm
-
8/17/2019 Lap Trình Shells
50/114
Check Argument for ValidDotted-Quad IP Address
-
8/17/2019 Lap Trình Shells
51/114
-
8/17/2019 Lap Trình Shells
52/114
Gọi hàm
-
8/17/2019 Lap Trình Shells
53/114
Thực hiện
-
8/17/2019 Lap Trình Shells
54/114
Test isvalidip
-
8/17/2019 Lap Trình Shells
55/114
Compound Commands
list of commands enclosed in ( … ) or … } expressions enclosed in (( … )) [[ …block-level shell keywords (case, for,select, while, until)
-
8/17/2019 Lap Trình Shells
56/114
Example
-
8/17/2019 Lap Trình Shells
57/114
function’s body wrapped in ()
is executed in a subshell,
changes made during its execution do notremain in effect after it exits
-
8/17/2019 Lap Trình Shells
58/114
Return
-
8/17/2019 Lap Trình Shells
59/114
Exit
-
8/17/2019 Lap Trình Shells
60/114
Function Libraries
-
8/17/2019 Lap Trình Shells
61/114
Debug
-
8/17/2019 Lap Trình Shells
62/114
Set
-
8/17/2019 Lap Trình Shells
63/114
variables are used by shell
-
8/17/2019 Lap Trình Shells
64/114
variables are set by shell
-
8/17/2019 Lap Trình Shells
65/114
Sample Scriptfour functions: die, usage, version, andreadline
readline function will differ according towhich shell you are usingcreates a basic web page, completewith title & primary headline ()adline function uses options to thebuiltin command read
-
8/17/2019 Lap Trình Shells
66/114
-
8/17/2019 Lap Trình Shells
67/114
-
8/17/2019 Lap Trình Shells
68/114
-
8/17/2019 Lap Trình Shells
69/114
-
8/17/2019 Lap Trình Shells
70/114
-
8/17/2019 Lap Trình Shells
71/114
-
8/17/2019 Lap Trình Shells
72/114
Lập trình Shell- Ứng dụng Tên
Quản lý các đĩa CD nhạc
Yêu cầu chức năng Chèn CD nhạc mới Tạo bài hát mới Sửa đổi thông tin về bài hát Xóa bài hátLiệt kê danh mục bài hát
-
8/17/2019 Lap Trình Shells
73/114
Lập trình Shell- Ứng dụng Thiết kế
CSDL
Dùng các tệp TEXT để lưu dữ liệu Gồm Tệp lưu thông tin về CD (ID, tên, kiểu, NSX,..) Tệp lưu thông tin bài hát(CD’s ID, tên, track,tác gi
ca sĩ) Tổ chức Dùng các ký tự CVS phân cách các cột
-
8/17/2019 Lap Trình Shells
74/114
Lập trình Shell- Ứng dụng Thiết kế
Các modulesGiao diện
Menu: dạng text (dùng echo, read) Các hàm cho các chức năng xử lý CD
Thêm, Xóa CDTìm CD, Đếm số CD
Cập nhật thông tin CD Các hàm cho các chức năng xử lý bài hát Thêm, Xóa, Cập nhật thông tin bài bát Liệt kê, Tìm các bài hát hiện có
-
8/17/2019 Lap Trình Shells
75/114
Ứng dụng – Cài đặt Các biến toàn cục
Menu_choice=“”
Current_CD=“” Title_file = “title.cdb” Track_file= “tracks.cdb”
Temp_file= “/tmp/cdb.$$” Trap “rm –f $temp_file” EXIT
-
8/17/2019 Lap Trình Shells
76/114
Ứng dụng – Cài đặt MenuSet_menu_choice(){
Echo “CD App” Echo “ a. Add new Cd” Echo “ f. Find Cd” Echo “ c. Count Cd” If [$cdcatnum != “”] then
Echo “l. List tracks of $cdtitle” Echo “r. remove $cdtitle” Echo “u. Update $cdtitle”
FiEcho “q. quit” Read menu_choiceReturn
}
-
8/17/2019 Lap Trình Shells
77/114
Ứng dụng – Cài đặt CD
Insert_Title (){
Echo $* >> $title_file}Insert_Track (){
Echo $* >> $tracks_file}
Add_Record_Tracks (){
echo “enter info for track. Q for quit” cdtrack = 1
cdtitle = 1while [“$cdtitle” != “q”]
doecho –e “Track $cdtrack , title\ c” Read tmpcdtitle = ${tmp%%,*}if [“$tmp” != $cdtitte] then
echo “Format invalid” continue
fiif [-n $cdtitle] then
if [“$cdtitle” != ‘q’] then insert_track $cdcatnum, $cdtrack,
$cdtitlefielse
cdtrack; = $((cdtrack -1))ficdtrack = $((cdtrack +1))
done}
-
8/17/2019 Lap Trình Shells
78/114
Ứng dụng – Cài đặt CD Add_record (){
# Create a CD# Input# ID’s CD # Title, Type, Author# Call insert_title,add_record_tracks
}
find_cd (){
# dùng grep tìm tên trong
tệp title.cdb }
-
8/17/2019 Lap Trình Shells
79/114
Ứng dụng – Cài đặt CDUpdate_CD (){
# tìm CD cần update # Xóa và tạo mới lineCD
}
remove_record (){
# loại bỏ title CD và bàihát khỏi tệp dữ liệu
}
-
8/17/2019 Lap Trình Shells
80/114
Ứng dụng – Cài đặt Main
Rm –f $temp_fileIf [! –f $title_file]; then
touche $title_fileFiIf [! –f $track_file]; then
touche $tracks_fileFiQuit = n
while [“$quit” != “y”] Do
set_menu_choice
case “$menu_choice” in a. add_record;;r. remove_record;;….
esacDoneRm –f $temp_fileExit 0
-
8/17/2019 Lap Trình Shells
81/114
File Operations & Commands
how shell works with filesshell options that
modify & extend file name expansionread and modify contents of files.
Several external cmd that work on files
-
8/17/2019 Lap Trình Shells
82/114
Reading a File
-
8/17/2019 Lap Trình Shells
83/114
kjv.txt
1(more) variable be supplied
-
8/17/2019 Lap Trình Shells
84/114
1(more) variable be suppliedas args to read
-
8/17/2019 Lap Trình Shells
85/114
Word splitting
-
8/17/2019 Lap Trình Shells
86/114
Linux Loader – lilo.conf
-
8/17/2019 Lap Trình Shells
87/114
Result
-
8/17/2019 Lap Trình Shells
88/114
sedreplacing a string or pattern with anotherstring
pulling particular line or range of lines fromfile.
-
8/17/2019 Lap Trình Shells
89/114
examples
-
8/17/2019 Lap Trình Shells
90/114
modifying a file
-
8/17/2019 Lap Trình Shells
91/114
File Name Expansion Options
-
8/17/2019 Lap Trình Shells
92/114
File Name Expansion Options
six shell options that affect way inwhich file names are expanded.
shopt command-s enable-u disable
-
8/17/2019 Lap Trình Shells
93/114
Ex: various globbing options
-
8/17/2019 Lap Trình Shells
94/114
nullglob
-
8/17/2019 Lap Trình Shells
95/114
failglob
-
8/17/2019 Lap Trình Shells
96/114
dotglob
-
8/17/2019 Lap Trình Shells
97/114
extglobfive new file name expansion operators areadded
pattern-list is a list of pipe-separated globbingpatternsparentheses, which are preceded by ?, *, +, @, or!
Exremove existing files in $HOME/globfest, create anew set
-
8/17/2019 Lap Trình Shells
98/114
Example
-
8/17/2019 Lap Trình Shells
99/114
-
8/17/2019 Lap Trình Shells
100/114
-
8/17/2019 Lap Trình Shells
101/114
-
8/17/2019 Lap Trình Shells
102/114
-
8/17/2019 Lap Trình Shells
103/114
nocaseglob
-
8/17/2019 Lap Trình Shells
104/114
-
8/17/2019 Lap Trình Shells
105/114
globstarallows use of ** to descend recursively intodirectories &subdirectories looking for matchingfiles
-
8/17/2019 Lap Trình Shells
106/114
review
Using script for automating
-
8/17/2019 Lap Trình Shells
107/114
g p gadmin
Create Commands That Affect MultipleSystems
Simple Multi-SSHsimple shell script that executes specifiedcommand on every machine listed inconfiguration file
Group-Based Multi-SSH
-
8/17/2019 Lap Trình Shells
108/114
Simple Multi-SSH
-
8/17/2019 Lap Trình Shells
109/114
Group-Based Multi-SSHrun a command on a certain subset ofmachines
instead of being forced to run it on everymachine in every circumstance
-
8/17/2019 Lap Trình Shells
110/114
-
8/17/2019 Lap Trình Shells
111/114
-
8/17/2019 Lap Trình Shells
112/114
Copying Filescopies one or more local files, recursively, toa given directory on multiple machines.first arg
local file, or a list of local files contained withinquotes.
second arg
a destination directory.Following the file(s) and destination directory isone or more group names.
-
8/17/2019 Lap Trình Shells
113/114
-
8/17/2019 Lap Trình Shells
114/114