Bash - Case

Bash Liste Des Attaques Ovh

About

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

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

Syntax

case WORD in PATTERN1) COMMANDS;; PATTERN2) COMMANDS ;; ... PATTERNN) COMMANDS;; esac
# or
case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac

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

Flow words:

  • continue
  • break

A case command first expands word, and tries to match it against each pattern in turn, using the same matching rules as for path-name expansion.

The word is expanded using:

Each pattern examined is expanded using:

  • tilde expansion,
  • parameter and variable expansion,
  • arithmetic substitution,
  • command substitution,
  • and process substitution.

Match

If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters.

When a match is found, the corresponding list is executed.

After the first match, no subsequent matches are attempted.

Exit Status

The exit status is zero if no pattern matches. Otherwise, it is the exit status of the last command executed in list.

Exit Status

Returns the status of the last command executed.

Documentation / Reference

help case





Discover More
Bash Liste Des Attaques Ovh
Bash - (Argument|Positional Parameter)

An argument is a parameter given: to a command to a function or to the bash shell They are referenced by position. A positional parameter is a parameter denoted: by one or more digits, ...
Bash Liste Des Attaques Ovh
Bash - Flow statement (Control Structure)

The words that control the flow of statement (ie condition and loop). help see
Bash Liste Des Attaques Ovh
Bash - getopts - Argument Parser (Builtin command)

getopts is used by shell procedures to parse positional parameters. dde p, d and f are the options. The script usage is: The implementations where: Bonus: Prompt for parameters is some...
Bash Liste Des Attaques Ovh
How to use Regular expression (regex) in Bash?

This article shous you how to use regular expression in Bash This example shows you how you can capture a Group to extract text. Example where we will extract a part of a file name When the...



Share this page:
Follow us:
Task Runner