160 likes | 312 Views
Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa.edu.cn ). Draft – Xu Zhenya( 2002/10/01 ) Rev 1.0 – Xu Zhenya( 2002/10/07 ). Agenda. 1. Shell Features and Built-in Commands ( 2 ) Loops and Branches, Function, Trap and set 2. Examples
E N D
Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa.edu.cn ) Draft – Xu Zhenya( 2002/10/01 ) Rev 1.0 – Xu Zhenya( 2002/10/07 )
Agenda • 1. Shell Features and Built-in Commands ( 2 ) • Loops and Branches, Function, Trap and set • 2. Examples • Chapter 5
Shell Features - Conditional Constructs (1) • 1. “ if “ • syntax: if commands_list; then consequent-commands; [elif more-commands; then more-consequents;] [else alternate-consequents;] fi • The test-commands list is executed, and if its return status is zero, the consequent-commands list is executed.
Shell Features - Conditional Constructs (2) • 2. “ case ” • Syntax: case word in [ [(] pattern [| pattern]...) command-list ;;] ... esac • Notes: • the shell equivalent of switch in C/C++. • The `|' is used to separate multiple patterns, and the `)' operator terminates a pattern list. A list of patterns and an associated command-list is known as a clause. Each clause must be terminated with `;;' • The word and each pattern undergo tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal before matching is attempted.
Shell Features - Conditional Constructs (3) • 3. “ select “ ( only in Bash ) • Syntax: • select name [in words ...]; do commands; done • Notes: • The select construct allows the easy generation of menus. • If the `in words' is omitted, the positional parameters are printed, as if `in "$@"' had been specifed. • The PS3 prompt is then displayed and a line is read from the standard input. • The line read is saved in the variable REPLY. • The commands are executed after each selection until a break or return command is executed, at which point the select command completes.
Shell Features - Loop Constructs (1) • 1. “ for “ • Syntax: • for name [in words ...]; do commands; done • Notes: • Expand words, and execute commands once for each member in the resultant list, with name bound to the current member. • If `in words' is not present, `in "$@"' is assumed.
Shell Features - Loop Constructs (2) • 2. “ while “ • Syntax: • while commands_list; do consequent-commands; done • Notes: • Execute consequent-commands as long as commands_list has an exit status of zero.
Shell Features - Loop Constructs (3) • 3. “ until “ • Syntax: • until test-commands; do consequent-commands; done • Notes: • Execute consequent-commands as long as test-commands has an exit status which is not zero.
Shell Features – test command • 1. the exit status • Well−behaved UNIX commands, programs, and utilities return a 0 exit code upon successful completion, though there are some exceptions. • The exit command • The special parameter: $?, $! • 2. the test command • Syntax Format: • test expression • [expression] • File: -f, -e, -c, -d, • String: s1 = s2; s1 != s2; • Arithmetic: n1 –eq n2; n1 –ge n2; n1 –gt n2
Shell Features - Examples • Example 1: “case” • Example 2: “select”
Shell Features - Function (1) • 1. Syntax: • [ function ] name () { command-list; } • 2. Explanation: • If the function reserved word is supplied, the parentheses are optional. • When a function is executed, the arguments to the function become the positional parameters during its execution: • The special parameter `#' that expands to the number of positional parameters is updated to reflect the change. • Positional parameter 0 is unchanged. • 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 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.
Shell Features - Function (2) • Variables local to the function may be declared with the local builtin. These variables are visible only to the function and the commands it invokes. • Functions may be recursive. No limit is placed on the number of 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.
Shell Features - trap • Textbook, p109 - 110 • “signal” in Unix • trap - Trap Signals • trap [action condition ...] • If action is "-", the shell will reset each condition to the default value. • If action is null ("), the shell will ignore each specified condition if it arises. • Otherwise, the argument action will be read and executed by the shell when one of the corresponding conditions arises. • The condition can be EXIT, 0 (equivalent to EXIT) or a signal specified using a symbolic name, without the SIG prefix, for example, HUP, INT, QUIT, TERM( Textbook, p109, table5-4 ). • When a subshell is entered, traps that are not being ignored are set to the default actions.
Shell Features - set • set - Set or Unset Options and Positional Parameters • If no options or arguments are specified, set will write the names and values of all shell variables. • "%s=%s\n", <name>, <value> • When arguments are specified, they will cause positional parameters to be set or unset. • set c a b # Set $1, $2 and $3 and set $# to 3 • The command “set –” without argument will unset all positional parameters and set the special parameter "#" to zero. • When options are specified, they will set or unset attributes of the shell. • -v The shell will write its input to standard error as it is read. • -x The shell will write to standard error a trace for each command after it expands the command and before it executes it.
Shell Built-in Utilities · break - Exit From for, while or until Loop · continue - Continue for, while or until Loop · colon - Null Utility, the exit status is zero. · dot - Execute Commands in Current Environment · exec - Execute Commands and Open, Close or Copy File Descriptors · exit - Cause the Shell to Exit · export - Set Export Attribute for Variables · return - Return from a Function · set - Set or Unset Options and Positional Parameters · shift - Shift Positional Parameters · times - Write Process Times · trap - Trap Signals · unset - Unset Values and Attributes of Variables and Functions · readonly - Set Read-only Attribute for Variables . local – set Local Attribute for variables
Examples • 1. cal • 2. which • 3. watchwho • 4. checkmail • 5. overwrite • 6. replace • 7. zap • 8. /etc/rc.d/init.d/smb