Linux - NewbieThis 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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Numeric tests should properly be done in bash using ((..)), and string tests in [[..]]. The only time you need to use the old single bracket [ is when you're writing posix-compliant scripts for systems without bash.
select is good for simple menus, but it's often necessary to roll your own for more complex stuff. Most experienced scripters tend to emulate select and put the menu in a never-ending while true loop, using an if or case statement inside to evaluate the choice made, and break commands to exit the loop when desired.
Untested, but to demonstrate the principle...
4. other scripts automatic
enter choice [1 | 2 | 3 | 4 ]: '
while true; do
echo -n "$menu"
if (( numchoice >= 1 && numchoice <= 4 )); then
echo -n "you entered incorrect, try again: "
echo "you chose $numchoice. goodbye!"
David the H., I think you forgot the $ signs in your if statement.
This worked for me:
$ cat tmp.bash
while [ $# -gt 0 ]
if (( $numchoice >= 1 )) && (($numchoice <= 4 ))
$ ./tmp.bash 0 1 2 3 4 5 6 7 8 9 10
Last edited by PTrenholme; 11-04-2011 at 04:41 PM.
Reason: Cat "assisted" prior post . . .
That's right. In any arithmetic environment field, the only recognized strings are digits and mathematical operators. So when the shell sees an alphabetical string, it assumes it's a variable and automatically expands it before evaluation. It saves a bit of typing and clutter, at the least.
As far as I know, this is true of all bourne-based shells. It's likely even a posix specification, although I haven't confirmed that.
By the way, not only is "=" a comparison operator inside the [ and [[ bracket tests, it's a string comparison; the two sides will be evaluated according to their alphanumeric/ascii order values. For an arithmetic comparison, you must use "-eq" and similar. That's one reason the ((..)) test is recommended, it makes it clear at a glance that it's an arithmetic operation, and lets you use the more natural math operators you're used to (with the exception of the "=/==" difference explained above).