Search code examples
linuxbashscriptingdivisionoperand

Bash Script division


I'm a beginner in scripting. I have a temperature sensor which gives me temperature if I cat the file /sys/bus/w1/devices/28-000006c5772c/w1_slave. The output of the file looks like:

83 01 4b 46 7f ff 0d 10 66 t=24187

As you can see the temperature is t=24187 which I have to divide by 1000. My script looks like this:

#!/bin/bash
date +%Y-%m-%d-%H-%M
s= cat /sys/bus/w1/devices/28-000006c5772c/w1_slave |  grep t= | cut -d "=" -f2
x= 1000
f= echo  $(( $s / $x )) | bc -l
echo  the actually  temperature  is $f

But it dosen´t work. When I start the script, I get this output here:

2015-05-04-08-51 (date is wrong NTP not configured^^) 
23687
/home/pi/RAB.sh: line 5: 1000: command not found
/home/pi/RAB.sh: line 6: /  : syntax error: operand expected (error token is "/  ")

Solution

  • To assign the output of a command to a variable, you need to use the backticks or (preferably) the $() syntax.

    s=$(cat /sys/bus/w1/devices/28-000006c5772c/w1_slave |  grep t= | cut -d "=" -f2)
    

    will set $s to 24187

    That, and removing the spaces after the = signs as suggested by chepner will get you what you want.