This isn't really possible, you can't rely the data being available by the
time you want to check if there is data to read. This is a race condition
that can only really be solved by putting in a timeout, which sucks.

testing if you have a tty is quite a common way to work around the issue

Thanks Steve


On Thu, Nov 21, 2013 at 2:43 AM, David Adam <zanc...@ucc.gu.uwa.edu.au>wrote:

> On Wed, 20 Nov 2013, Marcin Zajączkowski wrote:
> > I'm porting a script from bash to fish which reads stdin and I have a
> > problem with determining if there is anything to read from stdin or (n
> > the other case) input should be fetched from call params.
> >
> > There is in the original script:
> >      # If no tty, data should be available on stdin
> >      if ! [[ "$( tty )" == /dev/* ]]; then
> >        input="$(< /dev/stdin)"
> >      # Else, fetch input from params
> >      else
> >        input="$*"
> >      fi
> >
> > and in fact when used after pipe it returns "not a tty". In fish it
> > works fine for a simple execution:
> > $ echo "aaa" | tty
> > not a tty
> >
> > but when tty is called within my function it always returns /dev/pts/*.
>
> This mirrors the behaviour of your function above:
>
> ```
> function myfunc
>     if not tty >/dev/null
>         read input
>     else
>         set input $argv
>     end
>     echo $input
> end
> ```
> zanchey@motsugo ~> myfunc 1 2 3
> 1 2 3
> zanchey@motsugo ~> echo aaa | myfunc
> aaa
>
> > How can I determine that my function has something to on read stdin (to
> > not call "read" then which would open an interactive prompt)?
>
> This is a different question; the tty function does not check for the
> presence or absence of data on stdin. I don't actually know of a clever
> way of doing this from a shell script; in C I guess you'd do a select()
> with a zero timeout or something.
>
> David Adam
> zanc...@ucc.gu.uwa.edu.au
>
> ------------------------------------------------------------------------------
> Shape the Mobile Experience: Free Subscription
> Software experts and developers: Be at the forefront of tech innovation.
> Intel(R) Software Adrenaline delivers strategic insight and game-changing
> conversations that shape the rapidly evolving mobile landscape. Sign up
> now.
> http://pubads.g.doubleclick.net/gampad/clk?id=63431311&iu=/4140/ostg.clktrk
> _______________________________________________
> Fish-users mailing list
> Fish-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/fish-users
>
>
------------------------------------------------------------------------------
Shape the Mobile Experience: Free Subscription
Software experts and developers: Be at the forefront of tech innovation.
Intel(R) Software Adrenaline delivers strategic insight and game-changing 
conversations that shape the rapidly evolving mobile landscape. Sign up now. 
http://pubads.g.doubleclick.net/gampad/clk?id=63431311&iu=/4140/ostg.clktrk
_______________________________________________
Fish-users mailing list
Fish-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/fish-users

Reply via email to