On 2/19/2012 7:53 PM, Kenneth Bull wrote:
> 1)
> Why does "sleep 1000 | ls " not wait 1000 seconds before running ls?
> 'ls' runs immediately.
> How does ls or the shell know that the program on left will never send
> "ls" anything on the pipe?
"ls" never reads its stdin, hence never needs to wait for
the pipe to utter anything (not even to utter EOF). Perhaps
you want "sleep 1000 ; ls" instead.
> 2)
> When making filters that work on both files and programs, do we ever
> have to consider \t character? The problem is, reading a file the
> filter might get the \t character, but reading a tabbed stream from a
> program would look like a bunch of spaces instead.
No comprendo. A tab is a tab is a '\t', and there's an end
on't. The visual appearance of a tab on a terminal may be one or
more spaces (or simply horizontal space with no defined character
count), but '\t' is still '\t'. Consider: '\n' is still '\n', even
though it "prints" an infinite number of blank spaces at the end of
its line. If you don't worry about the infinite number of spaces
after '\n', you shouldn't worry about what '\t' looks like.
> 3)
> Is this how the shell works if I call an external program "prog"?
> Shell forks itself,
> The child calls exec "prog"
> The parents calls waitpid(child's pid)
> ?
Details may vary, but basically: Yes.
> 4) How does "foreground" and "background" work with the shell? Are
> there unix systems calls to implement this?
You've already mentioned them. The only question is whether
the parent shell does or does not wait for the child.
> How does the parent shell go into background and push the child into
> foreground in question 3 above? I am guessing what really is happening
> is just a reshuffling of stdin/stderr/stdout to implement the
> foreground/background function?
No, the I/O attachments have nothing to do with it. Not sure
what you're really asking about.
> 5)
> When we do "prog 1; prog2" in shell, does the shell fork 2 childs to
> start with (and 2nd child is forced to wait somehow) or does the shell
> have a table of "to be done commands in future"?
Aw, snap: Don't remember. Read about it once, somewhere, but
don't remember. Scarcely matters, unless you're implementing a
shell -- in which case you get to make your own rules.
> 6)
> Why do people do if [ "x"$string == "x" ] instead of if[ $string ==
> "" ] in sh scrips?
In case `string' is null. "$string" == "" is similar.
> Thank you very much
You're welcome 1) 2) 4) 6).
--
Eric Sosman
eso...@ieee-dot-org.invalid