LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 04-02-2011, 06:47 AM   #1
Fitch
LQ Newbie
 
Registered: Feb 2011
Location: Edinburgh
Distribution: Ubuntu 18.04
Posts: 27

Rep: Reputation: 1
Can't get the hang of while do


This code is meant to retrieve ascii from a temperature sensor in the fridge, check that it is not the error code (85 degrees) and append it to a csv file.

If the reading is 85 degrees, then either my fridge is on fire or the sensor cocked up, in which case it is meant to try again.

It's the last bit that I can't get to work.

Code:
#!/bin/bash
COUNT=4
while [ $COUNT -gt 0 ]; do
  /usr/bin/digitemp_DS9097 -a -q -o"%d/%b/%g %H:%M%t%.2C" -c /etc/digitemp.conf > /home/brafferton/Downloads/check.txt
  CHECK=$(tail -c 6 /home/brafferton/Downloads/check.txt)
     if [ "${CHECK}" = 85.00 ];then
#        echo bad result
        COUNT=$((COUNT - 1))
     else
        COUNT=0 
        cat /home/brafferton/Downloads/check.txt >> /home/brafferton/Downloads/$(date +%G-%b).csv
#        echo good result
     fi
done
Check.txt is empty if the reading is bad, and nothing is appended to the log.
This check is done every 4 hours, day and night. But as you can see from below, a few results are missing.

the results (so far) from 2011-Apr.csv

01/Apr/11 00:30 4.19
01/Apr/11 04:30 3.69
01/Apr/11 20:31 3.88
02/Apr/11 00:30 5.12
02/Apr/11 04:30 4.06
02/Apr/11 08:31 3.62

Can somebody tell me what I'm doing wrong?

I use Ubuntu 10.04 (Lucid), if that's any help...

Last edited by Fitch; 04-02-2011 at 06:58 AM.
 
Old 04-02-2011, 07:19 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
If "Check.txt is empty if the reading is bad" then the "bad" test will never be passed ...

If /home/brafferton/Downloads/check.txt is empty when the reading is bad then CHECK=$(tail -c 6 /home/brafferton/Downloads/check.txt) sets CHECK to an empty string and if [ "${CHECK}" = 85.00 ] fails, COUNT is set to zero, an empty string is appended to /home/brafferton/Downloads/$(date +%G-%b).csv and the while loop ends.

Would it be more robust to check that /home/brafferton/Downloads/check.txt contains a valid value rather than sentinel value of 85.00? Or to test for it being empty as you say it is when the reading is bad?
 
1 members found this post helpful.
Old 04-02-2011, 07:46 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
I am not good in re-playing code in my mind, so I let the code run and look at the output.

I would run the code with sh -x /path/to/your/script 2>&1 >> logfile

Next, I don't quite trust
Code:
[ "${CHECK}" = 85.00 ]
. Can you change that to:
Code:
[ "${CHECK}" == "85.00" ]
although the '==' is not really required, '= ' should work as well.

jlinkels
 
1 members found this post helpful.
Old 04-02-2011, 08:15 AM   #4
Fitch
LQ Newbie
 
Registered: Feb 2011
Location: Edinburgh
Distribution: Ubuntu 18.04
Posts: 27

Original Poster
Rep: Reputation: 1
I am eager to put in a check for a blank check.txt, but my knowledge of the code doesn't quite stretch that far. (this is my very first attempt at scripting)

I am now getting endless "good results", which is great, but doesn't really tell me if I've fixed it...

I bolded the line that is worrying below:

brafferton@cameras:~$ sh -x /usr/bin/fridgeexp 2>&1 >> /home/brafferton/Downloads/log.txt
+ COUNT=4
+ [ 4 -gt 0 ]
+ /usr/bin/digitemp_DS9097 -a -q -o%d/%b/%g %H:%M%t%.2C -c /etc/digitemp.conf
+ tail -c 6 /home/brafferton/Downloads/check.txt
+ CHECK= 3.81
+ [ 3.81 == 85.00 ]
[: 1: 3.81: unexpected operator
+ COUNT=0
+ date +%G-%b
+ cat /home/brafferton/Downloads/check.txt
+ echo good result
+ [ 0 -gt 0 ]
 
Old 04-02-2011, 11:51 AM   #5
Fitch
LQ Newbie
 
Registered: Feb 2011
Location: Edinburgh
Distribution: Ubuntu 18.04
Posts: 27

Original Poster
Rep: Reputation: 1
Using a single equals sign stopped the unexpected operator. And I've read up on the -o and -a functions.

I am hoping this works.. It will take a day or two to find out.
Meanwhile, is there anything glaringly obviously wrong?

Code:
#!/bin/bash
COUNT=4
while [ $COUNT -gt 0 ]; do
  /usr/bin/digitemp_DS9097 -a -q -o"%d/%b/%g %H:%M%t%.2C" -c /etc/digitemp.conf > /home/brafferton/Downloads/check.txt
  CHECK=$(tail -c 6 /home/brafferton/Downloads/check.txt)
     if [ "${CHECK}" = "85.00" -o "${CHECK}" = "" ]
	then     
	COUNT=$((COUNT - 1))
#	echo bad result
else
        COUNT=0 
        cat /home/brafferton/Downloads/check.txt >> /home/brafferton/Downloads/$(date +%G-%b).csv
#        echo good result
	fi     
done
The output is as follows, which seems to do the right thing:

brafferton@cameras:~$ sh -x /usr/bin/fridge 2>&1 >> /home/brafferton/Downloads/log.txt
+ COUNT=4
+ [ 4 -gt 0 ]
+ /usr/bin/digitemp_DS9097 -a -q -o%d/%b/%g %H:%M%t%.2C -c /etc/digitemp.conf
+ tail -c 6 /home/brafferton/Downloads/check.txt
+ CHECK= 3.25
+ [ 3.25 = 85.00 -o 3.25 = ]
+ COUNT=0
+ date +%G-%b
+ cat /home/brafferton/Downloads/check.txt
+ [ 0 -gt 0 ]

Last edited by Fitch; 04-02-2011 at 01:48 PM.
 
Old 04-02-2011, 02:42 PM   #6
Fitch
LQ Newbie
 
Registered: Feb 2011
Location: Edinburgh
Distribution: Ubuntu 18.04
Posts: 27

Original Poster
Rep: Reputation: 1
Didn't have to wait too long...

+ COUNT=4
+ [ 4 -gt 0 ]
+ /usr/bin/digitemp_DS9097 -a -q -o%d/%b/%g %H:%M%t%.2C -c /etc/digitemp.conf
+ tail -c 6 /home/brafferton/Downloads/check.txt
+ CHECK=
+ [ = 85.00 -o = ]
+ COUNT=3
+ [ 3 -gt 0 ]
+ /usr/bin/digitemp_DS9097 -a -q -o%d/%b/%g %H:%M%t%.2C -c /etc/digitemp.conf
+ tail -c 6 /home/brafferton/Downloads/check.txt
+ CHECK= 3.38
+ [ 3.38 = 85.00 -o 3.38 = ]
+ COUNT=0
+ date +%G-%b
+ cat /home/brafferton/Downloads/check.txt
+ [ 0 -gt 0 ]

So the null bit worked.
Thing is....
If I type it on the command line, I get perfect results every time.
It's when cron does it that the errors come.

My crontab:

MAILTO=""
# m h dom mon dow command
# Check fridge door every 3 minutes during the day
*/3 8-22 * * * fridgedoor >/dev/null 2>&1
# Get temperature readings from fridge and save to monthly file in Downloads
# 30 */4 * * * fridge >/dev/null 2>&1
30 */4 * * * sh -x /usr/bin/fridge -l 2>&1 | tee /home/brafferton/Downloads/log.txt
# Send report via email on the last day of the month
0 1 1 * * /usr/bin/sendfridge
 
Old 04-02-2011, 05:00 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
What goes wrong? What is the output of the job when it is run by cron?

jlinkels
 
Old 04-03-2011, 05:47 AM   #8
Fitch
LQ Newbie
 
Registered: Feb 2011
Location: Edinburgh
Distribution: Ubuntu 18.04
Posts: 27

Original Poster
Rep: Reputation: 1
Solved

The logs don't show any errors for cron.
The log of sh -x /usr/bin/fridge -l 2>&1 | tee -a /home/brafferton/Downloads/log.txt
does show a crc error on the reading from digitemp - which, funnily enough, gives a null output.

My theory (whistling in the dark now) is that cron calls digitemp faster than I can do it on the keyboard, and digitemp sometimes can't handle it.

Now however, the new checks allow digitemp to re-read, and hey presto! it gets it right second time around. Good enough!

Thanks Jlinklels and catkin for your help. Much appreciated.

Last edited by Fitch; 04-03-2011 at 05:48 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hang Up Gaetano Pace Linux - Newbie 1 07-30-2009 07:31 PM
Before you hang me... Titan2k General 4 01-04-2005 09:43 AM
Sometimes hang-up theonebeyond Slackware 2 09-23-2004 01:46 PM
GeForce, Hang on boot, hang on X ctrl-alt-bspace bigearsbilly Linux - Hardware 0 03-01-2004 07:56 AM
Hang Up sbb Linux - Newbie 2 08-24-2003 10:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration