Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 33 trang
THÔNG TIN TÀI LIỆU
Cấu trúc
Chapter 10: BASH Shell Scripting
In this chapter …
Control structure tests
test expressions
test expressions con’t
Other test-commands
if … then structure
if … then … else structure
if … then … elif structure
if … then … elif con’t
case structure
case structure con’t
for … in structure
for structure
while structure
until structure
break and continue
select structure
select structure con’t
File descriptors
Array Variables
Variable Scope
Special Parameters
Null and unset variables
Functions
Here document
type
read
read con’t
getopts
getopts con’t
Slide 32
Misc
Nội dung
Chapter 10: BASH Shell Scripting Fun with fi In this chapter … • Control structures • File descriptors • Variables Control structure tests • Control structures depend on a test that equates either true or false • The test builtin in bash allows logical, relational, and file property-based tests • Syntax: test expression OR [ expression ] test expressions • If expression is true, test returns 0; if false, it returns not (usually 1) • Comparing text strings string1 = string2 string1 != string2 • Comparing numbers num1 –OP num2 – Where OP can be eq, ne, lt, gt, le, ge test expressions con’t • File tests -option filename where option can be: d : file is a directory y : file exists f : file is a regular file Plus many more (check man bash) Other test-commands • Instead of test and [ ] you can use other bash contructs • ((expression)) can be used for integer comparisons • [[expression]] can be used for logical expressions and string comparisons • See pages 505-506 for complete list if … then structure • Syntax: if test-command then commands fi • test-command must evaluate true or false • commands can be zero or more lines if … then … else structure • Syntax: if test-command then commands else commands fi • Same guidelines as if…then if … then … elif structure • Syntax: if test-command then commands elif test-command then commands … else commands fi if … then … elif con’t • You can have one or more elif blocks • Remember, each elif line is following by a then statement • Rather than multiple elif’s, might try a case statement instead select structure con’t • select structure displays a numbered menu allowing user to select an arg • After displaying the menu, select displays the PS3 prompt – by default it’s #? • Set PS3 to customize the prompt to something more intelligible • The user’s selection (what was actually typed) is stored in REPLY File descriptors • Recall 0, 2> … now let’s make more • Syntax: exec n> outfile AND exec m< infile • exec associates streams with files • Then can treat those streams just like the standard ones • To close: exec n>&- AND exec m – the shift builtin rotates through the arguments Null and unset variables • ${varname:-default} : if varname is not set or is null, substitutes for default • ${varname:=default} : if varname is not set or null, substitues for default and sets varname • ${varname:?message} : if varname is not set, displays an error Functions • Syntax: function name () { … } • Note on scope – functions have same scope as calling script/shell … which means you can access (or step on!) existing variables Here document • Allows you to standard input redirection within a script • Denoted by