0

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.

user57649
  • 69
  • 1
  • 7

2 Answers2

2

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

Example

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

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

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.

slm
  • 363,520
  • 117
  • 767
  • 871
1

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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Marki
  • 824
  • 6
  • 15
  • 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