-1

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 "/  ")
3
  • 2
    Run your code through spellcheck.net. You can't put a space after the = in an assignment
    – chepner
    May 15, 2015 at 12:12
  • 1
    You'll have a much better chance of getting answers if your question is readable. It's worth spending the time to write in standard English (correct spelling, punctuation, etc.). The Stack Overflow Markdown also provides a number of formatting options to make posts even easier to read. I've edited your question for you so you can see how it can be improved. May 15, 2015 at 12:13
  • 1
    The thing with scripting (especially when new) is that you need to test commands on command line before scripting them, so in this case entering x= 1000 then echo $x might might have solved your issue. but live (do) and learn :)
    – hoss
    May 15, 2015 at 15:50

1 Answer 1

1

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.

1
  • You're welcome, but the real way to thank someone on stackexchange is to accept their answer. Click on the tick-mark to the left.
    – rojomoke
    May 20, 2015 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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