Re: c.u.s. FAQ, take two

From: Jeremiah DeWitt Weiner (jdw_at_panix.com)
Date: 07/21/03


Date: Mon, 21 Jul 2003 20:53:24 +0000 (UTC)

joe@invalid.address wrote:
> 23. How do I get a process id by process argv[0]?

> This depends on the shell being used. sh and Bourne derived shells
> use '.' (period/dot) to indicate that the argument should be read
> into the current shell, while csh and csh derived shells use
> "source" for the same thing.

        This looks like something from the next section (24. How do I
get a script to update my current environment?) made it into this one.

        I would have said "use pgrep if you're on Solaris, and
'ps h -o pid -C $STRING' if you're not and your ps supports that syntax"
but neither of those strikes me as perfect and/or ubiquitous.

> 28. How do I get one character input from the user?

> TBD

OLDSTTY=$(stty -g) # save our terminal settings
stty cbreak # enable independent processing of each input character
ONECHAR=$(dd bs=1 count=1 2>/dev/null) # read one byte from standard in
stty $OLDSTTY # restore the terminal settings

        Use the `something` format if your shell doesn't understand
$(something). This reads from standard input, which may or may not
be desirable. If you want to read from the terminal regardless of
where standard input is, add "if=$(tty)" to the dd command.

        I think this is a reasonably accepted and portable method of
doing this - maybe there's a problem with it that hasn't occurred to
me, though.


Loading