Text-only Table of Contents (frame/ no frame)
(19) Flow control (contd.) Previous Top Next

Flow Control and Compound Commands (contd.)

Case statement: pattern matching

case word in pattern) list;; esac
Compare word with each pattern) in turn, and executes the first list for which the word matches. The patterns follow the same rules as for filename wildcards. Less detail
(ksh and bash only) A pattern-list is a list of one or more patterns separated from each other with a |. Composite patterns can be formed with one or more of the following:
?(pattern-list)
Optionally matches any one of the given patterns.
*(pattern-list)
Matches zero or more occurrences of the given patterns.
+(pattern-list)
Matches one or more occurrences of the given patterns.
@(pattern-list)
Matches exactly one of the given patterns.
!(pattern-list)
Matches anything, except one of the given patterns.

Example:

    case $filename in
    *.dat)
        echo Processing a .dat file
        ;;
    *.sas)
        echo Processing a .sas file
        ;;
    *)
        # catch anything else that doesn't match patterns
        echo "Don't know how to deal with $filename"
        ;;
    esac

Miscellaneous flow control and subshells

break [n]
Break out of the current (or n'th) enclosing loop. Control jumps to the next statement after the loop

continue [n];
Resume iteration of the current (or n'th) enclosing loop. Control jumps to the top of the loop, which generally causes re-evaluation of a while or processing the next element of a for.

. filename
Read the contents of the named file into the current shell and execute as if in line. Uses $PATH to locate the file, and can be passed positional parameters. This is often used to read in shell functions that are common to multiple scripts. There are security implications if the pathname is not fully specified.

( ... ) Command grouping
Commands grouped in "( )" are executed in a subshell, with a separate environment (can not affect the variables in the rest of the script).

Previous Top Next


flow-control-2.src  last modified Mar 10, 2005 Introduction Table of Contents
(frame/no frame)
Printable
(single file)
© Dartmouth College