LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-29-2019, 02:16 PM   #1
jmbrown
LQ Newbie
 
Registered: May 2019
Posts: 5

Rep: Reputation: Disabled
Question until loop help


Hello,

I'm working on a bash script (see below) for a linux class that I'm taking. It's a command menu that uses an until loop and the case command.
This is probably such a newbie problem but I've searched the internet far and wide and have found no solutions.

The problem I'm having is this:

The until loop needs to include the line where they select a letter because I need the user to select a letter each time it loops. But then how do reference the variable before I define the variable? I'm guessing it can't be done this way, but I can't think of any other test that would make sense for this loop. (the loop should keep going until the user enters "j" to exit.

When I run this, it displays the menu and then immediately exits the program. No error messages, I just get a new prompt.

============================
#!/bin/bash


echo -e "\n COMMAND MENU\n"

echo " a. Email Program"
echo " b. Users currently logged on"
echo " c. Current Date and Time"
echo " d. This month's Calendar"
echo " e. Name of the Working Directory"
echo " f. Contents of the Working directory"
echo " g. Find the IP of a Web Address"
echo " h. See Your Fortune"
echo " i. Print a File"
echo " j. Exit"
echo

until [ "$char"="j" ]

do
read "Please select a letter: " char
case "$char" in

a)
read "Enter the subject line: " subj
read "Enter the email address: " addr
mail -s "$subj" "$addr"
;;

b)
who
;;

c)
date
;;

d)
calendar
;;

e)
pwd
;;

f)
ls
;;

g)
read "Enter the web address" wadd
host "$wadd"
;;

h)
fortune
;;

i)
read "Enter the name of the file: " fnam
more "$fnam"
;;

j)
exit
;;

esac

done
==================================
I don't want someone to necessarily fix it because it's an assignment that I need to learn from, but any nudges in the right direction would be greatly appreciated!

Thanks!
Jill
 
Old 05-29-2019, 02:28 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147
how to show you without fixing it? humm.
read with a message use -p for print the message.

Code:
read -p "Please select a letter: " char
Code:
[[ ]] vs [ ]
that is my hints.

Last edited by BW-userx; 05-29-2019 at 02:30 PM.
 
1 members found this post helpful.
Old 05-29-2019, 03:00 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 20,529

Rep: Reputation: 3591Reputation: 3591Reputation: 3591Reputation: 3591Reputation: 3591Reputation: 3591Reputation: 3591Reputation: 3591Reputation: 3591Reputation: 3591Reputation: 3591
Why can't you define the variable with a dummy value before the loop?
 
Old 05-29-2019, 04:19 PM   #4
jmbrown
LQ Newbie
 
Registered: May 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
Question update

BW-userx, thank you for the tips! I feel a little dumb that I missed the -p on all the read lines. And I changed the braces. It still won't loop, but I'm not getting any errors. I'm guessing it has to do with the fact that I'm calling the variable before I define it.

michaelk, I think maybe I just don't get how to do this. I tried just defining it as 0 before the loop and I'm not getting any errors doing that, but the program still doesn't run. I don't understand how I can define it first and then hand it over to the user to redefine. Maybe I'm looking at this all wrong?

Thanks again for any and all help!
 
Old 05-29-2019, 05:16 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147
Quote:
Originally Posted by jmbrown View Post
BW-userx, thank you for the tips! I feel a little dumb that I missed the -p on all the read lines. And I changed the braces. It still won't loop, but I'm not getting any errors. I'm guessing it has to do with the fact that I'm calling the variable before I define it.

michaelk, I think maybe I just don't get how to do this. I tried just defining it as 0 before the loop and I'm not getting any errors doing that, but the program still doesn't run. I don't understand how I can define it first and then hand it over to the user to redefine. Maybe I'm looking at this all wrong?

Thanks again for any and all help!
I got mine (yours) to loop, but per your instructions, I am trying not to show you.
but what does yours look like now?


[ ] vs [[ ]]
read, skim though until your find the [[ ]] towards the bottom, this should help you advance in your class as well.
Code:
#!/bin/bash
#define var
j=0

echo $j
echo;echo
until [[ "$j" = "b" ]]
do
read -p "type in anything , hit enter 
b to exit " j

echo "you typed $j"
[[ "$j" = "b" ]] && { echo "going bye bye now" ; exit ; }
done
run that code.
Global variables.
the value of a variable can be changed after it is defined and set to a value. It is outside of the loop so it does not get set back to j=0 until the script is ran again. but within the loop it is whatever the input is set to by the user.

To save a little time by eliminating having to type 'echo' for every line.
Code:
echo "    

          COMMAND MENU

a. Email Program
b. Users currently logged on
c. Current Date and Time
d. This month's Calendar
e. Name of the Working Directory
f. Contents of the Working directory
g. Find the IP of a Web Address
h. See Your Fortune
i. Print a File
j. Exit

"
your 'j' does not necessarily need to be defined. Though defining and setting a value eliminates corruption of data values. for the higher levels of programing. arrays in bash too can be defined ahead of time as empty, then filled dynamically. but that is I'd think advance bash'ing.

Last edited by BW-userx; 05-29-2019 at 05:47 PM.
 
Old 05-29-2019, 07:03 PM   #6
jmbrown
LQ Newbie
 
Registered: May 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
I got it running!

BW-userx, thanks for the guidance! I don't actually know what was wrong with it. I basically took it apart and put it back together again using your example script. I commented out all but the basic loop, got that running, and then added one piece back at a time, running it after each change. I got to the last part and the whole thing was working.

Thanks again!
 
1 members found this post helpful.
Old 05-29-2019, 07:33 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147Reputation: 2147
Quote:
Originally Posted by jmbrown View Post
BW-userx, thanks for the guidance! I don't actually know what was wrong with it. I basically took it apart and put it back together again using your example script. I commented out all but the basic loop, got that running, and then added one piece back at a time, running it after each change. I got to the last part and the whole thing was working.

Thanks again!
NP:


Code:
#!/bin/bash
set -x

#comment then uncomment to see results
j=0

until [ "$j" = 'b' ] 
do
  read -p "enter some input : b to exit " j
done
run just that and look at the output, first with j not defined, then with j defined, using b to see the check in the condition used by until..

Actually you should be able to remove that last case check for j because your until [ condition ] runs until true, as soon as j = your condition it fails, or exits. As you will see in that/this example.

after you do your single brackets change them to double brackets and do both checks on them with the j.

Last edited by BW-userx; 05-29-2019 at 07:46 PM.
 
Old 05-29-2019, 09:09 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,489

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
To avoid an uninitialized variable, the trick is to have two commands between the while/until and the do
Code:
until
  read -p "Please select a letter: " char
  [ "$char" = "j" ]
do
  case "$char" in
...
The exit status of the last command before the do decides if the loop continues or ends.

If you exit or break between the do and the done, then you should not test the same condition again: have an endless loop:
Code:
until false
do
...
or better (think positive!)
Code:
while true
do
  read -p "Please select a letter: " char
  case "$char" in
...
  j)
    exit
  ;;
  *)
    echo "illegal choice"
  ;;
  esac
done

Last edited by MadeInGermany; 05-29-2019 at 09:12 PM.
 
  


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
[SOLVED] Bash Scripting (until loop) cryingthug Programming 10 06-30-2010 09:30 PM
Until loop in bash shell scripting help computergeek7 Programming 6 03-01-2010 11:34 PM
Recalculate variable for each pass of an until loop wideyes Linux - Newbie 5 02-12-2010 01:23 AM
Tail and a until loop Fredde87 Linux - Software 8 03-04-2008 05:25 AM
incrementing variable in an until loop jeffreybluml Programming 2 05-12-2005 07:45 AM

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

All times are GMT -5. The time now is 06:52 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
Open Source Consulting | Domain Registration