[SOLVED] menu loop using case - selections aren't processing
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
menu loop using case - selections aren't processing
Hello all,
Sadly, I've been working on this all day and I'm stumped. I've searched this forum and other forums and googled it, all to no avail. So, please help me.
Here is my program. It's just a first draft, I just want to get it running before I complicate things. It's just a simple command menu using case and an until loop.
I get the menu and the prompt to enter a selection, but instead of processing the commands it just exits the program.
I'm not sure if this is a problem with case or with the loop or both but it seems like things work fine until case gets involved.
I just know I'm going to feel dumb when I find out what's wrong. Thanks for help with this!
Code:
#!/bin/bash
pick=0
until [[ "$pick" == "q" ]]
do
clear
echo -e "\n Jill's Main Menu\n"
echo " a. Display users currently logged on"
echo " b. Display a calendar for a specific month and year"
echo " c. Display the current directory path"
echo " d. Change directory"
echo " e. Long listing of visible files in current directory"
echo " f. Display current time and date and calendar"
echo " g. Start the vi editor"
echo " h. Email a file to a user"
echo " q. Quit"
echo
read -p "Please make your selection: " pick
case "$pick" in
a)
who | more
;;
b)
read -p "Enter the month you would like to see: " mon
read -p "Enter the year you would like to see: " yr
cal "$mon" "$yr"
;;
c)
pwd
;;
d)
read -p "which directory would you like to change to? " dir
cd "$dir"
;;
e)
ls -l
;;
f)
date
cal
;;
g)
read -p "Enter the name of the file you would like to open: " file
vi "$file"
;;
#sort this out later
# h)
# read -p "Enter the name of the user you would like to email: " user
#
# if [ "$user" in /etc/passwd ]; then
#
# echo "That is not a valid user. Try again."
#
# else
#
# read -p "Enter the email address: " email
# read -p "Enter the subject line: " sub
# read -p "Enter the name of the file you would like to attach: " fl
#
# fi
#
#;;
q)
clear
exit
;;
esac
done
to see it working. it looks to run so fast on my system that it's pushing the results up and off screen of the terminal here. then showing that menu again.
there probably is a better way to do this but it gets the job done. that's all that really matters.
a good program is one that works.
Code:
case "$pick" in
a)
who | more
#just holds it in place until enter is pressed
# to see results
echo
read -p "press Enter continue"
;;
b)
read -p "Enter the month you would like to see: " mon
read -p "Enter the year you would like to see: " yr
cal "$mon" "$yr"
#just holds it in place until enter is pressed
# to see results
echo
read -p "press Enter continue"
;;
c)
pwd
#just holds it in place until enter is pressed
# to see results
echo
read -p "press Enter continue"
;;
...
to see it working. it looks to run so fast on my system that it's pushing the results up and off screen of the terminal here. then showing that menu again.
A better way might be to use read. Then the user only needs to press Enter to go to the beginning of the loop.
EDIT: On another note, it is redundant to have your while-loop condition and your case condition check for q. You should pick one or the other.
Last edited by individual; 05-31-2019 at 09:16 PM.
A better way might be to use read. Then the user only needs to press Enter to go to the beginning of the loop.
EDIT: On another note, it is redundant to have your while-loop condition and your case condition check for q. You should pick one or the other.
yeah if you're talking about removing that seep thing, I did here, just added the read enter
to OP
I put that in a function
Code:
#!/bin/bash
#set -x
pick=0
message ()
{
echo
read -p "press Enter to continue"
}
until [[ "$pick" == "q" ]]
do
#clear
echo -e "\n Jill's Main Menu\n"
echo " a. Display users currently logged on"
echo " b. Display a calendar for a specific month and year"
echo " c. Display the current directory path"
echo " d. Change directory"
echo " e. Long listing of visible files in current directory"
echo " f. Display current time and date and calendar"
echo " g. Start the vi editor"
echo " h. Email a file to a user"
echo " q. Quit"
echo
read -p "Please make your selection: " pick
case "$pick" in
a)
who | more
message
;;
b)
read -p "Enter the month you would like to see: " mon
read -p "Enter the year you would like to see: " yr
cal "$mon" "$yr"
message
;;
c)
pwd
message
;;
d)
read -p "which directory would you like to change to? " dir
cd "$dir"
message
;;
e)
ls -l
message
;;
f)
date
cal
message
;;
g)
#it's opening an external app no message added here.
read -p "Enter the name of the file you would like to open: " file
vi "$file"
;;
#sort this out later
# h)
# read -p "Enter the name of the user you would like to email: " user
#
# if [ "$user" in /etc/passwd ]; then
#
# echo "That is not a valid user. Try again."
#
# else
#
# read -p "Enter the email address: " email
# read -p "Enter the subject line: " sub
# read -p "Enter the name of the file you would like to attach: " fl
#
# fi
#
#;;
....
as stated redundant
Code:
##redundant your [ conditional ] causes it to exit on q
# q)
# clear
# exit
# ;;
esac
done
OP : jmbrown again you're missing the basic concept of a conditional statement used with a loop. This basic concept goes across board with all programming languages because computers all work on true and false, 0 and 1, else they throw an error. no shades of gray.
Code:
#!/bin/bash
#LOOPS
#while loop:
#The while loop will run until the condition is set to a false statement (condition).
#the condition needs to be true before it will even execute (at least once).
#match condition here for while loop to start
c=g
while [[ $c = 'g' ]]
do
echo "$c"
#increment count
((b++))
#check a number if match change the
#value of c to make it a false statement in order for the while loop to stop.
[[ "$b" -eq '5' ]] && { c=s ; echo "exiting while..." ; }
done
echo "c= $c, b= $b"
sleep 1
#until loop:
#The until loop will run until the condition is set to a true statement (condition).
#the condition needs to be false before it will even execute (at least once).
#unmatch the condition of c here for until loop to start, and b for the count to work in the until loop.
c=0
b=0
echo "reset c= $c, b= $b"
until [[ $c = 'g' ]] ;
do
echo "$c"
#increment count
((b++))
#check count number, if match then change the
#value of c to make it a true statement in order to stop the until loop
[[ "$b" -eq '5' ]] && { c=g ; echo "exiting until..." ; }
done
echo "c= $c, b= $b"
sleep 1
echo
#for loop to complete the loops types
#loops until the condition is matched.
#there are a many ways for loops can be written. as seen here, two of them
#in bash depending on version of bash determines
#the syntax that can be used to have for loops written
for (( d=0;d<10; )) ; do
echo $((d++))
done
sleep 1
echo
#or
for ((d=0;d<10;d++)) ; do
echo "$d"
for loops . to compete your loops you need to know.
Just to point out in case your teacher or others may say this about until loops always running at least once. As stated here in this tutorial and perhaps others.
Code:
#!/bin/bash
c=g
b=0
echo "before until c=$c"
until [[ $c = 'g' ]] ;
do
echo "in Until loop c=$c"
[[ "$((b++))" -eq '5' ]] && { c=g ; echo "exiting until..." ; }
done
that until loop will not run at least once. It is the do .. while loop that will always run at least once. Which as far as I know bash does not support it. c++,java, and a few others do. Just to get a fuller coverage of your loops.
#! /usr/bin/env bash
#PS3 Prompt
PS3='
Select an option.: '
#Array of options
options=('a. Display users currently logged on'
'b. Display a calendar for a specific month and year'
'c. Display the current directory path'
'd. Change directory'
'e. Long listing of visible files in current directory'
'f. Display current time and date and calendar'
'g. Start the vi editor'
'h. Email a file to a user'
'q. Quit')
#return function
ret() {
read -p "Press enter to return: "
}
#Main loop
while :; do
clear
select opt in "${options[@]}"; do
case "$opt" in
"${options[0]}") who
ret; break ;;
"${options[1]}") echo "option 2"
ret; break ;;
"${options[2]}") pwd
ret; break ;;
"${options[3]}") echo "option 4"
ret; break ;;
"${options[4]}") ls -l
ret; break ;;
"${options[5]}") date; cal
ret; break ;;
"${options[6]}") echo "option 7"
ret; break ;;
"${options[7]}") echo "option 8"
ret; break ;;
"${options[8]}") exit ;;
esac
done
done
Wow. I had a couple of long days at work and couldn't get back to this. So many suggestions and so much info! Thank you everyone! I'll sit down and go through it all and try it all out. I really appreciate the effort to teach rather than just give an answer. I'll come back and mark as solved as soon as I get it working.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.