Advanced
Searching inSummaries
How to search
  • Words find lesson titles, text and code. Filter by type (guides, courses, programs, books) or stay in this book.
  • book:rhcsa and ch:8 narrow the search; #code finds a word only in commands of the lessons.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

Chapter 31Name in the book: Flow Control: Branching with case

Choose with case

When one value can have many possible answers, a long if … elif … elif is hard to read. case compares a word with a list of patterns and runs the first match. This chapter rewrites the menu with case and shows the pattern rules.

  • 5 Lessons
  • 0 Commands
  • 1 Labs

$ run as a normal user# run as root (or with sudo)

31.1

Write a case statement

case checks a word against patterns, from top to bottom. The first matching pattern wins; ;; ends its commands.

the form
case word in
    pattern1) commands ;;
    pattern2) commands ;;
    *)        commands ;;
esac
case-menu
#!/bin/bash
 
# case-menu: a menu driven system information program
 
clear
echo "
Please Select:
 
1. Display System Information
2. Display Disk Space
3. Display Home Space Utilization
0. Quit
"
read -p "Enter selection [0-3] > "
 
case "$REPLY" in
    0)  echo "Program terminated."
        exit
        ;;
    1)  echo "Hostname: $HOSTNAME"
        uptime
        ;;
    2)  df -h
        ;;
    3)  if [[ "$(id -u)" -eq 0 ]]; then
            echo "Home Space Utilization (All Users)"
            du -sh /home/*
        else
            echo "Home Space Utilization ($USER)"
            du -sh "$HOME"
        fi
        ;;
    *)  echo "Invalid entry" >&2
        exit 1
        ;;
esac

The same menu with if needed many more lines.

31.2

Use patterns

case patterns are like the shell's wildcards (pathname expansion). *) at the end catches everything else.

PatternMatches
a)The word "a"
[[:alpha:]])One letter
???)Exactly three characters
*.txt)Words that end with .txt
*)Anything (put it last as the default)
case-pattern
#!/bin/bash
 
read -p "enter word > "
 
case "$REPLY" in
    [[:alpha:]])        echo "is a single alphabetic character." ;;
    [ABC][0-9])         echo "is A, B, or C followed by a digit." ;;
    ???)                echo "is three characters long." ;;
    *.txt)              echo "is a word ending in '.txt'" ;;
    *)                  echo "is something else." ;;
esac

31.3

Combine patterns with |

| joins several patterns for the same commands. Good for upper and lower case answers.

accept q or Q
case "$REPLY" in
    q|Q)     echo "Program terminated."
             exit
             ;;
    a|A)     echo "Hostname: $HOSTNAME"
             uptime
             ;;
    yes|y|Y) echo "Confirmed." ;;
    *)       echo "Invalid entry" >&2
             exit 1
             ;;
esac

At work

case is the standard way to read options and sub-commands: start|stop|restart|status) in service scripts.

31.4

Run more than one match with ;;&

Normally case stops after the first match. With ;;& instead of ;;, it also tests the next patterns.

case4-2: test a character in several ways
#!/bin/bash
 
read -n 1 -p "Type a character > "
echo
case "$REPLY" in
    [[:upper:]])    echo "'$REPLY' is upper case." ;;&
    [[:lower:]])    echo "'$REPLY' is lower case." ;;&
    [[:alpha:]])    echo "'$REPLY' is alphabetic." ;;&
    [[:digit:]])    echo "'$REPLY' is a digit." ;;&
    [[:graph:]])    echo "'$REPLY' is a visible character." ;;&
    [[:punct:]])    echo "'$REPLY' is a punctuation symbol." ;;&
    [[:space:]])    echo "'$REPLY' is a whitespace character." ;;&
    [[:xdigit:]])   echo "'$REPLY' is a hexadecimal digit." ;;&
esac
case4-2
Type a character > a
'a' is lower case.
'a' is alphabetic.
'a' is a visible character.
'a' is a hexadecimal digit.

31.5

Summary of the chapter

case word in pattern) … ;; esac replaces long if chains; | joins patterns; *) is the default; ;;& keeps testing.

1Which pattern catches every value that no other pattern matched?

*), written as the last pattern.

2How do you accept both "q" and "Q" in one pattern?

q|Q).

3What is the difference between ;; and ;;&?

;; stops after the match; ;;& also tests the following patterns.

Practice