Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
awk '{ TEMPVAR="/usr/bin"; printf("%s", system("ls $TEMPVAR")); }' empty

In this example I'm trying to bring in the variable TEMPVAR into the system call. How would I do this?

What I'm aiming to do: I'm trying to use date -d $0 +%s in a system call that occurs every line of a file. However, I'm struggling with how to get that $0 value into the system call.

share|improve this question

2 Answers

up vote 4 down vote accepted

awk can access environment variables using the ENVIRON special array. However, while you can assign values to elements of that array, it is not passed in the environment of the commands executed by awk's system, | getline or print |. That ENVIRON array is only intended for awk to get the value of the environment variables it is passed.

You can do: system("ls " var), but beware that the string that is passed to awk's system() (or print | or | getline) is actually passed as an argument to sh -c, so it is interpreted as shell code.

For instance, if the awk var variable contains foo;rm -rf /, it will not tell ls to list the file called "foo;rm -rf /" but instead to list the file called foo and then the rm command will be run.

So, you may need to escape all the characters special to the shell in that var variable.

This could be done for instance with:

awk '
  function escape(s) {
    gsub(/'\''/, "&\\\\&&", s)
    return "'\''" s "'\''"
  }
  {
    cmd = "date -d " escape($0) " +%s"
    cmd | getline seconds
    close(cmd)
    print seconds
  }'

While that means running one shell and one date command per line, you might as well do the reading of the file with the shell itself:

while IFS= read <&3 -r line; do
  date -d "$line" +%s
done 3< the-file
share|improve this answer

The answer came from this: http://stackoverflow.com/questions/10008407/awk-transmission-of-a-variable-with-getline-to-system

Basically create a string contactination within the system call.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.