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: Thu, 15 Feb 2024 07:56:12 -0500

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

> This problem should therefore not happen any more for that program.
>

Alright, so the problem's been solved, but I'm curious how help-bash feels
about how I would do this.

$ cat ./stdin-doubly
#!/bin/bash

shopt -s lastpipe

file_argument="${1}"

if [[ -n ${file_argument} ]]; then
  exec {script_input}< "${file_argument}"
else
  exec {script_input}<&0
fi

printf "1 2 3\n4 5 6\n" |
  while read a b c; do
    cat <&"${script_input}"
    printf '%s\n' "${a}"
  done
#

exec {script_input}<&-

$ printf '%s\n' "Words words words" | ./stdin-doubly
Words words words
1
4

$ cat nonsense.txt
banana
apple

$ ./stdin-doubly nonsense.txt
banana
apple
1
4


reply via email to

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