0

I have a program that works fine but I need it to not exit after a choice is made. Ideally after one choice is made the command will execute, and then the menu will pop back up asking the user to enter another choice. I know I will need to use some kind of loop for this, but I'm very new to bash scripting and couldn't find much help online / in my book. Here is what I have so far:

#!/bin/bash
#create the menu
echo -e "\n     MENU\n"
echo " a. Current date and time"
echo " b. Users logged in"
echo " c. Name of working directory"
echo " d. Create file with a custom name"
echo -e " e. Exit the program \n"
read -p "Enter a, b, c, d, or e: " answer
echo
#read input and perform utility
case "$answer" in
a)
date
;;
b)
who
;;
c)
pwd
;;
d)
read -p "Enter a name for your file " file
touch "$file"
;;
e)
exit
;;
*)
echo "$answer is not an option"
;;
esac
yes indeed
  • 59
  • 1
  • 7
  • Have it call itself :) – TheFiddlerWins Apr 30 '18 at 20:39
  • @TheFiddlerWins, unless that's done with `exec`, you consume extra memory each time (since the prior runs are still sitting there in RAM waiting for the copy of themselves they started as child processes to exit) -- not at all ideal. – Charles Duffy Apr 30 '18 at 20:42
  • Read up on the `select` command. – chepner Apr 30 '18 at 20:54
  • @MasonCox ...btw, generally speaking, `printf` is better form than `echo -e`; the latter is a shell-specific violation of the POSIX sh standard (not an extension, which would require it to be in unspecified space, but a *violation*, as the specification requires `echo -e` to print `-e` on output -- and doesn't even work with all runtime configurations of bash, which can be configured for compliance); whereas the former will be consistent across all POSIXy shells. See the APPLICATION USAGE and RATIONALE sections of http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html – Charles Duffy Apr 30 '18 at 21:12

0 Answers0