evaluating variables


 
Thread Tools Search this Thread
# 1  
Question evaluating variables

Hi

I have a script in which I have several variables

var1
var2
var3
var4
etc......

and

field1
field2
field3
field4
etc.......

The script similar to this:

#!/usr/bin/ksh

counter=1
cmd="awk"

while read line
do

cmd=$cmd" -vvar$counter=$field$counter"

(( counter = counter + 1 ))

done < file

What I would like to do is evaluate the output of the cmd line to be:

awk -vvar1=<evaluated contents of field1> -vvar2=<evaluated contents of field2>

I have tried using eval and lots of different <a href="http://www.ntsearch.com/search.php?q=quotes&v=56">quotes</a> and braces to achieve this but haven't managed to get the correct output.

Can someone help please?

Many thanks
Helen
# 2  
Hmm, not too sure that I follow....

What is the content of "file" (i.e. that's providing STDIN to the while loop)?

Cheers
ZB
# 3  
Hmmm... Re-reading your question, i don't know if this'll help....

Code:
#!/usr/bin/ksh

counter=1
cmd="awk"

number_of_fields=4

while [[ $counter -le $number_of_fields ]]
do
  cmd=$cmd" -vvar$counter=\"$counter\""
  (( counter = counter + 1 ))
done

counter=1

cmd=$cmd" '{ print "
while [[ $counter -le $number_of_fields ]]
do
  cmd=$cmd"\$var$counter\" \" "
  (( counter = counter + 1 ))
done

cmd=$cmd" '} file"

eval $cmd

exit 0

But I still don't really understand the original question so your milage may vary. This code will print the first four fields of "file". It should give you a lead on the quoting necessary anyways.

Cheers
ZB
# 4  
Hi ZB

The contents of file doesn't really have any bearing on the question and is probably misleading. I'll try to phrase the question a bit differently.

I am trying to build up a dynamic string using a loop. I know the example script below is a bit pointless but hopefully it explains a little more clearly what I'm trying to do.

#!/usr/bin/ksh

counter=1
cmd="awk"
field1="Fred"
field2="Joe"
field3="Mary"
field4="Jane"


while [ $counter -le 4 ]
do
cmd=$cmd" -vvar$counter=$field$counter"

(( counter = counter + 1 ))
done

What I wanted to get is the contents of the variable 'cmd' to literally equal 'awk -vvar1=Fred -vvar2=Joe -vvar3=Mary -vvar4=Jane'

To do this I would need the $counter part of -vvar$counter to be evaluated to -vvar1, -vvar2 etc, but for the $field$counter I would need the variable evaluated twice. Firstly to $field1, $field2 etc then to the contents of $field1 (Fred), $field2 (Joe) etc

I hope this explains what I'm trying to achieve.

Cheers
Helen
# 5  
try:
eval eval cmd=$cmd" -vvar$counter=\$field$counter"
# 6  
Ah, I see.

How's about
Code:
#!/usr/bin/ksh

counter=1
cmd="awk"
field1="Fred"
field2="Joe"
field3="Mary"
field4="Jane"


while [ $counter -le 4 ]
do
field=field$counter
eval field=\$$field
cmd=$cmd" -vvar$counter=${field}"

(( counter = counter + 1 ))
done

echo $cmd

We get back
awk -vvar1=Fred -vvar2=Joe -vvar3=Mary -vvar4=Jane
from the "echo $cmd" line

EDIT: Looks like me and Perderabo were replying simultaneously!

Cheers
ZB
# 7  
Hi ZB/Perderabo

This has worked. Thanks for your help (again!).

Cheers
Helen Smilie
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #861
Difficulty: Medium
Vue.js was created by Evan Ewe.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem evaluating condition

First, given this bit of code (line numbers included for reference: 59 get_all_db () { 60 #echo getting all db 61 dblist=`egrep -i "product/12" /etc/oratab |grep -v "listener"|\ 62 awk -F\: '{print $1}'|sort` 63 echo list is $dblist 64 echo 65 echo 66 echo "INSTANCE_NAME ... (15 Replies)
Discussion started by: edstevens
15 Replies

2. UNIX for Dummies Questions & Answers

Comparing and Evaluating

Hi Guys, Good day ULF :) I hope you can help me again with my problem. I have a file which looks like this: Command was launched from partition 0. ------------------------------------------------ Executing command in server server3 Dec 18 21:31:12 AHM04 nseventmgr: EVENT-SET:... (4 Replies)
Discussion started by: rymnd_12345
4 Replies

3. Shell Programming and Scripting

Evaluating a variable

Does anyone know of a way to force a variable name held in another variable to return the value of the first variable? Best if I give an example, that does not work: /usr/local/bin >cat mike.sh NUM1ref=16 NUM2ref=32 echo "==============" for VAR in NUM1 NUM2 do XXXX=${VAR}ref echo $XXXX... (4 Replies)
Discussion started by: mikejordan
4 Replies

4. Shell Programming and Scripting

$$# is evaluating to 1 when no value

I have the following in my makefile: RESULT=`../${TOOLS_ROOT_PATH}/ext_tools.sh 11`; \ set $$RESULT > tMp; \ rm tMp; \ if ; then \ echo copying external-local tool: $< \($$*\); \ mkdir -p ${EXTERNAL_LOCAL_BIN_DIR}/$<; \ cp -f... (4 Replies)
Discussion started by: jake_ryan
4 Replies

5. Shell Programming and Scripting

K Shell evaluating value to a variable

Hi, I have the following requirement. V="First" R="V" echo $$R The output should be First. How do i achieve this. how do we evaluate the $R and evaluate it to $V as $R contains V and $V is First. Thanks Vijay (2 Replies)
Discussion started by: vijaykrc
2 Replies

6. UNIX for Dummies Questions & Answers

evaluating date +%m

how do i evaluate the value of date if ( $(date +%m) > 8 ) then FY_STAMP=FY$(echo $(($(date +%Y) + 1)) | cut -c3-4) else FY_STAMP=FY$(date +%y) fi i want this to make the FY_STAMP increment by 1 if the month is september and up. but cant seem to make it work (3 Replies)
Discussion started by: rsf01
3 Replies

7. Shell Programming and Scripting

* character evaluating too soon - Help!

I have a user defined configuration file, which could contain the following type of entries: directory_001=/a/directory/structure pattern_001=fred* pattern_002=* I have a script which reads the file generically which will loop round loop 1 genvar=”directory” iteration=”001” ... (11 Replies)
Discussion started by: Bab00shka
11 Replies

8. UNIX for Dummies Questions & Answers

evaluating params

Hi all, I ve a script like.... TBL=employee sql=`cat abhi.sql` \\ abhi.sql contains ------- select a from $TBL echo $TBL echo $sql SQL=`echo $sql` echo $SQL now i want SQL as select a from employee and as select a from $TBL How can I achieve this? Help appriciated (3 Replies)
Discussion started by: abzi
3 Replies

9. UNIX for Advanced & Expert Users

evaluating core files

Does anyone know any tools or how to really get something out of a core file. I can use strings and look for certain things like out of memory. I am trying to use adb but I can't make heads or tails from it. I guess it is my lack of know how with the adb/mdb debugger. anything would... (3 Replies)
Discussion started by: Gary Dunn
3 Replies

10. UNIX for Dummies Questions & Answers

evaluating for a number

I apologize for the simple question but can someone please help me with how to evaluate a number? I will be reading in a file and if a number is >= 100000000, I will do something, if not, I will exit the if statement. Thanks in advance (1 Reply)
Discussion started by: hedrict
1 Replies

Featured Tech Videos