until loop help

jmbrown

New Member
Joined
May 29, 2019
Messages
1
Reaction score
1
Credits
0
Hello,
I'm working on a bash script 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.


Code:
#!/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
 
Last edited by a moderator:


...because it's an assignment that I need to learn from, but any nudges in the right direction would be greatly appreciated!

We get a number of young people here who hope we will do their homework for them, so Big Like to the above :)

G'day Jill and welcome to linux.org :p

I am moving this to Command Line, where it may gain the attention you need from our scripters.

Good luck with the assignment, and if/when you get time, perhaps swing over to https://www.linux.org/forums/member-introductions.141/ and tell us a little of the Jill Brown story and meet a few of The gang.

Cheers

Chris Turner
wizardfromoz
 
First up - for future reference, it helps if you use code tags in your post. Hopefully one of the mods can edit your post to add the code tags.

In the toolbar of the post editor, there is a button that shows a box with a + in it Three dots (an ellipsis ... ). That is the insert button. Click that and select "code" and then paste your code into the box that pops-up and your code will be inserted inside code-tags in your post.

The first problem with your script is your until loop.
Code:
until [ "$char"="j" ]

Because there are no spaces between the variable name, the = operator and the condition, what you've actually got there is an assignment, not a comparison.
So what you are doing is setting $char to "j". The boolean result of assigning a value to $char will be true - so what you have there basically equates to:
Code:
until [ true ]
So the code enclosed in the do...done block never gets fired off.

So you need to separate the variable name, the operator and the condition using spaces like this:
Code:
until [ "$char" = "j" ]
Now you have a comparison operation, not an assignment operation.

Also, one other issue I can see is with your read commands. You should be using the -p flag where you are prompting the user to enter a value.
e.g.
Code:
read -p "Please select a letter: " char
And do this for all of the read statements where you are displaying a prompt for the user.

In fact with the very first read statement, you only want to read a single character, so you could also add the -n flag and set the number of characters to 1, like this:
Code:
read -n 1 -p "Please select a letter: " char
That will cause the read command to read a single character from the input buffer, without waiting for the return key to be pressed.

I hope this has helped!
 
Last edited:
Hopefully one of the mods can edit your post to add the code tags.

That would be ... moi :D:D

Ta, Jas

BTW Jill - icon of plus sign on your reply pane toolbar houses Spoiler, Code, etc ;)
 
  • Like
Reactions: Rob


Top