echoing two variables in one statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting echoing two variables in one statement
# 1  
Old 05-09-2006
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.
# 2  
Old 05-09-2006
${var}${ta}
# 3  
Old 05-10-2006
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  
Old 05-10-2006
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  
Old 05-10-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Difficulty with incrementing Variables and using the results in a If/else statement

Environment: BASH version: GNU bash, version 3.2.51(1)-release (sparc-sun-solaris2.10) Copyright (C) 2007 Free Software Foundation, Inc. OS: Oracle Solaris 10 9/10 s10s_u9wos_14a SPARC Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. ... (4 Replies)
Discussion started by: os2mac
4 Replies

2. Shell Programming and Scripting

comparing variables in an if statement

#!/bin/bash #timetest TIMENOW="$(date)" T1=12:00:00 echo $TIMENOW >timenow cat timenow |cut -f4 -d' ' >time1 T2=$(sed -n "${1}p" time1) echo "T1 = " $T1 echo "T2 = " $T2 if then echo $T1 else echo $T2 fi I thought scripting was simple! So why does this script result in: T1 =... (4 Replies)
Discussion started by: habuchas
4 Replies

3. UNIX for Dummies Questions & Answers

variables in awk statement

Hi, i can print 2nd, 4th and 6th columns in a file using the shell command: awk -F "|" '{print $2 $4 $6}' file1.txt Now, the position and number of columns can be variable and from a variable POSLIST So let's say POSLIST='$2 $4 $6' when i use the command: a) awk -F "|" '{print... (2 Replies)
Discussion started by: ysrini
2 Replies

4. UNIX for Dummies Questions & Answers

Initializing multiple variables in one statement

HI, I have 5 variables var1, var2, var3, var4 and var5 I need to initialize all of them to zero. Is there a way to do it in a single line something like this var1=var2=var3=var4=var5=0. I am unable to achieve this. What is going wrong? (2 Replies)
Discussion started by: paku
2 Replies

5. Shell Programming and Scripting

Echoing weird variables.

kindly find below:- var="'(]\\{}\$\"" echo $var # it wil give this '(]\{}$" echo "$var" # '(]\{}$" Doesn't make a difference why???. if we set IFS to '\' also the below happens IFS='\' echo $var # '(] {}$" \ converted to space. Why? weird!! echo "$var"... (3 Replies)
Discussion started by: ahmad.diab
3 Replies

6. Shell Programming and Scripting

using variables in case statement

is it possible to call a variable in a case statement, for example lsmonth=Jan|Feb l |while read ans do mymonth=`echo $ans |awk '{print $6}'` case $mymonth in $lsmonth) echo do something ;; *) echo do something else ;; esac done I want to use $lsmonth... (8 Replies)
Discussion started by: gefa
8 Replies

7. Shell Programming and Scripting

Using two shell variables in single AWK statement

meas is a shell variable, and this works perfectly fine for me: awk -v var=$meas -F, '$1==var' /abcd/efgh.txt > temp1.csv However, i want to introduce another shell variable, named, defnfile in the statement, in place of hardcoded path for efgh.txt like: awk -v var=$meas -F, '$1==var'... (3 Replies)
Discussion started by: indianjassi
3 Replies

8. Shell Programming and Scripting

I can't seem to pass variables properly into a nawk statement

Ok, So up front I'm going to say that I'm a very elementary scripter, and I tend to use tools I don't fully understand, but I shotgun at something until I can get it to work...that said, I can't for the life of me understand why I can't get this to go down the way I want it to. The goal: -to... (6 Replies)
Discussion started by: DeCoTwc
6 Replies

9. Shell Programming and Scripting

Variables within a sed statement

I am just wondering if it's possible to refer to variables within a sed statement as follows:- cat $file | sed -e 1's/$oldtext/$newtext/' > $file as when I run the script, the variables are not recognised and nothing happens..?? Thanks (5 Replies)
Discussion started by: sirtrancealot
5 Replies

10. UNIX for Dummies Questions & Answers

How can I put wildcards in an if statement that uses variables?

With the if statement: if How can I make it so it accepts a wildcard after the ${CURR_DAY_MONTH} variable? Putting a -f /webtrends/SUN/mrw2/access.${CURR_DAY_DAY}${CURR_DAY_MONTH}* won't work, right? I think I need some kind of special character so it knows the wildcard is... (3 Replies)
Discussion started by: LordJezo
3 Replies
Login or Register to Ask a Question