shposixbusybox

Using busybox shell, i'm trying to divide the output of a cat command by 1000, as a float


I am trying to run the following command with two-point precision (because I want to convert it to Fahrenheit).

echo "$(cat /sys/class/thermal/thermal_zone*/type) $(($(cat /sys/class/thermal/thermal_zone*/temp)/1000))"

Extra points to help me add the code to convert it to Fahrenheit!

Again, this is Busybox, not bash, so available commands/syntax are somewhat limited. THANKS!!!

I promise I'm not being lazy on purpose. It's over 100 degrees Fahrenheit here today, I have a terrible dehydration headache, so my brain isn't working at total capacity. All help is apprecaited


Solution

  • busybox provides bc which is arbitrary precision. Its scale command sets how many digits after the decimal point to show on output.

    /sys/class/thermal/thermal_zone* can return more than one directory, so you should use a loop.

    C to F conversion is: F = 32 + 9C/5

    for z in /sys/class/thermal/thermal_zone*; do
        echo "$(cat $z/type) $(echo "scale=2;32+9*$(cat $z/temp)/5000"|bc -l)"
    done