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.
$ ./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 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?
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 ??
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.