Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're looking at this the wrong way, I think. Awk's <code>system</code> just takes any old string, so give it one, e.g.:</p> <pre><code>system("echo " var); # see side note below </code></pre> <p>(remember that in awk, strings are concatenated by adjacency). Moreover, <code>system</code> just runs a command; to capture its output, you need to use <code>getline</code>, similar to your question #1.</p> <p>If you want to read all the output of <code>ls</code> you need to loop over the result from <code>getline</code>:</p> <pre><code>awk 'BEGIN { while ("ls" | getline var) print "I got: " var; }' </code></pre> <p>Since this defines only a <code>BEGIN</code> action, <code>awk</code> will start up, run <code>ls</code>, collect each output line and print it, and then exit.</p> <hr> <p>Side note: be very careful with variables passed to a shell (this includes both calls to <code>system</code> and items on the left hand side of <code>| getline</code>, plus some other cases in modern varieties of <code>awk</code>—anything that runs a command). Backquotes, <code>$(command)</code>, and semicolons can all allow users to invoke arbitrary commands. For instance, in the <code>system("echo " var)</code> example above, if <code>var</code> contains <code>; rm -rf $HOME</code> the command becomes <code>echo ; rm -rf $HOME</code>, which is almost certainly not something you want to have happen.</p> <p>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), <code>$</code>, <code>&lt;</code>, <code>&gt;</code>, <code>|</code>, and <code>`</code>. If you use single quotes to quote arguments (to make them appear as a single "word"), you need only escape the single quotes. See <a href="https://unix.stackexchange.com/a/64218">this unix.stackexchange.com answer</a> for more details.</p> <hr> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload