Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Note that the first example is dependent on the shell. The pipe is an IPC (inter-process communications) operator and the shell can implement that by creating a subshell on either side of the pipe. (Technically I suppose some shell could even evaluate both sides in separate sub-processes).</p> <p>The <code>read</code> command is a built-in (must, inherently be so). So, in shells such as <code>bash</code> and the classic Bourne shell derivatives the suprocess (subshell) is on the right of the pipe (reading from the current shell) and that process ends after its <code>read</code> (at the semicolon in this example). Korn shell (at least as far back as '93) and <code>zsh</code> put their subshell on the other side of the pipe and are reading data from those into the current process.</p> <p>That's the point of the interview question. </p> <p>The point of my question here is to look for some consensus or metric for how highly to rate this level of question. It's not a matter of trivia because it does affect real world scripts and portability for shell scripting and it relies upon fundamental understanding of the underlying UNIX and shell semantics (IPC, pipes, and subprocesses/subshells).</p> <p>The second example is similar but more subtle. I will point out that the following change "works" (the <code>ssh</code> will execute on each of the hosts in the file):</p> <pre><code>cat $SOME_FILE | while read host; do ssh "$host" "$some_cmd" &lt; /dev/null; done </code></pre> <p>Here the issue is that the <code>ssh</code> command buffers up input even if the command on the remote never reads from its <em>stdin</em>. Because the shell/subshell (reading from the pipe) and the <code>ssh</code> are sharing the same input stream, the <code>ssh</code> is "stealing" most of the input from the pipeline, leaving only the occasional line for the <code>read</code> command.</p> <p>This is not an artificial question. I actually encountered it in my work and had to figure it out. I know from experience that understanding this second example is at least a notch or two above the first. I also know, also from years of experience, that fewer than 10% of the candidates (for sysadmin and programming positions) that I've interviewed can get the first question right away).</p> <p>I've never used the second question in a live interview and I've been discouraged from doing so.</p>
    singulars
    1. This table or related slice is empty.
    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. 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