ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I cannot seem to figure out why I cant execute "top" or "nano" is this test script. Feedback much welcome.
It seems my value of $C always breaks and doesn't execute my command.
Code:
#!/bin/bash
clear
echo "Testing this script"
echo
echo "1- top"
echo "2- nano"
echo
echo "Select either (1-2) and press Enter:"
while read C && test "$C" \!= "1" -a "$C" \!= "2"; do
case $C in
1)
top
clear && break ;;
2)
nano
clear && break ;;
*)
clear && echo "That's not one of the choices, try again"
echo ;;
esac
done
Newbie in Training....
Last edited by manwithaplan; 02-25-2009 at 06:11 AM.
well you've already got a working test in the *) option. that's the point of the case statement, right? I've never seen additional commands between a while and a do, suprised it works at all personally, but if it did work your logic is backwards, that test will only be true if C is NOT 1 or 2, therefore those 1) and 2) clauses can logically never be reached.
Your right it doesn't work... it seems test doesnt support '*' wildcards, like in a case statement.
I changed it:
Code:
#!/bin/bash
clear
echo "Testing this script"
echo
echo "1- top"
echo "2- nano"
echo
echo "Select either (1-2) and press Enter:"
while read C && test "$C" \!= "1" -a "$C" \!= "2"; do
clear && echo "That's not one of the choices, try again"
done
case $C in
1)
top
clear && break ;;
2)
nano
clear && break ;;
esac
It seems I'm way limited to adding additional choices. e.g. 3,4,5,6,7,8,9
bigearsbilly:
Perfect, I changed it too:
Code:
select choice in top nano;
do
$choice
break
done
Though I need to add a menu with extra print
Last edited by manwithaplan; 02-25-2009 at 07:26 AM.
no no, just nuke the test statement, and use a PROPER case statement.
Code:
#!/bin/bash
clear
echo "Testing this script"
echo
echo "1- top"
echo "2- nano"
echo
echo "Select either (1-2) and press Enter:"
while read C ; do
case $C in
1)
top
clear && break ;;
2)
nano
clear && break ;;
*)
clear && echo "That's not one of the choices, try again"
echo ;;
esac
done
test is a slightly odd tool, as it is used to evaluate statements, but normally in line with other structured commands, like if and while. test is also called by [ which allows the format of a test query to look much more like a standard logic statement:
if test $a -eq b
then
...
fi
can be changed to
if [ $a -eq b ]
then
...
fi
which probably looks more self explanatory, but here [ is actually a symbolic link to the test command itself. clever little trick basically.
acid_kewpie I was looking @ your PROPER case, and noticed that it kept creating a line in my menu as it returned and didnt clear the selected number off the screen
e.g.
Code:
Testing this script"
"1- top"
"2- nano"
"Select either (1-2) and press Enter
1
2
4
etc...
So I used the [ ] "the clever little trick" and tried this to return the menu:
Code:
#!/bin/bash
clear
C=0
while [ $C -eq 0 ]; do
echo "Testing this script"
echo
echo "1- top"
echo "2- nano"
echo
read -p "Select either (1-2) and press Enter: " C
case $C in
1)
top
clear && break ;;
2)
nano
clear && break ;;
*)
clear
C=0 ;;
esac
done
This automatically reset C=0 and returns to the menu when an incorrect choice is chosen.
test is a slightly odd tool, as it is used to evaluate statements, but normally in line with other structured commands, like if and while. test is also called by [ which allows the format of a test query to look much more like a standard logic statement:
if test $a -eq b
then
...
fi
can be changed to
if [ $a -eq b ]
then
...
fi
which probably looks more self explanatory, but here [ is actually a symbolic link to the test command itself. clever little trick basically.
...wow...
i did not know that the [ was just a link for test...
i had always used the if [...]; fi format with no idea it was the same as if test...
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Believe it or not, Bash even has a formal language description. That is something I never had expected from something which seems so unlogical as bash to humans.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.