Register
It is currently Mon Sep 29, 2014 11:55 pm

Nested variable within a variable


All times are UTC - 6 hours


Post new topic Reply to topic  [ 7 posts ] 
Author Message
 PostPosted: Wed Nov 09, 2011 10:34 am   

Joined: Tue Sep 27, 2011 12:45 pm
Posts: 8
Below is my broken code. The problem is trying to read the original values.
Code:
x=1
y=2
z=3

for var in x y z
do
     if [ -n ${$var} ]
     then
          echo "$var: ${$var}"
     fi
done

Desired output:

x: 1
y: 2
z: 3


Top
 Profile  
 PostPosted: Wed Nov 09, 2011 11:05 am   
User avatar

Joined: Sat Jul 02, 2011 10:43 am
Posts: 23
Location: Devon,UK
Indirect variables are referenecd via the ! ie:
Code:
#!/bin/bash -e
x=1
y=2
z=3

for var in x y z
do
     if [ -n ${!var} ]
     then
          echo "$var: ${!var}"
     fi
done

gives
Code:
x: 1
y: 2
z: 3


Top
 Profile WWW  
 PostPosted: Wed Nov 09, 2011 1:25 pm   

Joined: Tue Sep 27, 2011 12:45 pm
Posts: 8
That helps a lot, thanks! Only problem is the -n doesn't seem to be working properly. For instance if you didn't specify 3 for variable z the output would be:

x: 1
y: 2
z:

I don't want the empty variables to print.


Top
 Profile  
 PostPosted: Wed Nov 09, 2011 1:37 pm   

Joined: Tue Sep 27, 2011 12:45 pm
Posts: 8
As a workaround I just swapped out the -n piece for

Code:
if [ "${!var}" != "" ]


Just curious why the -n didn't work.


Top
 Profile  
 PostPosted: Wed Nov 09, 2011 2:26 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 549
hi,

because you don't use enough quotes :
Code:
x=1 y=2
for var in x y z
do [ -n "${!var}" ] && echo "$var = ${!var}"
done


Top
 Profile  
 PostPosted: Wed Nov 09, 2011 4:06 pm   
User avatar

Joined: Sat Jul 02, 2011 10:43 am
Posts: 23
Location: Devon,UK
from the abs guide
Quote:
-n

string is not null.

Caution

The -n test requires that the string be quoted within the test brackets. Using an unquoted string with ! -z, or even just the unquoted string alone within test brackets (see Example 7-6) normally works, however, this is an unsafe practice. Always quote a tested string. [1]


so just use if [ -n "${!var}" ]


Top
 Profile WWW  
 PostPosted: Wed Nov 09, 2011 7:52 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
test(0)'s default flag is -n, so you don't have to specify it.

And since you're writing in bash(1), then just use the [[ ... ]] syntax:
Code:
if [[ ${!var} ]]; then


You don't have to quote the left-hand side.


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 8 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:  
cron


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