Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )
-
Upload
scarlet-chaney -
Category
Documents
-
view
25 -
download
0
description
Transcript of Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )
![Page 1: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/1.jpg)
Unix Programming Unix Programming EnvironmentEnvironment
Part-4 Shell Programming
Prepared by Xu Zhenya( [email protected] )
Draft – Xu Zhenya( 2002/10/01 )
Rev 1.0 – Xu Zhenya( 2002/10/07 )
![Page 2: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/2.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
AgendaAgenda 1. Shell Features and Built-in Commands ( 2 )1. Shell Features and Built-in Commands ( 2 )
Loops and Branches, Function, Trap and setLoops and Branches, Function, Trap and set
2. Examples 2. Examples
Chapter 5Chapter 5
![Page 3: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/3.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
Shell Features - Conditional Constructs (1)Shell Features - Conditional Constructs (1)
1. “ if “1. “ if “
syntax: syntax:
if if commands_listcommands_list; then ; then
consequent-commandsconsequent-commands; ;
[elif [elif more-commandsmore-commands; then ; then
more-consequentsmore-consequents;] ;]
[else [else alternate-consequentsalternate-consequents;] ;]
fi fi
The The test-commandstest-commands list is executed, and if list is executed, and if its return status is zeroits return status is zero, , the the consequent-commandsconsequent-commands list is executed. list is executed.
![Page 4: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/4.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
Shell Features - Conditional Constructs (2)Shell Features - Conditional Constructs (2) 2. “ case ”2. “ case ”
Syntax: Syntax:
case case wordword in in
[ [(] [ [(] patternpattern [| [| patternpattern]...) ]...)
command-listcommand-list ;;] ;;]
... ...
esac esac
Notes:
the shell equivalent of switch in C/C++.
The The `|'`|' is used to separate multiple patterns, and the is used to separate multiple patterns, and the `)'`)' operator terminates operator terminates a pattern list. A list of patterns and an associated command-list is known a pattern list. A list of patterns and an associated command-list is known as a as a clauseclause. Each clause must be terminated with . Each clause must be terminated with `;;'`;;'
The The wordword and each and each patternpattern undergo tilde expansion, parameter expansion, undergo tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal before command substitution, arithmetic expansion, and quote removal before matching is attempted. matching is attempted.
![Page 5: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/5.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
Shell Features - Conditional Constructs (3)Shell Features - Conditional Constructs (3)
3. “ select “ ( only in Bash )3. “ select “ ( only in Bash )
Syntax:Syntax:
select name [in words ...]; do commands; doneselect name [in words ...]; do commands; done
Notes: Notes:
The select construct allows the easy generation of menus. The select construct allows the easy generation of menus.
If the If the `in words'`in words' is omitted, the positional parameters are printed, as is omitted, the positional parameters are printed, as if `in "$@"' had been specifed. if `in "$@"' had been specifed.
The The PS3PS3 prompt is then displayed and a line is read from the prompt is then displayed and a line is read from the standard input. standard input.
The line read is saved in the variable The line read is saved in the variable REPLYREPLY. .
The The commandscommands are executed after each selection until a are executed after each selection until a breakbreak or or returnreturn command is executed, at which point the select command command is executed, at which point the select command completes. completes.
![Page 6: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/6.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
Shell Features - Loop Constructs (1)Shell Features - Loop Constructs (1)
1. “ for “1. “ for “
Syntax: Syntax:
for for namename [in [in wordswords ...]; do ...]; do commandscommands; done ; done
Notes:Notes:
Expand Expand wordswords, and execute , and execute commandscommands once for each member in once for each member in the resultant list, with the resultant list, with namename bound to the current member. bound to the current member.
If `in If `in wordswords' is not present, `in "' is not present, `in "$@$@"' is assumed. "' is assumed.
![Page 7: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/7.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
Shell Features - Loop Constructs (2)Shell Features - Loop Constructs (2)
2. “ while “2. “ while “
Syntax: Syntax:
while commands_list; do consequent-commands; donewhile commands_list; do consequent-commands; done
Notes:Notes:
Execute consequent-commands as long as Execute consequent-commands as long as commands_list has commands_list has an exit status of zeroan exit status of zero. .
![Page 8: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/8.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
Shell Features - Loop Constructs (3)Shell Features - Loop Constructs (3)
3. “ until “3. “ until “
Syntax: Syntax:
until until test-commandstest-commands; do ; do consequent-commandsconsequent-commands; done; done
Notes:Notes:
Execute Execute consequent-commandsconsequent-commands as long as as long as test-commandstest-commands has an has an exit status which is not zero. exit status which is not zero.
![Page 9: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/9.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
Shell Features – test commandShell Features – test command
1. the exit status1. the exit status
Well−behaved UNIX commands, programs, and utilities return a 0 exit Well−behaved UNIX commands, programs, and utilities return a 0 exit code upon successful completion, though there are some exceptions.code upon successful completion, though there are some exceptions.
The exit commandThe exit command
The special parameter: $?, $!The special parameter: $?, $!
2. the test command2. the test command
Syntax Format:Syntax Format:
test test expressionexpression
[[expressionexpression]]
File: -f, -e, -c, -d, File: -f, -e, -c, -d,
String: s1 = s2; s1 != s2; String: s1 = s2; s1 != s2;
Arithmetic: n1 –eq n2; n1 –ge n2; n1 –gt n2Arithmetic: n1 –eq n2; n1 –ge n2; n1 –gt n2
![Page 10: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/10.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
Shell Features - ExamplesShell Features - Examples
Example 1: “case”Example 1: “case”
Example 2: “select”Example 2: “select”
![Page 11: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/11.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
Shell Features - Function (1)Shell Features - Function (1) 1. Syntax: 1. Syntax:
[ function ] [ function ] namename () { () { command-listcommand-list; } ; }
2. Explanation:2. Explanation:
If the function If the function reservedreserved word is supplied, the parentheses are optional. word is supplied, the parentheses are optional.
When a function is executed, the arguments to the function become When a function is executed, the arguments to the function become the positional parametersthe positional parameters during its execution: during its execution:
The special parameter `#' that expands to the number of positional The special parameter `#' that expands to the number of positional parameters is updated to reflect the change.parameters is updated to reflect the change.
Positional parameter 0 is unchanged. Positional parameter 0 is unchanged.
If a numeric argument is given to return, that is the function's return If a numeric argument is given to return, that is the function's return status; otherwise the function's return status is the exit status of the status; otherwise the function's return status is the exit status of the last command executed before the return.last command executed before the return.
Shell scripts normally pass only value parameters to functions.
For a function to return a string or array, use a dedicated variable.
![Page 12: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/12.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
Shell Features - Function (2)Shell Features - Function (2) Variables local to the function may be declared with the Variables local to the function may be declared with the locallocal builtin. builtin.
These variables are visible only to the function and the commands it These variables are visible only to the function and the commands it invokes. invokes.
Functions may be recursive. No limit is placed on the number of Functions may be recursive. No limit is placed on the number of recursive calls.recursive calls.
The largest positive integer a function can return is 256.
A function is essentially a code block, which means its stdin can be redirected.
![Page 13: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/13.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
Shell Features - trapShell Features - trap Textbook, p109 - 110Textbook, p109 - 110
““signal” in Unixsignal” in Unix
trap - Trap Signals trap - Trap Signals
traptrap [ [action condition ...action condition ...]]
If If actionaction is "-", is "-", the shell will reset each the shell will reset each conditioncondition to to the default valuethe default value. .
If If actionaction is null ("), is null ("), the shell will ignore each specified the shell will ignore each specified conditioncondition if it arises. if it arises.
Otherwise, the argument Otherwise, the argument actionaction will be read and executed by the shell when will be read and executed by the shell when one of the corresponding conditions arises. one of the corresponding conditions arises.
The condition can be EXIT, 0 (equivalent to EXIT) or a signal specified using The condition can be EXIT, 0 (equivalent to EXIT) or a signal specified using a a symbolic namesymbolic name, without the SIG prefix, for example, HUP, INT, QUIT, , without the SIG prefix, for example, HUP, INT, QUIT, TERM( Textbook, p109, table5-4 ). TERM( Textbook, p109, table5-4 ).
When a subshell is entered, traps that are not being ignored are set to the When a subshell is entered, traps that are not being ignored are set to the default actionsdefault actions..
![Page 14: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/14.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
Shell Features - setShell Features - set
set - Set or Unset Options and Positional Parameters set - Set or Unset Options and Positional Parameters
If no options or If no options or argumentarguments are specified, s are specified, setset will write the names and will write the names and values of all shell variables.values of all shell variables.
"%s=%s\n", <"%s=%s\n", <namename>, <>, <valuevalue>>
When When argumentarguments are specified, they will cause positional parameters s are specified, they will cause positional parameters to be set or unset. to be set or unset.
set c a b # Set $1, $2 and $3 and set $# to 3 set c a b # Set $1, $2 and $3 and set $# to 3
The command “The command “setset –” without –” without argumentargument will unset all positional will unset all positional parameters and set the special parameter "#" to zero.parameters and set the special parameter "#" to zero.
When options are specified, they will set or unset attributes of the When options are specified, they will set or unset attributes of the shell.shell.
-v-v The shell will The shell will write its input to standard errorwrite its input to standard error as it is read. as it is read.
-x-x The shell will write to standard error The shell will write to standard error a tracea trace for each command for each command after it expands the command and before it executes it. after it expands the command and before it executes it.
![Page 15: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/15.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
Shell Built-in Utilities Shell Built-in Utilities · break - Exit From for, while or until Loop · break - Exit From for, while or until Loop
· continue - Continue for, while or until Loop · continue - Continue for, while or until Loop
· colon - Null Utility, the exit status is zero. · colon - Null Utility, the exit status is zero.
· dot - Execute Commands in Current Environment · dot - Execute Commands in Current Environment
· exec - Execute Commands and Open, Close or Copy File Descriptors · exec - Execute Commands and Open, Close or Copy File Descriptors
· exit - Cause the Shell to Exit · exit - Cause the Shell to Exit
· export - Set Export Attribute for Variables · export - Set Export Attribute for Variables
· return - Return from a Function · return - Return from a Function
· set - Set or Unset Options and Positional Parameters · set - Set or Unset Options and Positional Parameters
· shift - Shift Positional Parameters · shift - Shift Positional Parameters
· times - Write Process Times · times - Write Process Times
· trap - Trap Signals · trap - Trap Signals
· unset - Unset Values and Attributes of Variables and Functions · unset - Unset Values and Attributes of Variables and Functions
· readonly - Set Read-only Attribute for Variables · readonly - Set Read-only Attribute for Variables
. local – set Local Attribute for variables. local – set Local Attribute for variables
![Page 16: Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )](https://reader035.fdocuments.in/reader035/viewer/2022080922/5681303e550346895d95dabe/html5/thumbnails/16.jpg)
Unix Programming EnvironmentUnix Programming Environment Dept. of CSE, BUAADept. of CSE, BUAA
ExamplesExamples
1. cal1. cal
2. which2. which
3. watchwho3. watchwho
4. checkmail4. checkmail
5. overwrite5. overwrite
6. replace 6. replace
7. zap7. zap
8. /etc/rc.d/init.d/smb8. /etc/rc.d/init.d/smb