Bash - Case

About

The case control statement execute commands based on regular expression pattern matching.

Syntax

case WORD in PATTERN1) COMMANDS;; PATTERN2) COMMANDS ;; ... PATTERNN) COMMANDS;; esac

Selectively execute COMMANDS based upon WORD matching PATTERN. The | is used to separate multiple patterns.

Flow words:

  • continue
  • break

Exit Status

Returns the status of the last command executed.

Example

Initscript example:

case "$1" in
        start)
            start
            ;;
         
        stop)
            stop
            ;;
         
        status)
            status anacron
            ;;
        restart)
            stop
            start
            ;;
        condrestart)
            if test "x`pidof anacron`" != x; then
                stop
                start
            fi
            ;;
         
        *)
            echo $"Usage: $0 {start|stop|restart|condrestart|status}"
            exit 1
 
esac

Documentation / Reference


Powered by ComboStrap