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
pass variable from awk to shell script user_prady Shell Programming and Scripting 3 04-17-2008 05:43 AM
How to pass Shell script variable to awk HIMANI UNIX for Dummies Questions & Answers 3 07-16-2007 12:23 AM
transfer a variable in a shell script? fedora Shell Programming and Scripting 11 09-22-2006 12:56 PM
How to Pass variable to shell Script sam70 UNIX for Dummies Questions & Answers 5 08-23-2005 07:27 PM
passing awk variable to the shell script bcheaib Shell Programming and Scripting 3 07-21-2004 10:00 AM

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 08-07-2008
arvindng arvindng is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 24
Smile shell script Variable

hi all,

i want use the variable value as a new variable name. And i want to use this new variable value


for i in COMPUTER1 COMPUTER2
do
flag_name=${i}_FLAG
eval ${flag_name}=123
echo $i'_FLAG'
done

output is
COMPUTER1_FLAG
COMPUTER2_FLAG

i need output as 123
  #2 (permalink)  
Old 08-07-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 need an eval around the echo too. And a literal dollar sign in front of the variable.

Code:
eval echo \$${i}_FLAG
It will print 123 twice, of course, when the loop runs twice.
  #3 (permalink)  
Old 08-07-2008
arvindng arvindng is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 24
Its Working. Thank U
  #4 (permalink)  
Old 08-07-2008
arvindng arvindng is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 24
having problem with if condiation

hi era,

your given code (eval echo \$${i}_FLAG) is woking fine.

now i want to use the variable value to in if condiation..
is following code is right. please help


for i in COMPUTER1 COMPUTER2
do
flag_name=${i}_FLAG
eval ${flag_name}=1
if [ `eval echo \$${i}_FLAG` -eq 1 ]
then
echo yes
else
echo no
fi
done
  #5 (permalink)  
Old 08-07-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
With the backticks, you will be requiring even more backslashes. But I'd recommend against using backticks, and "if test -eq".

Code:
eval 'case $'${i}'_FLAG in 1) echo yes;; *) echo no;; esac'

Last edited by era; 08-07-2008 at 02:24 AM.. Reason: Oops, fix quoting
Closed Thread

Bookmarks

Tags
eval, variable name from variable

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 04:39 AM.


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