help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: While loop and stdin


From: Zachary Santer
Subject: Re: While loop and stdin
Date: Sun, 25 Feb 2024 20:40:42 -0500

On Thu, Feb 15, 2024 at 8:41 AM Greg Wooledge <greg@wooledge.org> wrote:

> On Thu, Feb 15, 2024 at 07:56:12AM -0500, Zachary Santer wrote:
> > Alright, so the problem's been solved, but I'm curious how help-bash
> feels
> > about how I would do this.
> >
> > $ cat ./stdin-doubly
>
> What's the purpose of this script?
>
> > $ cat nonsense.txt
> > banana
> > apple
> >
> > $ ./stdin-doubly nonsense.txt
> > banana
> > apple
> > 1
> > 4
>
> I don't quite get what you're doing.  You appear to be running the
> equivalent of cat "$@" followed by echo 1; echo 4.  If that's actually
> what you want, then I'd simplify it down to:
>
> #!/bin/sh
> cat "$@"
> echo 1
> echo 4
>
> If you've got some more subtle purpose in mind, then we need to understand
> what it is.  In particular, what your script does *not* do is interleave
> the lines of the input file with the numbers, or read a line at a time
> from each of two input sources, or reuse the input for each iteration
> of the numbers, do anything else that the word "doubly" might imply.
>

Your email got into my spam folder. I didn't give a ton of thought to what
to call the thing. The while loop can read the stdin that was fed to the
script and then also its own stdin from the printf command. Two stdins, I
guess.

Otherwise, I'm replicating Mohammad's example code, formatted a little
nicer.

On Wed, Feb 14, 2024 at 4:25 PM Mohammad Akhlaghi <mohammad@akhlaghi.org>
wrote:

> $ printf "1 2 3\n4 5 6\n" \
>           | while read a b c; do cat /dev/stdin; echo $a; done
>

No clue if his real script would have something within the while loop that
would read everything 'til EOF or what, but I thought 'cat' was just for
the purpose of illustration.

Then the beginning bit setting up the script_input FD was in response to
this:

On Wed, Feb 14, 2024 at 8:48 PM Mohammad Akhlaghi <mohammad@akhlaghi.org>
wrote:

> until
> now it would read standard input, even if a file was given as an input
> argument! From now on, it will only check standard input when no input
> file is given on the command-line.
>

If you don't give 'sed' a file argument, for instance, it's going to read
from stdin whether that's redirected or piped from something or still the
terminal. So my script does the same.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]