Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
BASH script fails to run on Gentoo but not other systems
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
deltamalloc
Apprentice
Apprentice


Joined: 18 Jan 2013
Posts: 279

PostPosted: Fri Jul 12, 2013 9:59 pm    Post subject: BASH script fails to run on Gentoo but not other systems Reply with quote

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
View user's profile Send private message
666threesixes666
Veteran
Veteran


Joined: 31 May 2011
Posts: 1248
Location: 42.68n 85.41w

PostPosted: Fri Jul 12, 2013 10:25 pm    Post subject: Reply with quote

post the entire script?

mkultra@mksrv ~ $ echo $IP

mkultra@mksrv ~ $ echo ${hosts[$i]}

unset in my OS.
Back to top
View user's profile Send private message
Aiken
Apprentice
Apprentice


Joined: 22 Jan 2003
Posts: 239
Location: Toowoomba/Australia

PostPosted: Sat Jul 13, 2013 12:12 am    Post subject: Reply with quote

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
View user's profile Send private message
666threesixes666
Veteran
Veteran


Joined: 31 May 2011
Posts: 1248
Location: 42.68n 85.41w

PostPosted: Sat Jul 13, 2013 12:59 am    Post subject: Reply with quote

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
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10587
Location: Somewhere over Atlanta, Georgia

PostPosted: Sat Jul 13, 2013 1:26 am    Post subject: Re: BASH script fails to run on Gentoo but not other systems Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum