Re: [SLUG] bash and stdin

From: Eben King (eben01@verizon.net)
Date: Sat Jul 28 2007 - 16:37:35 EDT


On Sat, 28 Jul 2007, Mike Branda wrote:

> Slug,
>
> I have a bash script I'm playing with that reads input from a pipe. I
> want to add some error handling to the effect of when the script is
> called by itself and no information is piped to it, it exits and/or
> exits and complains. The problem I have is that I'm doing a "while read
> line do" and if there's no data coming in, it sits there waiting on the
> read. I've tried to use a if [ -s /dev/stdin ] conditional before the
> while but to no success.
>
> Does anybody have an idea on this or on how to test stdin?
>
> examples:
>
> :~>cat lines.txt | myscript.sh -takeinput
> I've got input... echoing
> line 1
> line 2
> line 3
> :~>
> :~>myscript.sh -takeinput
> (sits here waiting for input or ctrl-c)

This might be of use to you:

BASH(1) BASH(1)
...
read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d
        delim] [name ...]
...
          -t timeout
                 Cause read to time out and return failure if a complete
                 line of input is not read within timeout seconds. This
                 option has no effect if read is not reading input from
                 the terminal or a pipe.

It'll cause a delay, and is sort of a guessing game to pick the right
timeout to allow manual entry of data or programs that wait a while, but
that's better than waiting forever IMO.

-- 
"The Web brings people together because no matter what kind of a
  twisted sexual mutant you happen to be, you've got millions of pals
  out there.  Type in 'Find people that have sex with goats that are on
  fire' and the computer will say, 'Specify type of goat.'" -- Rich Jeni
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS).  Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 18:13:06 EDT