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



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-09-2006
Registered User
 

Join Date: Jan 2006
Posts: 37
echoing two variables in one statement

I have the following
--------------------
foreach var (STO SNY WKF)

set ta = 5

end

---------

How can I echo both variables at the same time. Something to the effect of

echo ${$var}ta

But this doesn't work. Seems like it would. Thanks.
Sponsored Links
  #2 (permalink)  
Old 05-09-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,200
${var}${ta}
  #3 (permalink)  
Old 05-10-2006
Registered User
 

Join Date: Jan 2006
Posts: 37
Yeah I didn't phrase my question the way I wanted to. Let me try again.

Given the following:
----------------------
foreach var (t v w)

grep $var ./file > ./newfile

set ${var}temp = `awk '{print $2}' ./newfile`


now this is where I need help
how can I echo each variable with only one echo statement, essentially, do:

echo $ttemp $vtemp $wtemp

But doing this with alot of variables, creates a long script, and is not very efficient.

I've tried the following

echo ${$var}temp, this doesn't work

end

Hopefully this makes more sense than before.

Thanks in advance.
  #4 (permalink)  
Old 05-10-2006
Registered User
 

Join Date: Mar 2006
Location: South Yorkshire, UK
Posts: 114
I may not have understood the problem correctly - and it looks a bit artificial, not homework is it? - but it looks as if you trying to set variables where the variable name is created from the loop variable (t, v, w) and 'tmp' (i.e. ttmp, vtmp, wtmp)?
If so then you need to use 'eval' to do the substitutions for the variable name before assigning it a value. Something like:

Code:
for var in t v w do
  grep $var ./file > ./newfile
  eval ${var}tmp=`awk '{print $2}' ./newfile`
  eval print ${var}tmp set
done

Note that this will fail if the 'grep' finds more than one matching line

If I've got hold of the wrong end of the twig then try a more detailed explanation of what you are trying to achieve

cheers

Steve
  #5 (permalink)  
Old 05-10-2006
Registered User
 

Join Date: Jan 2006
Posts: 37
Quote:
Originally Posted by thestevew
I may not have understood the problem correctly - and it looks a bit artificial, not homework is it? - but it looks as if you trying to set variables where the variable name is created from the loop variable (t, v, w) and 'tmp' (i.e. ttmp, vtmp, wtmp)?
If so then you need to use 'eval' to do the substitutions for the variable name before assigning it a value. Something like:

Code:
for var in t v w do
  grep $var ./file > ./newfile
  eval ${var}tmp=`awk '{print $2}' ./newfile`
  eval print ${var}tmp set
done

Note that this will fail if the 'grep' finds more than one matching line

If I've got hold of the wrong end of the twig then try a more detailed explanation of what you are trying to achieve

cheers

Steve

thanks for the help, don't worry it isn't homework.
Sponsored Links
Closed Thread

Bookmarks

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 Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
I can't seem to pass variables properly into a nawk statement DeCoTwc Shell Programming and Scripting 6 03-30-2008 03:07 PM
Echoing Okema Shell Programming and Scripting 3 03-12-2008 11:33 PM
echo not echoing correctly shorty UNIX for Dummies Questions & Answers 3 09-25-2006 05:45 PM
Variables within a sed statement sirtrancealot Shell Programming and Scripting 5 07-18-2006 05:41 PM
How can I put wildcards in an if statement that uses variables? LordJezo UNIX for Dummies Questions & Answers 3 06-14-2004 01:27 PM



All times are GMT -4. The time now is 02:07 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-2010. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0