OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

case

In bash, multiple-choice compound command is called case. It has the following syn- tax:

case word in

[pattern [| pattern]...) commands ;;]...

esac

If we look at the read-menu program from Chapter 28, we see the logic used to act on a user’s selection:



#!/bin/bash

# read-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] > "


if [[ $REPLY =~ ^[0-3]$ ]]; then if [[ $REPLY == 0 ]]; then

#!/bin/bash

# read-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] > "


if [[ $REPLY =~ ^[0-3]$ ]]; then if [[ $REPLY == 0 ]]; then


echo "Program terminated." exit

fi

if [[ $REPLY == 1 ]]; then echo "Hostname: $HOSTNAME" uptime

exit

fi

if [[ $REPLY == 2 ]]; then df -h

exit

fi

if [[ $REPLY == 3 ]]; then

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 exit

fi else

echo "Invalid entry." >&2 exit 1

fi

echo "Program terminated." exit

fi

if [[ $REPLY == 1 ]]; then echo "Hostname: $HOSTNAME" uptime

exit

fi

if [[ $REPLY == 2 ]]; then df -h

exit

fi

if [[ $REPLY == 3 ]]; then

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 exit

fi else

echo "Invalid entry." >&2 exit 1

fi


Using case, we can replace this logic with something simpler:


#!/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

;;

#!/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

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 case command looks at the value of word, in our example, the value of the REPLY variable, and then attempts to match it against one of the specified patterns. When a match is found, the commands associated with the specified pattern are executed. After a match is found, no further matches are attempted.


 

Top OS Cloud Computing at OnWorks: