User Tools

Site Tools


bash:case
click
to support this page (eg 0,42 € or so) Thanks.

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

bash:case [2015/05/27 22:27] (current)
37.120.108.140 created
Line 1: Line 1:
 +===== case =====
 +<code bash>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.
 +</code>
 +
 +
 +=== bash case syntax ===
 +<code bash>
 +case "$variable" in
 +
 + "$condition1" )
 + command...
 + ;;
 +
 + "$condition2" )
 + command...
 + ;;
 +
 +esac 
 +</code>
 +
 +
 +=== bash case example ===
 +<code bash>
 +#! /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
 +</code>
 +
 +=== output of case ===
 +<code bash>
 +no output atm</code>
advertising
bash/case.txt · Last modified: 2015/05/27 22:27 by 37.120.108.140

Impressum