menu with while loop
I'm trying create a menu with a while loop in the following manner
Code:
#!/bin/bash [: missing `]' I suspect its something to do with my syntax. can someone please tell me what i'm doing wrong, thanks! |
while loops don't work quite like that. This guide will show you where you've gone wrong:
http://tldp.org/LDP/abs/html/loops1.html |
Don't use While loop for menu. use " select ".
Code:
#!/bin/bash -- Ramesh Jonathan Godishela |
Here is what I use:
Code:
#!/bin/bash Here is a sample run of the above script: Code:
$ /bin/bash h.sh You can remove these double quotes: Code:
"1" ) ./healthchk.sh ;; You see: Code:
h.sh: line 14: ./healthchk.sh: No such file or directory |
First, are you using Bash's builtin [, or are you using an external implementation of [? Here's how to find out; if it is Bash's [ then it doesn't understand the --help option.
Code:
tmp$ [ --help Now in your script, here's the problem line: Code:
while [ !($numchoice -ge 1) && !($numchoice -le 4) ] |
change line with while definition to
while [ $numchoice -lt 1 -a $numchoice -gt 4 ] |
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.
http://mywiki.wooledge.org/ArithmeticExpression http://mywiki.wooledge.org/BashFAQ/031 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... Code:
#!/bin/bash |
I just tried to answer OP's qyestion as directly as possible. My previous reply doesn't seem very clear. Here is a much better explanation of the problem I see in OP's code:
BashPitfalls - Greg's Wiki:6. [ "$foo" = bar && "$bar" = foo ] All the replies I see here now bring up other important points too, and IMHO OP should heed them. HTH |
:scratch: David the H., I think you forgot the $ signs in your if statement.
This worked for me: Code:
$ cat tmp.bash |
Quote:
There is more confusion about ((...)). Consider the following: Code:
num=3 Code:
num=3 Code:
num=3 |
Yes, you're correct. :redface:
In fact, both (( numchoice >= 1 )) && (( numchoice <= 4 )) and (( numchoice >= 1 && numchoice <= 4 )) work as the object of the if with bash 4.2.10. I plead being set in my ways after 70 odd years . . . :) |
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). |
All times are GMT -5. The time now is 02:34 PM. |