Bash : While Loop behavior

Good Morning

I think there may be something I dont understand fully.

The following code works well, but I dont like the set domen method.

#!/bin/bash
#
domen="y"
while [ "$domen" == "y" ]
do
echo " M A I N - M E N U"
echo "1. Contents of /etc/passwd"
echo "2. List of users currently logged"
echo "3. Prsent handling directory"
echo "4. Exit"
echo "Please enter option [1-4] : "
read theopt
echo $theopt
case $theopt in
1) echo $opt;;
2) echo $opt;;
3) echo $opt;;
4) exit 0;;
*) echo "Bad opt";;
esac
done

So I looked in one of my books (Written by Chris FA Johnson) and on the web for a second example. The following doesnt work. You'll have to run it to see what I mean. Ill try to explain. As I run through the menu options to test, at one point or another, my option isnt read and the behavior is kind of like a back space. Even when using -n1 for read behavior was odd.

#!/bin/bash
#
while : 
do
echo " M A I N - M E N U"
echo "1. Contents of /etc/passwd"
echo "2. List of users currently logged"
echo "3. Prsent handling directory"
echo "4. Exit"
echo "Please enter option [1-4] : "
read theopt
echo $theopt
case $theopt in
1) echo $opt;;
2) echo $opt;;
3) echo $opt;;
4) exit 0;;
*) echo "Bad opt";;
esac
done

Chris' example does provide a function to do the reading of the options, but I want to understand whats going on here.

As usual thanks for the help !
Pop

In what way does it not work?

$ ./oneopt.sh
 M A I N - M E N U
1. Contents of /etc/passwd
2. List of users currently logged
3. Prsent handling directory
4. Exit
Please enter option [1-4] :
1
1

 M A I N - M E N U
1. Contents of /etc/passwd
2. List of users currently logged
3. Prsent handling directory
4. Exit
Please enter option [1-4] :
2
2

 M A I N - M E N U
1. Contents of /etc/passwd
2. List of users currently logged
3. Prsent handling directory
4. Exit
Please enter option [1-4] :
3
3

 M A I N - M E N U
1. Contents of /etc/passwd
2. List of users currently logged
3. Prsent handling directory
4. Exit
Please enter option [1-4] :
4
4

$

I see nothing wrong.

1 Like

I can't replicate the error that you report. Did you consider bash 's select builtin?

1 Like

I just realized something. By 'backspace' do you mean 'goes to the beginning of the line'? Your file may have carriage returns in it. What have you been editing it with?

1 Like

I'd "double quote" the $variables so the script does not fail for all whitespace in or shell sensitive characters.

You read 'theopt' but echo 'opt', which is never set.

Usually ':' prompts have no linefeed, like: 'echo "Please enter option [1-4] : \c"' or use the 'read -p prompt' option.

1 Like

VERY INTERESTING !

Ive tried this on a VM at school, a Ubuntu VM on my latptop and a Centos VM on my laptop.

What happens on all is that as I run through and test each option, at one point it seems that the read doesnt except input. It really behave like a backspace, but what I suspect is happening is that read is executing again. Kind of ignores input.

PS Stumped the instructor too !!!

---------- Post updated at 01:13 PM ---------- Previous update was at 01:05 PM ----------

I just tried to execute it on a work VM ... shhh dont tell the boss. Its running on that vserver. What the heck ? Only difference is that Im using VMPLAYER on my laptop and at school. .... scratching my head. Technical problem between chair and keyboard ??

We will not know exactly what you mean unless you copy-paste the output of it misbehaving.

Also, from earlier:

1 Like

Im editing with vi. Ive :set list and dont see any extraneous items in the script files on the VMPLAYER VMs. I will also double quoted all the vars when I get to the case. Yep. At the time I was looking at this I didnt really care what the case was doing because it was a read problem I was having.

Could you copy-paste the output of it misbehaving? I really don't understand what "like a backspace" means.

You could also do

$ script 
Script started, file is typescript
$ ./myscript.sh

...

$ exit

and attach the typescript file it creates to the forum so we can see what's happening.

1 Like

DOPEY !!!! I stripped down everything that wasnt really needed for the forum and posted.

I grabbed the script lines I posted on the forum and ran that on the work VM. Worked fine but still failing on my laptop.

Brought up works VM and my laptop VM. Eyeballed line by line.
... sitting here thinking and laughing at myself "What a freekin idiot !"

Put a clear between the do and echo Main ... and you'll see what I was talking about. At least I realized it was probably me !!!

:slight_smile: sheez !!

Thanks guys !

1 Like

I just wonder what you expected output is from echo $opt as opt is never set, only theopt .

hth