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}'
You’re looking at this the wrong way, I think. Awk’s
systemjust takes any old string, so give it one, e.g.:(remember that in awk, strings are concatenated by adjacency). Moreover,
systemjust runs a command; to capture its output, you need to usegetline, similar to your question #1.If you want to read all the output of
lsyou need to loop over the result fromgetline:Since this defines only a
BEGINaction,awkwill start up, runls, 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
systemand items on the left hand side of| getline, plus some other cases in modern varieties ofawk—anything that runs a command). Backquotes,$(command), and semicolons can all allow users to invoke arbitrary commands. For instance, in thesystem("echo " var)example above, ifvarcontains; rm -rf $HOMEthe command becomesecho ; 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.