User Tools

Site Tools


Sidebar

Programming Reference/Librarys

Question & Answer

Q&A is closed







bash:case

case

The case and select constructs are technically not loops, 
since they do not iterate the execution of a code block. Like loops,
however, they direct program flow according to conditions at the top or bottom of the block.
 
Controlling program flow in a code block
 
case (in) / esac
 
The case construct is the shell scripting analog to switch in C/C++. 
It permits branching to one of a number of code blocks, depending on condition tests. 
It serves as a kind of shorthand for multiple if/then/else statements and is an appropriate tool for creating menus.

bash case syntax

case "$variable" in
 
 "$condition1" )
 command...
 ;;
 
 "$condition2" )
 command...
 ;;
 
esac 

bash case example

#! /bin/bash
 
 
while [ $# -gt 0 ]; do    # Until you run out of parameters . . .
  case "$1" in
    -d|--debug)
              # "-d" or "--debug" parameter?
              DEBUG=1
              ;;
    -c|--conf)
              CONFFILE="$2"
              shift
              if [ ! -f $CONFFILE ]; then
                echo "Error: Supplied file doesn't exist!"
                exit $E_CONFFILE     # File not found error.
              fi
              ;;
  esac
  shift       # Check next set of parameters.
done
 
#  From Stefano Falsetto's "Log2Rot" script,
#+ part of his "rottlog" package.
#  Used with permission

output of case

no output atm

on the occasion of the current invasion of Russia in Ukraine

Russian Stop this War
bash/case.txt · Last modified: 2024/02/16 00:48 (external edit)

Impressum Datenschutz