View previous topic :: View next topic |
Author |
Message |
deltamalloc Apprentice
Joined: 18 Jan 2013 Posts: 279
|
Posted: Fri Jul 12, 2013 9:59 pm Post subject: BASH script fails to run on Gentoo but not other systems |
|
|
I have a script I wrote myself that works on other systems, e.g. standard Slackware, but fails on Gentoo.
I remember something similar from a distro that didn't really use bash, bash was just a symlink to something else. But on my Gentoo system /bin/bash is the real bash (at least it is not a symlink). What could it be that makes the script fail to run?
The line that fails is:
Code: |
if [ $IP == ${hosts[$i]} ]; then
|
This fails with:
Code: |
[: ==: unary operator expected
|
|
|
Back to top |
|
|
666threesixes666 Veteran
Joined: 31 May 2011 Posts: 1248 Location: 42.68n 85.41w
|
Posted: Fri Jul 12, 2013 10:25 pm Post subject: |
|
|
post the entire script?
mkultra@mksrv ~ $ echo $IP
mkultra@mksrv ~ $ echo ${hosts[$i]}
unset in my OS. |
|
Back to top |
|
|
Aiken Apprentice
Joined: 22 Jan 2003 Posts: 239 Location: Toowoomba/Australia
|
Posted: Sat Jul 13, 2013 12:12 am Post subject: |
|
|
I would be tempted to put
Code: |
echo $IP $i ${hosts[$i]}
|
before the if to see if the 3 variables are set. The error message has me thinking 1 of the variables is not set.
Code: |
#!/bin/bash
IP="172.16.31.1"
hosts[1]="172.16.31.1"
hosts[2]="172.16.31.2"
i=1
echo $IP $i ${hosts[$i]}
if [ $IP == ${hosts[$i]} ]; then echo found; fi
|
works for me but if for example IP is not set then I get the same error message as you do. Same error if i is out of range so hosts[i] is not set. _________________ Beware the grue. |
|
Back to top |
|
|
666threesixes666 Veteran
Joined: 31 May 2011 Posts: 1248 Location: 42.68n 85.41w
|
Posted: Sat Jul 13, 2013 12:59 am Post subject: |
|
|
ok... and what does your slackware box say about
Code: |
echo $IP && echo ${hosts[$i]}
|
under the user that it works? (awww snap slackware supporting gentoo) |
|
Back to top |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10589 Location: Somewhere over Atlanta, Georgia
|
Posted: Sat Jul 13, 2013 1:26 am Post subject: Re: BASH script fails to run on Gentoo but not other systems |
|
|
deltamalloc wrote: | Code: |
if [ $IP == ${hosts[$i]} ]; then
|
This fails with:
Code: |
[: ==: unary operator expected
|
| If you quoted those variables properly, then the syntax error would never occur, regardless of whether or not they exist. Bad form not to quote 'em.
- John _________________ I can confirm that I have received between 0 and 499 National Security Letters. |
|
Back to top |
|
|
|