For example, suppose I define a variable:
DROP=
And I have a loop:
# highly compatible, streaming,
# line-wise parser for embedded device
# no awk or external dependency
while IFS= read -r line; do
...
generate_output_1 | xargs echo
...
generate_output_2 | xargs echo
...
generate_output_N | xargs echo
done <<<$(cleanFile x.txt) > "$OUTPUT_STREAM"
How would I toggle the ability to forward the output? I can think of defining a function, but is there a command that allows the output to be turned off or on?
# e.g.
DROP=-d
generate_output | spigot ${DROP}
To address the comments, before asking this question, I used if statements. For those of you familiar with a streaming parser, avoiding if statements where possible is fundamentally important, since you tend to require if-statements in the body of the while loop already. So I'll go to some length to avoid any of the if statement solutions, including any control expressions (e.g. [ .. ] && ... || ...
).
Secondly, I tried defining a PIPE
variable:
if [ -z "$DROP" ]; then
PIPE=/dev/stdout
else
PIPE=/dev/null
fi
And appended it in place of ... | xargs echo
. This should have worked, intuitively. In practical terms, it did not work inside my while loop in my version of BASH and virtualization environment. That is because I've already used that technique in managing the output of my while loop. The final line of the example is making use of the technique already. In the OUTPUT_STREAM
, I swap around files based on arguments or default to /dev/stdout
:
done <<<$(cleanFile x.txt) > "$OUTPUT_STREAM"
What I am really looking for is a concise command that does what spigot
does. And that is all.
[ "x$DROP" = x ] && (generate_output_1 | xargs echo)
: do a command only if $DROP is empty – Giacomo Catenazzi Aug 29 '23 at 14:52$DROP
. May be tell us: The script should be equivalent withwhile ...WHAT1 ... done
when DROP is empty and withwhile...WHAT2.. done
. Why is[ "x$DROP" = x ] && ...
not useful?. – minorChaos Aug 29 '23 at 14:58if [ "$DROP" = "--quiet" ]; then exec 1>/dev/null; fi
– minorChaos Aug 29 '23 at 15:00[ -n "$DROP" ] && OUTPUT_STREAM=/dev/null
before the while loop. Also, don't be so worried about awk or other external programs. Running external programs is shell's purpose. Most embedded systems have busybox installed, which includes a decent implementation of awk. And any version of awk is going to be much faster than a shell while read loop. BTW, see Why is using a shell loop to process text considered bad practice? and Understanding "IFS= read -r line" – cas Aug 30 '23 at 01:09spigot
there would need to do. But there's also Stéphane Chazelas' answer using aliases to avoid a pass-through command. – ilkkachu Aug 30 '23 at 15:05PIPE
-variable? I can't tell from the post, but I wondered what would have been wrong with it. – ilkkachu Aug 30 '23 at 15:07xargs echo
is not the right tool (chokes on quotes, backslashes, mangles spacing, may choke on lines starting with-
, could output more than one line). Trypaste -sd ' ' -
instead. – Stéphane Chazelas Aug 30 '23 at 15:47