1 / 16

Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )

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

Download Presentation

Unix Programming Environment Part-4 Shell Programming Prepared by Xu Zhenya( xzy@buaa )

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 )

  2. Agenda • 1. Shell Features and Built-in Commands ( 2 ) • Loops and Branches, Function, Trap and set • 2. Examples • Chapter 5

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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

  10. Shell Features - Examples • Example 1: “case” • Example 2: “select”

  11. 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.

  12. 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.

  13. 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.

  14. 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.

  15. 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

  16. Examples • 1. cal • 2. which • 3. watchwho • 4. checkmail • 5. overwrite • 6. replace • 7. zap • 8. /etc/rc.d/init.d/smb

More Related