From the course: Linux System Engineer: Bash Shell Scripting for Automation

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Conditional flow with case

Conditional flow with case

- [Instructor] Generally we use if conditionals in bash, however in cases where we're matching more than one pattern with an if/then/else/if conditional block, it may be more efficient to use a case statement. The case statement evaluates the condition one time and acts accordingly. This statement evaluates the age variable. If the value is one through nine numerically, then it executes the action listed to the right of the parenthesis until he double semi-colons which is the action list terminator. The characters to the left of the parenthesis make up a glob. You can do pattern matching in the same manner as you would on the command line using wild cards, character sets, and character classes. However you can not use regular expressions, which is unfortunate. The equivalent if condition would evaluate age up to four times to do the same thing, so this case statement would be much faster. The last condition, which is asterisk, catches everything that doesn't match above. This would be…

Contents