The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
use variable for sed print line atchleykl UNIX for Dummies Questions & Answers 3 02-23-2009 01:49 PM
How to print a value in the variable using awk ? jisha Shell Programming and Scripting 1 01-14-2008 07:45 AM
can awk print column using a variable ?? jambesh Shell Programming and Scripting 36 09-26-2006 07:39 AM
print the name of variable sun-guy Shell Programming and Scripting 1 08-06-2006 05:03 PM
print variable in awk kazanoova2 Shell Programming and Scripting 8 05-17-2004 06:30 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-17-2008
aravindc aravindc is offline
Registered User
  
 

Join Date: Apr 2008
Posts: 2
Unhappy Print variable of a variable

Hi,

I'm trying the following:

Code:
code_0_0=1001
code_0_1=Test1
code_1_0=1002
code_1_1=Test1

x=1001

while [ $i -le 1 && $x -ne $code_$i_0 ]
do
i=`expr i + 1`
done

echo The Value Matched at $i variable and has value $code_$i_1
Please let me know where the problem in above code

Regards,
Aravind. C
  #2 (permalink)  
Old 04-18-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
You want the value of $code_$i_1 where $i is another variable?

Code:
eval echo The value matched at \$i variable and has value \$code_${i}_1
  #3 (permalink)  
Old 04-18-2008
krishmaths krishmaths is offline
Registered User
  
 

Join Date: Sep 2006
Location: Mysore, India
Posts: 191
Also, in the increment step, you have missed a $.

i=`expr $i + 1`
  #4 (permalink)  
Old 04-19-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by aravindc View Post
Hi,

I'm trying the following:

Code:
code_0_0=1001
code_0_1=Test1
code_1_0=1002
code_1_1=Test1

x=1001

while [ $i -le 1 && $x -ne $code_$i_0 ]
do
i=`expr i + 1`
done

echo The Value Matched at $i variable and has value $code_$i_1
Please let me know where the problem in above code

First, you haven't initialized $i.

Then, there are two problems in the test statement. The first is the attempt to use an indirect variable.

If you are using bash, you can build the name of the variable and use the ! expansion:

Code:
iv=code_${i}_0
val=${!iv}
For other shells (and for portability), use eval:

Code:
eval "val=\$code_${i}_0"
In both of the above examples, note the braces around the 'i' to prevent the variable being interpreted as $i_0.

You cannot use && inside a test expression; either use -a or two separate test commands:

Code:
while [ $i -le 1 -a $x -ne $val ]
Or:

Code:
while [ $i -le 1 ] && [ $x -ne $val ]
Closed Thread

Bookmarks

Tags
bash, bash eval, eval

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0