1

I'm trying to use the quote program to make a little shell script that'll keep updating the stock prices every second. quote has to access the Internet and therefore has a small delay when it runs. I have to clear the screen each time, but I want to clear after quote has completed and before cat prints its output so there isn't a significant delay between the clear and cat:

while true
do
   quote spy gld aapl amzn goog nflx tsla f gm msft | ??? clear | cat
   sleep 1
done

I want the ??? command (which runs after quote has printed its output of course) to run clear then pass the output from quote straight to the input for cat. What command does that?

3 Answers 3

2

Grats on getting it to work, but it looks like you're re-inventing 'watch'.

Also, querying at one second intervals might be a bit excessive? I'd pick 20 seconds or more, especially when 'watch' can show you the differences between runs.

watch -n 20 -d quote spy gld aapl amzn goog nflx tsla f gm msft
1

I want to clear after quote has completed and before cat prints its output

So what you're looking for is not actually to run clear then cat (which would be easy: quote … | { clear; cat; }), because cat would still start printing before quote has completed. A pipe won't solve your problem since it runs the producer and the consumer in parallel. Instead, you need to store the output of quote, then display it. Store the output of a command substitution into a variable.

text=$(quote spy gld aapl amzn goog nflx tsla f gm msft)
clear
echo "$text"

This removes blank lines at the end of the output. If this is not acceptable, you can work around this feature by printing an extra non-empty line at the end, and stripping it off. The command echo also mangles a few short strings that start with -.

text=$(quote spy gld aapl amzn goog nflx tsla f gm msft; echo .)
clear
printf %s "${text%.}"
1
  • The other answers worked as well, but this relates the most to my original question. I didn't know that the pipe worked like that, so what I'm asking for isn't practical.
    – sudo
    Oct 31, 2014 at 5:39
0

This isn't exactly what I asked, but it gets the job done. I set a variable to the output of quote. I didn't think of that at first because I'm new to Bash...

clear
while true
do
   OUTPUT="$(quote spy gld aapl amzn goog nflx tsla f gm msft)"
   clear
   echo "${OUTPUT}"
   sleep 1
done
2
  • 1
    I don't think you need the | cat in the subshell, VAR=$(foo) already stores the output of foo into $VAR.
    – DopeGhoti
    Oct 30, 2014 at 23:58
  • Yes, I meant to get rid of the | cat when I figured this out but forgot to. Thanks for the catch. I edited it.
    – sudo
    Oct 31, 2014 at 0:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .