LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-31-2019, 09:47 PM   #1
jmbrown
LQ Newbie
 
Registered: May 2019
Posts: 5

Rep: Reputation: Disabled
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
Thanks!
Jill
 
Old 05-31-2019, 10:12 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,591

Rep: Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066
it works here just add sleep 3 between
Code:
esac 
sleep 3
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"
       
      ;; 
...

Last edited by BW-userx; 05-31-2019 at 10:38 PM.
 
Old 05-31-2019, 10:15 PM   #3
individual
Member
 
Registered: Jul 2018
Posts: 267
Blog Entries: 1

Rep: Reputation: 210Reputation: 210Reputation: 210
Quote:
Originally Posted by BW-userx View Post
it works here just add sleep 3 between
Code:
esac 
sleep 3
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.
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 10:16 PM.
 
Old 05-31-2019, 10:16 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,515

Rep: Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133
Or use the set -xv to help debug.

It's due to the clear command at the top of the until loop...
 
Old 05-31-2019, 10:42 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,591

Rep: Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066
Quote:
Originally Posted by individual View Post
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
and yes, I removed the sleep on your script here.

Last edited by BW-userx; 05-31-2019 at 10:53 PM.
 
Old 06-01-2019, 05:25 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,768

Rep: Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051Reputation: 3051
As you have been provided with some solutions, I would add you could consider looking at the "select" command
 
Old 06-01-2019, 06:12 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,515

Rep: Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133Reputation: 3133
FYI this script is a homework assignment for a linux class...
 
Old 06-01-2019, 08:19 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware FreeBSD Win10 MX
Posts: 9,591

Rep: Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066
OIC, then OP must have missed when the teacher spoke of conditionals and loops, and these two must be in the same class.

https://www.linuxquestions.org/quest...47#post6000247

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.

Last edited by BW-userx; 06-01-2019 at 09:54 AM.
 
Old 06-01-2019, 10:57 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,576

Rep: Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341Reputation: 4341
Quote:
Originally Posted by jmbrown View Post
I get the menu and the prompt to enter a selection, but instead of processing the commands it just exits the program.
I tested your script and this statement is not valid (for me).
 
Old 06-01-2019, 01:25 PM   #10
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 2,598

Rep: Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634
Code:
#! /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
 
Old 06-02-2019, 01:13 PM   #11
jmbrown
LQ Newbie
 
Registered: May 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.

Jill
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Scientific Audio Processing, Part III - How to apply Advanced Mathematical Processing Effects on Audio files with Octave 4.0 on Ubuntu LXer Syndicated Linux News 0 06-22-2016 07:12 PM
LXer: Scientific Audio Processing, Part II - How to make basic Mathematical Signal Processing in Audio files using Ubuntu with Octave 4.0 LXer Syndicated Linux News 0 06-20-2016 12:51 PM
[SOLVED] Centos equivalent of dpkg --get-selections/set-selections inemetz Linux - Server 1 03-20-2014 02:30 PM
2 Ubuntu Selections on Boot Menu krimzen85 Linux - Newbie 1 08-21-2009 09:35 PM
Can't seem to find reason why case/if ControlsStructs aren't working properly... RHLinuxGUY Programming 2 05-03-2006 02:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration