Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk: setting environment variables directly from within an awk script

Tags:

awk

first post here, but been a lurker for ages. i have googled for ages, but cant find what i want (many abigious topic subjects which dont request what the topic suggests it does ...). not new to awk or scripting, just a little rusty :)

i'm trying to write an awk script which will set shell env values as it runs - for another bash script to pick up and use later on. i cannot simply use stdout from awk to report this value i want setting (i.e. "export whatever=awk cmd here"), as thats already directed to a 'results file' which the awkscript is creating (plus i have more than one variable to export in the final code anyway).

As an example test script, to demo my issue:

echo $MYSCRIPT_RESULT          # returns nothing, not currently set
echo | awk -f scriptfile.awk   # do whatever, setting MYSCRIPT_RESULT as we go
echo $MYSCRIPT_RESULT          # desired: returns the env value set in scriptfile.awk 

within scriptfile.awk, i have tried (without sucess)

1/) building and executing an adhoc string directly:

{
  cmdline="export MYSCRIPT_RESULT=1"
  cmdline
}

2/) using the system function:

{
  cmdline="export MYSCRIPT_RESULT=1"
  system(cmdline)
}

... but these do not work. I suspect that these 2 commands are creating a subshell within the shell awk is executing from, and doing what i ask (proven by touching files as a test), but once the "cmd"/system calls have completed, the subshell dies, unfortunatley taking whatever i have set with it - so my env setting changes dont stick from "the caller of awk"'s perspective.

so my question is, how do you actually set env variables within awk directly, so that a calling process can access these env values after awk execution has completed? is it actually possible?

other than the adhoc/system ways above, which i have proven fail for me, i cannot see how this could be done (other than writing these values to a 'random' file somewhere to be picked up and read by the calling script, which imo is a little dirty anyway), hence, help!

all ideas/suggestions/comments welcomed!

like image 465
user1603289 Avatar asked Aug 16 '12 13:08

user1603289


People also ask

How do I pass an environment variable in awk?

If you are in the middle of a text processing pipeline, and need to insert a shell or environment variable into the output of awk, you can use the “-v” flag. By passing the loop variable in using “-v”, it can be used in the awk output. This can be used for any shell or environment variable.

What does 1 mean in awk?

AWK works on method of condition and then action. So if any condition is TRUE any action which we mention to happen will be executed then. In case of 1 it means we are making that condition TRUE and in this case we are not mentioning any action to happen, so awk's by default action print will happen.

Can awk write to file?

Redirections in awk are written just like redirections in shell commands, except that they are written inside the awk program. This redirection prints the items into the output file named output-file . The file name output-file can be any expression.


2 Answers

You cannot change the environment of your parent process. If

MYSCRIPT_RESULT=$(awk stuff)

is unacceptable, what you are asking cannot be done.

like image 95
tripleee Avatar answered Jan 04 '23 00:01

tripleee


You can do this, but it's a bit of a kludge. Since awk does not allow redirection to a file descriptor, you can use a fifo or a regular file:

$ mkfifo fifo
$ echo MYSCRIPT_RESULT=1 | awk '{ print > "fifo" }' &
$ IFS== read var value < fifo
$ eval export $var=$value

It's not really necessary to split the var and value; you could just as easily have awk print the "export" and just eval the output directly.

like image 32
William Pursell Avatar answered Jan 04 '23 01:01

William Pursell