LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2009, 06:13 PM   #1
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 179

Rep: Reputation: 17
find string in file - test the value - from my com port gizmo


Friends,

i have a com port gizmo [ vk011 ]
This gizmo gives the temp of the room.

I can connect to it via mini com
and it spits out line after line of

Sensor 1 +70.13 DegF Hi +77.56 DegF Low +29.75 DegF
Sensor 1 +70.13 DegF Hi +77.56 DegF Low +29.75 DegF
Sensor 1 +70.13 DegF Hi +77.56 DegF Low +29.75 DegF

My Goal is to have an ALARM if the room gets above a num ( 80 )

There may be a better way but so far I have this
i can write 1 line to a text file

like this

head -1 /dev/ttyS0 > 01.txt
It runs and then stops - i get
Sensor 1 +70.13 DegF Hi +77.56 DegF Low +29.75 DegF

so now i wonder if I can
1-- read the line from the file 01.txt
2-- look at the characters 11 & 12 - basically see the 70.
3-- do an 'if that num (70) is greater than some value I declare
like say 80' do something

I already know ( i think ) how to send an email in a bash.
that would be the something I would like to do.

now it might be possible to do that without writting to a file-- but this is what i have so far.
 
Old 09-10-2009, 06:34 PM   #2
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
There would be many ways to do this, but the first script that came to mind was:

Code:
#!/bin/sh
# 
# Script to trigger an alarm past a certain temperature

# Alert temp
ALARM="80"

# Get just the temperature value
TEMP=`head -1 /dev/ttyS0 | awk '{print $3}' | cut -c2-3`

# Compare to trigger temperature
if [ "$TEMP" -ge "$ALARM" ]; then
     # Put command to run here
     echo "Temperature alarm!"
fi
That was off the top of my head, so there might be some syntax issues there. But I am pretty sure that will do what you want.
 
Old 09-11-2009, 07:25 AM   #3
zimbot
Member
 
Registered: Nov 2005
Location: cincinnati , ohio . USA
Distribution: ubuntu , Opensuse , CentOS
Posts: 179

Original Poster
Rep: Reputation: 17
Thumbs up YES! mucho thanks

Yes that works
putting the alarm val in a var - i knew before
but the means to get the string - to parse the output - OUTSTANDING!

TEMP=`head -1 /dev/ttyS0 | awk '{print $3}' | cut -c2-3`

awk - print $3 gets the 3rd colum and piped into cut getting #2 & #3 pos
so that TEMP is that num

then the if the -ge for greater ( i never knew ... )

# Compare to trigger temperature
if [ "$TEMP" -ge "$ALARM" ]; then


wow thnaks

also -- i caused me to read about awk.
amazing written ( in part ) by the fellow who wrote C [ mr K of awk ]

again , thanks!

I trullllly love this way of learning.

I will be able to USE this!
 
  


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
Perl find file and then replace string in file moos3 Programming 5 07-29-2009 07:10 AM
Sending test data from a file to port and IP tnine9 Linux - Networking 5 04-27-2009 04:06 PM
how to find out particular string in a file mksc Linux - Newbie 1 06-23-2008 03:04 AM
Shell script: how to find a file containing a string guarriman General 2 08-27-2007 03:39 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:21 AM.

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