This is related to Shell script too slow for output to Conky

This code is nearly perfect:

stdbuf -oL jack_cpu_load \
    | grep --line-buffered "jack DSP load" \
    | stdbuf -oL cut -d' ' -f4 \
    | while read line; do
        echo "scale=0; $line*100/1" | bc -l > /tmp/buffer
    done &

The only issue is that bc removes the decimal point and zero when the output is <1.

I'd like to see two places after the decimal point, but no fractions at all, with a zero for output <1 would be fine.

  • 1
    Can you give us just the output of stdbuf... without the bc? – slm Jan 27 '14 at 17:33
  • The "divide by 1" operation looks useless. – jlliagre Jan 27 '14 at 17:49
  • Ouch! Why not just stdbuf -oL jack_cpu_load | awk '/jack DSP load/{print $4*100}'? – Stéphane Chazelas Jan 27 '14 at 21:40

You just need to make it

stdbuf -oL jack_cpu_load |
grep --line-buffered "jack DSP load" |
stdbuf -oL cut -d' ' -f4 |
while read line; do echo "$line" > /tmp/buffer; done &

to output the value that you input without modification.

  • Yeah, that's it. I just add my little sed bit to it and it's what I want. Thanks again. – user57649 Jan 27 '14 at 18:08
  • What does sed do again in your case? Here we already have cut in place, which extracts the fourth part (the float) of the input. – Marki Jan 27 '14 at 18:54
  • I'm using sed to trim off the last four digits. Just to make it look better. – user57649 Jan 27 '14 at 19:49

I think if you just add the scale= so that it has a value higher than 0 you'll get decimal numbers displayed.


$  echo "scale=0; 100*100/1" | bc -l

$  echo "scale=2; 100*100/1" | bc -l

Getting 44.93 and not 0.4493, why?

The use of $line*100/1 with the 100/1 seems unnecessary. Take that bit out. Multiplying the $line by 100 is skewing your results by 2 decimal places.

  • I tried that, but the decimal point is in the wrong place, i.e. what should be 0.4493 shows as 44.93 and what I want is 0.44 – user57649 Jan 27 '14 at 17:43
  • @user57649 - that's b/c you're multiplying it by 100/1. – slm Jan 27 '14 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.