简体   繁体   中英

Display output of command in terminal while using command substitution

So I'm trying to check for the output of a command, but I also want to be able display the output directly in the terminal.

#!/bin/bash
while :
do
OUT=$(streamlink -o "$NAME" "$STREAM" best)
echo "$OUT"
if [[ $OUT == *"No playable streams"* ]]; then
  echo "Delaying!"
  sleep 15s
fi
done

This is what I tried to do.

The code checks if the output of a command contains that error substring, if so it'd add a delay. It works well on that part.

But it doesn't work well when the command is actually successfully downloading a file as it won't perform that echo until it is finished with the download (which would take hours). So until then I have no way of personally checking the output of the command

Plus the output of this particular command displays and updates the speed and filesize in real-time, something echo wouldn't be able to replicate.

So is there a way to be able to display the output of a command in real-time, while also command substituting them in order to check the output for substrings after the command is finished?

Use a temporary file:

TEMP=$(mktemp) || exit 1

while true
do
    streamlink -o "$NAME" "$STREAM" best |& tee "$TEMP"
    OUT=$( cat "$TEMP" )
    #echo "$OUT" # not longer needed
    if [[ $OUT == *"No playable streams"* ]]; then
        echo "Delaying!"
        sleep 15s
    fi
done

# not really needed here because of endless loop
rm -f "$TEMP"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM