#!/bin/bash
#
# 'select' allows menu generation.
# The syntax is similar to that of the 'for' loop:
#
# select WORD [in LIST]; do COMMANDS; done
#
# LIST is expanded and printed in stderr.
# Each item is preceded by a number.
# Then the PS3 prompt is printed and one line from standard input is read.
#   - If read line consists of a number of the options, WORD is set to the
#     name of the item.
#   - If read line is empty, the items and PS3 prompt are printed again.
#   - If EOF is read, the loop exits. (Since most users don't know how to
#     enter EOF, using a 'break' command as one of the items is
#     recommended.)
#   - Reading any other value set WORD to the null string.
# 
# The read line is stored in the REPLY variable.
# COMMANDS are executed when selected until the number representing the
# 'break' (or EOF) is entered.

PS3="Your choice: "
QUIT="Quit this program."
touch "$QUIT"
echo "Enter the number of the file you want to delete:"

select filename in *;
do
     case $filename in
          "$QUIT")
            echo "Bye."
            break
            ;;
          
          *)
            echo "You selected $filename ($REPLY). Deleting it ..."
            rm "$filename"
            ;;
     esac
done
rm "$QUIT"

# Note 1:
# If LIST is not present, 'select' expands the positional parameters as if
# '@#' would have been used.
# Try it!
#
# Note 2:
# You can do submenus with nested 'select'.
# The PS3 variable is not changed, so you need to change it if you want a
# different prompt in the submenu.


