Register
It is currently Sun Apr 20, 2014 8:34 am

Bash square bracket substitution


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Thu Feb 12, 2009 12:01 pm   

Joined: Thu Feb 12, 2009 11:53 am
Posts: 3
Hi, let say I run the following script running on Solaris 9

x="WORD [1.2.3.4]"
for y in $x
do
echo y = $y
done

I would expect to see :
WORD
[1.2.3.4]

If I run the script from my prompt, it is exactely what I see.
But if the script is called from an other program (Smarts which is a Java application to monitor Networks), I see
WORD
1

The bash seams to do a condition evaluation of [1.2.3.4] to return TRUE (1)

Any way of not doing that?

Thanks,
Luc


Top
 Profile  
 PostPosted: Thu Feb 12, 2009 3:04 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
escape the brackets and maybe the dots?

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Fri Feb 13, 2009 8:00 am   

Joined: Thu Feb 12, 2009 11:53 am
Posts: 3
Hi Fredrik, thank you for you reply.

Yes I tried escaping the [ with a \ but this time the output is
y = WORD
y = \[1.2.3.4]

Because it is working fine when I am at the prompt but not when called from the program, I was thinking that something changed in the environnement but I can not figure out what it is to change it back.


Top
 Profile  
 PostPosted: Fri Feb 13, 2009 9:18 am   

Joined: Thu Feb 12, 2009 11:53 am
Posts: 3
Fredrik,
I used your suggestion to change my code for this:
x="WORD \\[1.2.3.4]"
for y in $x
do
y=${y//\\\\\\\\[/[}
echo y = $y
done

and it works find.

I still do not understand why the for evaluates the [1.2.3.4] word to 1.

If any one can tell me and solve the problem more elegantely, I would apreciate.


Top
 Profile  
 PostPosted: Fri Feb 13, 2009 9:26 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Honestly, I have no idea why it should react.
Probably something that brackets invokes in the builtin shell functions... like a condition statement of some sort.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Sat Apr 04, 2009 2:40 pm   

Joined: Sat Apr 04, 2009 2:32 pm
Posts: 1
To control the output of the echo command, you can specify a separator and extract words one by one. Here is your original script rewritten in biterscripting ( http://www.biterscripting.com for free download ) . You can follow the same logic in bash.

Code:
set $wsep = " "
var str x ; set $x="WORD [1.2.3.4]"
while ($x <> "")
do
var str y ; wex "1" $x > $y ; echo "y = " $y
done


The above should produce the exact same result irrespective of how or where from it is called.

Randi


Top
 Profile  
 PostPosted: Mon Apr 06, 2009 3:57 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 224
Location: London - UK
You could try and source more of the interactive environment into the script to see if you can pick out what the difference is.
Maybe the calling software generates an unusual environment.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 10 guests


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 post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP