Comparing a number in a text file with a specific value


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Comparing a number in a text file with a specific value
# 1  
Old 01-27-2011
Comparing a number in a text file with a specific value

My project is to get a temperature reading from a refridgerator every 2 minutes and check to see if the door has been left open.

I don't yet have the mastery of Linux, being a complete noob, but I reckon I need a text file with the latest temperature reading in it. This I've managed to do by getting cron to call up a script called fridgedoor every 2 minutes and outputting the result from Digitemp (serial temperature reader) to fridgedoor.txt.

So every 2 minutes fridgedoor.txt is overwritten with the current temperature. The file just has a number in it e.g "3.06"

So I've got line 1 of my script done and dusted.

My next job is to get it to play a .wav if the temperature is greater than 5.00

Aaaah! My knowledge of commands was pretty well used up on line 1.

I know I start with if and end with fi. It's just the teensy weensy bits in the middle I'm clueless about.

Anybody want to help a knackered old sop like me out with the middle bit?

I await in eager anticipation...

Happy New Year by the way...
# 2  
Old 01-27-2011
Code:
while true
do
    [  $(( $(<fridgedoor.txt)  > 5.00 ))  ]  && echo ok || echo 'not ok'
    sleep 120
done

Place a command after the || part (ie remove the second echo statement) that plays a tone on your box.
# 3  
Old 01-28-2011
Oooh! It's so close!

Thanks for the quick reply
Hit a bit of a snag though...

The script is:

Code:
#!/bin/bash
/usr/bin/digitemp_DS9097 -a -q -o"%.2C" -c /etc/digitemp.conf > /home/brafferton/Downloads/fridgedoor.txt

while true
do
  [ $(( $(</home/brafferton/Downloads/fridgedoor.txt) > 5.00 )) ] && echo ok || echo 'not ok'
done

I've left out the sleep 120 as cron does that.
Q: Is while do a never ending loop?

Anyway!
Typing in the command line:

brafferton@cameras:~/Downloads$ fridgedoor

gets:

/usr/bin/fridgedoor: line 6: 2.88 > 5.00 : syntax error: invalid arithmetic operator (error token is ".88 > 5.00 ")

---------- Post updated at 11:04 PM ---------- Previous update was at 08:45 PM ----------

I've been doing a lot of trawling and I think that either the CR at the end of the line, (and I presume there is one) is cocking up everything OR that bash won't do decimals, or both
If it's not the CR thingy then how would I strip the decimals off fridgedoor.txt i.e. if 6 > 5 then sound the alarm?

---------- Post updated 28-01-11 at 08:49 AM ---------- Previous update was 27-01-11 at 11:04 PM ----------

More trawling overnight and I've managed:

Code:
REAL=$(/usr/bin/digitemp_DS9097 -a -q -o"%.0C" -c /etc/digitemp.conf)
echo "${REAL} C"
if [ "${REAL}" -ge 5 ] ;then
echo door closed    
else
echo door open
fi

No Errors!
Seems to work...

Last edited by Yogesh Sawant; 02-01-2011 at 07:53 AM.. Reason: added code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

2. UNIX for Dummies Questions & Answers

Comparing to specific line in file bash script

Hi, I have a value stored in x and I need to compare it to the numbers in every other line of a file. The file contains alternating lines of numbers and letters: aaaa1111 AAAAAAAA bbbb2222 BBBBBBBB cccc3333 CCCCCCCC I need to compare x to the numbers in every other line without the... (2 Replies)
Discussion started by: ShiGua
2 Replies

3. UNIX for Dummies Questions & Answers

Deleting cells that contain a specific number only from a space delimited text file

I have this space delimited large text file with more than 1,000,000+ columns and about 100 rows. I want to delete all the cells that consist of just 2 (leave 2's that are not by themselves intact): File before modification aa bb cc 2 NA100 dd aa b1 c2 2 NA102 de File after modification... (1 Reply)
Discussion started by: evelibertine
1 Replies

4. UNIX Desktop Questions & Answers

COMPARING COLUMNS IN A TEXT FILE

Hi, Good day. I currently have this data called database.txt and I would like to check if there are no similar values (all unique) on an entire row considering the whole column data is unique. the data is as follows cL1 cL2 cL3 cL4 a12 c13 b13 c15 b11 a15 c19 b11 c15 c17 b13 f14 with... (1 Reply)
Discussion started by: whitecross
1 Replies

5. Shell Programming and Scripting

Comparing and Formatting the text file

hi, I need a script which can format the below text file which contains comments file1.txt -------- //START //Name: some value //Date: //Changes:............. //..................... //END //START //Date: //Name: some value //Changes:............. //..................... (3 Replies)
Discussion started by: flamingo_l
3 Replies

6. Linux

Get a specific line number from a text file

Hello! All, Could you please tell me how to get a specific line number from a text file? For example below, ABC DEF ---> Get this line number, return to an variable GHI My OS is Linux. Thank you so much for your help in advance! (3 Replies)
Discussion started by: barryxian
3 Replies

7. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

8. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

9. Shell Programming and Scripting

extract the lines between specific line number from a text file

Hi I want to extract certain text between two line numbers like 23234234324 and 54446655567567 How do I do this with a simple sed or awk command? Thank you. ---------- Post updated at 06:16 PM ---------- Previous update was at 05:55 PM ---------- found it: sed -n '#1,#2p'... (1 Reply)
Discussion started by: return_user
1 Replies

10. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies
Login or Register to Ask a Question