awk variables --> shell variables 
Author Message
 awk variables --> shell variables

I'm trying to write a sh/awk script to verify the existance of directories.
I have a configuration file with the specified directories.

namehere,/dir1
something,/dir3

What I would like to do is parse the config file using awk, and extract the
directories to /bin/sh variables.  Its easy enough reading from /bin/sh
variables
to awk (awkvar1 = "'$ORACLE_HOME'").  But how do you go in reverse?
Does awk have the capability to write to /bin/sh variables?  One thing I've
already
tried is the use of :

nawk 'BEGIN {if (system("TEST1=" awkvar1) == 0 && system("export TEST1") ==
0)
    print "Success!" }'
echo $TEST1
---------------
all i get is:
Success!

-------------

That tells me its returning an exit status of 0, so its working?  But it
fails to echo my TEST1 shell variable, why?

Any help greatly appreciated!
Sean
Oracle DBA



Sat, 18 Jan 2003 03:00:00 GMT  
 awk variables --> shell variables
The "why" is relatively easy.  Each System statement is creating its own
child-shell that is "unaware" of the other.  To do something similar in
cgi programming, I did this:

<program creates assign statements in output file xxx.yyy>
chmod +x xxx.yyy
. xxx.yyy

The initial "." above indicates to ksh/posix shell scripts to run as
part of the existing shell, so all instructions are treated as part of
the executing scripts, so use this to write a program to create
environmental shells for your script.

This should get you started.


Quote:

> I'm trying to write a sh/awk script to verify the existance of
directories.
> I have a configuration file with the specified directories.

> namehere,/dir1
> something,/dir3

> What I would like to do is parse the config file using awk, and
extract the
> directories to /bin/sh variables.  Its easy enough reading from
/bin/sh
> variables
> to awk (awkvar1 = "'$ORACLE_HOME'").  But how do you go in reverse?
> Does awk have the capability to write to /bin/sh variables?  One thing
I've
> already
> tried is the use of :

> nawk 'BEGIN {if (system("TEST1=" awkvar1) == 0 && system("export
TEST1") ==
> 0)
>     print "Success!" }'
> echo $TEST1
> ---------------
> all i get is:
> Success!

> -------------

> That tells me its returning an exit status of 0, so its working?  But
it
> fails to echo my TEST1 shell variable, why?

> Any help greatly appreciated!
> Sean
> Oracle DBA

Sent via Deja.com http://www.deja.com/
Before you buy.


Sat, 18 Jan 2003 03:00:00 GMT  
 awk variables --> shell variables

Quote:

> What I would like to do is parse the config file using awk, and
> extract the directories to /bin/sh variables.  Does awk have the
> capability to write to /bin/sh variables?  One thing I've already
> tried is the use of :
> nawk 'BEGIN {if (system("TEST1=" awkvar1) == 0 && system("export TEST1") == 0)
>     print "Success!" }'
> echo $TEST1
> all i get is: Success!  That tells me its returning an exit status
> of 0, so its working?  But it fails to echo my TEST1 shell variable,
> why?

Can't really help you with system("export") -- though I do see a
potential problem above, in that the two system() calls are not
necessarily connected to a single shell session -- but here's one way
to set a shell environment variable (asssuming "sh" is the outer shell):

testx=`gawk 'BEGIN{print "Value of test"}'`
echo $testx

--



Sat, 18 Jan 2003 03:00:00 GMT  
 awk variables --> shell variables

Quote:



> > What I would like to do is parse the config file using awk, and
> > extract the directories to /bin/sh variables.  Does awk have the
> > capability to write to /bin/sh variables?  One thing I've already
> > tried is the use of :

> > nawk 'BEGIN {if (system("TEST1=" awkvar1) == 0 && system("export TEST1") == 0)
> >     print "Success!" }'
> > echo $TEST1
> > why?

system() results in an extra Shellprocess and its variable assignments are lost
when the system()
call is ready.

Quote:
> Can't really help you with system("export") -- though I do see a
> potential problem above, in that the two system() calls are not
> necessarily connected to a single shell session -- but here's one way
> to set a shell environment variable (asssuming "sh" is the outer shell):

> testx=`gawk 'BEGIN{print "Value of test"}'`
> echo $testx

If you want to assign values to a set of variables, you can get this in two steps
via the 'eval' - shell function:
$ set_this_vars=`nawk '{print "var1="value1;print "var2"=value2....} ..`
$ eval $set_this_var
and you will get
$ echo $var1 $var2
value1 value2

Quote:
> --


--
Quote of the month:
        Perl can do everything Awk can do, but not as elegantly.


Mon, 20 Jan 2003 03:00:00 GMT  
 awk variables --> shell variables
Perfect! Thanks...


Quote:



> > > What I would like to do is parse the config file using awk, and
> > > extract the directories to /bin/sh variables.  Does awk have the
> > > capability to write to /bin/sh variables?  One thing I've already
> > > tried is the use of :

> > > nawk 'BEGIN {if (system("TEST1=" awkvar1) == 0 && system("export
TEST1") == 0)
> > >     print "Success!" }'
> > > echo $TEST1

> > > why?

> system() results in an extra Shellprocess and its variable assignments are
lost
> when the system()
> call is ready.

> > Can't really help you with system("export") -- though I do see a
> > potential problem above, in that the two system() calls are not
> > necessarily connected to a single shell session -- but here's one way
> > to set a shell environment variable (asssuming "sh" is the outer shell):

> > testx=`gawk 'BEGIN{print "Value of test"}'`
> > echo $testx

> If you want to assign values to a set of variables, you can get this in
two steps
> via the 'eval' - shell function:
> $ set_this_vars=`nawk '{print "var1="value1;print "var2"=value2....} ..`
> $ eval $set_this_var
> and you will get
> $ echo $var1 $var2
> value1 value2

> > --

> --
> Quote of the month:
>         Perl can do everything Awk can do, but not as elegantly.



Mon, 20 Jan 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. AWK-Script to set a shell variable (Win32) ?

2. Passing command line variables to AWK in shell script

3. setting shell variables with results from awk

4. Exporting awk variables to the UNIX shell

5. help for newbie: way to get the value of an awk variable in a shell script

6. how to use a shell variable in awk for pattern matching

7. Shell variables in AWK

8. Quoting Swamp: Saving an awk command in a shell variable

9. How to pass shell variable to awk?

10. Getting awk computed value into shell variable

11. how to print a shell variable in awk states

12. accessing a shell variable in an awk print statement

 

 
Powered by phpBB® Forum Software