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

后端 未结 1 1173
感动是毒
感动是毒 2021-01-05 09:13

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\")}\         


        
1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-05 09:30

    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.

    0 讨论(0)
提交回复
热议问题