AWK - Transmission of a variable with getline to system ()?

Tedee12345 picture Tedee12345 · Apr 4, 2012 · Viewed 14.5k times · Source

I have a theoretical question:

1) How pass a variable to the system of getline ()?

awk 'BEGIN{var="ls"; var | getline var; system("echo $var")}'

2) How to assign a variable the output system ("ls") and print the result in awk?

awk 'BEGIN{var="system("ls")"; print '$var'}'

3) Can you assign a variable in the system (var = "ls") and print the result in awk?

awk 'BEGIN{system(var="ls"); print "'"$var"'"}'

Thank you for the information.

EDIT:

torek: Thank you for your response.

I understand that in the first example, you can do this:

awk 'BEGIN { while ("ls -l" | getline var) system("echo " var  );}'

For this application, you can not assign a variable output from system ()? As in this example:

awk 'BEGIN {var="ls -l"; system(var); print var}'

Answer

torek picture torek · Apr 4, 2012

You're looking at this the wrong way, I think. Awk's system just takes any old string, so give it one, e.g.:

system("echo " var);  # see side note below

(remember that in awk, strings are concatenated by adjacency). Moreover, system just runs a command; to capture its output, you need to use getline, similar to your question #1.

If you want to read all the output of ls you need to loop over the result from getline:

awk 'BEGIN { while ("ls" | getline var) print "I got: " var; }'

Since this defines only a BEGIN action, awk will start up, run ls, collect each output line and print it, and then exit.


Side note: be very careful with variables passed to a shell (this includes both calls to system and items on the left hand side of | getline, plus some other cases in modern varieties of awk—anything that runs a command). Backquotes, $(command), and semicolons can all allow users to invoke arbitrary commands. For instance, in the system("echo " var) example above, if var contains ; rm -rf $HOME the command becomes echo ; rm -rf $HOME, which is almost certainly not something you want to have happen.

You can check for "bad" characters and either object, or quote them. Modern 8-bit-clean shells should only require quoting quotes themselves (for syntactic validity), $, <, >, |, and `. If you use single quotes to quote arguments (to make them appear as a single "word"), you need only escape the single quotes. See this unix.stackexchange.com answer for more details.


One other side note: I tend to add "unnecessary" semicolons to my awk scripts, making them look more like C syntactically. Old habit from decades ago.