passing vars to shell script 
Author Message
 passing vars to shell script

I've seen how to pass variables into an awk script but not the other way
around.

I have a csh script that reads data from a comma delimited file and would
like to use awk to assign these variables and pass them on to the csh
script.

Thanks,



Tue, 22 May 2001 03:00:00 GMT  
 passing vars to shell script


Quote:

>I've seen how to pass variables into an awk script but not the other way
>around.

>I have a csh script that reads data from a comma delimited file and would
>like to use awk to assign these variables and pass them on to the csh
>script.

Probably more of a shell question than an awk one. Given comma-separated file
var_file containing var_name, var_value pairs (I'll assume var_value entries
are _not_ within quotes), try the following csh script fragment:

awk -F, '{ printf("set %s = \"%s\"\n", $1, $2) }' var_file > ~/mytemp
source ~/mytemp
rm > ~/mytemp

This could be simplified by using sed rather than awk. I'm rusty on csh, so I
hesitate to say you can put this into a pipe rather than use a temporary file.
This sets variables as an entire batch.

If var_file contains {var1_value, var2_value, . . . , varN_value} records that
you want to process one record at a time, your csh script needs to feed your
awk script one line from var_file at a time , and it would look like

    awk -F, 'BEGIN { var[1] = "var1"; var[2] = "var2"; # etc. }
    { for (i = 1;i <= NF;i++) printf("set %s = \"%s\"\n",var[i],$i) }' var_file

Quote:
> ~/mytemp

    source ~/mytemp
    rm ~/mytemp
    # do other stuff
end


Wed, 23 May 2001 03:00:00 GMT  
 passing vars to shell script


Quote:

>I've seen how to pass variables into an awk script but not the other way
>around.

>I have a csh script that reads data from a comma delimited file and would
>like to use awk to assign these variables and pass them on to the csh
>script.

Probably more of a shell question than an awk one. Given comma-separated file
var_file containing var_name, var_value pairs (I'll assume var_value entries
are _not_ within quotes), try the following csh script fragment:

awk -F, '{ printf("set %s = \"%s\"\n", $1, $2) }' var_file > ~/mytemp
source ~/mytemp
rm > ~/mytemp

This could be simplified by using sed rather than awk. I'm rusty on csh, so I
hesitate to say you can put this into a pipe rather than use a temporary file.
This sets variables as an entire batch.

If var_file contains {var1_value, var2_value, . . . , varN_value} records that
you want to process one record at a time, your csh script needs to feed your
awk script one line from var_file at a time in some variable, call it thisline.
Then try this:

echo thisline | awk -F, 'BEGIN { var[1] = "var1"; var[2] = "var2"; # etc. }
{ for (i = 1;i <= NF;i++) printf("set %s = \"%s\"\n",var[i],$i) }' > ~/mytemp
source ~/mytemp
rm ~/mytemp



Wed, 23 May 2001 03:00:00 GMT  
 passing vars to shell script

% I've seen how to pass variables into an awk script but not the other way
% around.

You can use eval in both csh and sh.

csh:
  eval `awk 'BEGIN { myvar=20; print "set x=\"" myvar "\"" }'`

sh:
  eval `awk 'BEGIN { myvar=20; print "x=\"" myvar "\"" }'`

The awk command can be in a script. You can set as many values as you like
by adding additional x=y pairs, so your script could be like this:

 { vals[$1]=$2 }
 END { print "set"    # you need this for csh, not for sh, ksh, et al
       for (a in vals)
         print a "=" vals[a]
 }

which you could execute as
 eval `awk -f setvals.awk` listovalues
--

Patrick TJ McPhee
East York  Canada



Wed, 23 May 2001 03:00:00 GMT  
 passing vars to shell script

Quote:
> I have a csh script that reads data from a comma delimited file and would
> like to use awk to assign these variables and pass them on to the csh
> script.

cat theCommaDelimitedDataFile |awk 'BEGIN {FS="'"}
{   for (f=1;f<=NF;f++) {print $(f)}   }'  |
while read shVariable
do
  echo ${shVariable} ': Action you do with '  ${shVariable}
done

not tested
 LMS
free sed/awk book:
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.ps.gz
      ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.tar.gz



Thu, 24 May 2001 03:00:00 GMT  
 passing vars to shell script


% ....
% > which you could execute as
% >  eval `awk -f setvals.awk` listovalues
%
% Shouldn't listovalues be inside the backquotes (or grave accents)?

Yeah, well, ..., that was left as an exercise ....
--

Patrick TJ McPhee
East York  Canada



Sat, 26 May 2001 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. passing shell vars to gawk

2. Shell vars into Awk executable script not so easy

3. How to use shell vars in a script?

4. passing vars to awk script

5. Newbie - passing variable number of vars to expect script

6. Passing command line variables to AWK in shell script

7. passing variable to shell script

8. Scheme Shell: Shell Scripting for the 90's

9. Scheme Shell: Shell Scripting for the 90's

10. Scheme Shell: Shell Scripting for the 90's

11. Scheme Shell: Shell Scripting for the 90's

12. Scheme Shell: Shell Scripting for the 90's

 

 
Powered by phpBB® Forum Software