Linux – Bash Shell Script with Menu

bashlinux

I've written a bash shell script (code provided below) that gives the user 4 options. However I'm having a little trouble with the code. Right now when they select option 3, to show the date. It loops over and over again. I have to close the terminal window to stop it because it's an infinite loop. How would I prevent this? Also quit doesn't seem to be working either.

If someone could help me out a bit, thanks.

#!/bin/bashe
 echo -n "Name please? "
 read name
 echo "Menu for $name
    1. Display a long listing of the current directory
    2. Display who is logged on the system
    3. Display the current date and time
    4. Quit "
read input
input1="1"
input2="2"
input3=$(date)
input4=$(exit)
while [ "$input" = "3" ]
do
echo "Current date and time: $input3"
done

while [ "$input" = "4" ]
do
echo "Goodbye $input4"
done

Best Answer

A compact version:

options=(
    "Display a long listing of the current directory"
    "Display who is logged on the system"
    "Display the current date and time"
    "Quit" 
)

PS3="Enter a number (1-${#options[@]}): "

select option in "${options[@]}"; do
    case "$REPLY" in 
        1) ls -l ;;
        2) who ;;
        3) date ;;
        4) break ;;
    esac
done
Related Question