Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Testing if there is data on stdin?
- X-seq: zsh-users 14404
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: Lloyd Zusman <ljz@xxxxxxxxxx>
- Subject: Re: Testing if there is data on stdin?
- Date: Fri, 18 Sep 2009 01:47:40 +0200
- Cc: zsh-users@xxxxxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=Fs0Xl7Q2a9ctNPwmQyDxdAV5NFJPcq+fI3/N6VMTizY=; b=PpkP5OkpnmSyx2FJG9qbH4Japym66uoQPvEQ2E/e418t1sXYwax5EBS7KSf3LM5L/n suP0rl4nSnxmo00u1sJUbEWFBugbgO1kTwemGxZnRd+efWmYVTr44hQjifa3qKSDfZX7 A7AeG+F90w6dRqL465fQWCxC2Qy8YeSgeZnhI=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=omhSBolqN9jmmrjfgqbj0Y7WdhsZiMMMuui6wSMeYZsE1oOb0x06vUHn2G9Hztk+hw W5JVWWO8XXRXFdrGQ3/U59ZvgfrSAMSNwlh5ZKdGb7iQzfGc+buQcfCVPsJd4NQQ8gqP GQOnBAfQnl7e1kjutFVHtbGdqJxd6HgNJW9rI=
- In-reply-to: <m2ocp9ma6k.fsf@xxxxxxxxxx>
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <m2ocp9ma6k.fsf@xxxxxxxxxx>
2009/9/18 Lloyd Zusman <ljz@xxxxxxxxxx>:
> Is there a way in zsh to test whether there is at least one byte of data
> waiting to be read from stdin without actually reading that data?
>
> I know I can do a [[ -t 0 ]] to see whether stdin is attached to a
> terminal, but that doesn't tell me if there is any data waiting to
> be read.
>
> The reason I want to do this is because I have a zsh script which, under
> certain circumstances, needs to get its arguments via stdin instead of
> from the command line. I'd like to be able to test within the script
> whether there is data waiting on stdin, and if so, to read it and use it
> to construct the argument list. If there is no data waiting on stdin,
> then I just want to get the arguments from the command line.
>
> Also, there is a slight difference in processing logic if I find data in
> stdin, and I need to do a little bit of special initialization _before_
> I try to read that data ... but only if that data is on stdin waiting to
> be read.
>
> Any suggestions as to how I might do this? I couldn't find anything in
> the zsh docs, but I could easily have overlooked something.
>
> Thanks in advance.
You can either use
zmodload zsh/zselect
zselect -t0 0
or
read -t0
I have a feeling the code can get pretty complicated quickly, since
both of those will return true if there's one byte of data, then you
have to recheck after each character you read so it won't suddenly
block (or use a loop of read -t -k). Unless I'm overlooking some
clever solution.
--
Mikael Magnusson
Messages sorted by:
Reverse Date,
Date,
Thread,
Author