Why do those loops freezing or ending? (long)
Why do those loops freezing or ending? (long)
am 08.10.2007 16:17:25 von unknown
Hello,
First I would like to apologise for my very poor english...
I'm new in shell script and I have a problem with 2 scripts. I want to
replace a serial printer which print log from an external system by a
computer, Logs are two lines long and end with a ^M character on each
line.
first script ( launched at boot time )
#! /bin/sh
echo "emulation imprimante alarme"
stty raw 2400 oddp istrip < /dev/ttyS0 cat /dev/ttyS0 >
/home/christophe/alarme/imp.txt & /usr/bin/triep.sh &
Second one (triep.sh, called by the first)
#! /bin/sh
while true ; do
NBLIGNE=$(cat /home/christophe/alarme/imp.txt | wc -l)
if [ "$NBLIGNE" != 0 ] ; then
sleep 3
NBLIGNE=$(cat /home/christophe/alarme/imp.txt | wc -l)
head -n $NBLIGNE /home/christophe/alarme/imp.txt >
/home/christophe/alarme/imp.ok
RESTE=0
while [ "$RESTE" != "$NBLIGNE" ] ; do
let "RESTE += 1"
sed -i '1'd /home/christophe/alarme/imp.txt
done
awk '{gsub(/\015/, "", $0); printf("%s%s", $0, /:/?FS:RS )}'< /home/christophe/alarme/imp.ok >> /home/christophe/alarme/$(date +%Y_%m)
rm /home/christophe/alarme/imp.ok
fi
sleep 10
done
Hum, I got help for awk ( remove ^M and get one line per log of two lines,
one of the line contains ":")
The problem is that this script die somewhere! while i get the first log
in the right format and file (even if there are lot of lines), the new one
never appears, neither in the imp.txt or 2007_10 (today...) files
I suspect the
while ;do
if
while ;do
done
fi
done
structure
but how to get the expected result?
So, i need your help !
Thanks,
Christophe
Re: Why do those loops freezing or ending? (long)
am 08.10.2007 16:57:26 von Malcolm
On 08 Oct 2007 14:17:25 GMT
Christophe Maquaire
wrote:
> Hello,
>
> First I would like to apologise for my very poor english...
>
> I'm new in shell script and I have a problem with 2 scripts. I want to
> replace a serial printer which print log from an external system by a
> computer, Logs are two lines long and end with a ^M character on each
> line.
>
> first script ( launched at boot time )
>
> #! /bin/sh
> echo "emulation imprimante alarme"
> stty raw 2400 oddp istrip < /dev/ttyS0 cat /dev/ttyS0 >
> /home/christophe/alarme/imp.txt & /usr/bin/triep.sh &
>
> Second one (triep.sh, called by the first)
Hi
This isn't a fix for your script, but have you looked at SEC - simple
event correlator to action your log?
http://kodu.neti.ee/~risto/sec/
--
Cheers Malcolm °¿° (Linux Counter #276890)
SLED 10.0 SP1 x86_64 Kernel 2.6.16.53-0.8-smp
up 6 days 0:14, 1 user, load average: 0.02, 0.07, 0.08
Re: Why do those loops freezing or ending? (long)
am 08.10.2007 17:53:58 von unknown
Le Mon, 08 Oct 2007 09:57:26 -0500, Malcolm a écrit:
> Hi
> This isn't a fix for your script, but have you looked at SEC - simple
> event correlator to action your log?
>
> http://kodu.neti.ee/~risto/sec/
>
No, i didn't.
So I had a look, but isn't it too much?
I only want to get something like:
External source ------> serial port -------> file (with some ordering in
data, one file per month and one ligne per log)
And the sec.pl manpage is not so clear for me to get this.
Re: Why do those loops freezing or ending? (long)
am 09.10.2007 19:19:56 von Spiros Bousbouras
On Oct 8, 3:17 pm, Christophe Maquaire
w...@nerim.net.invalid> wrote:
>
> I'm new in shell script and I have a problem with 2 scripts. I want to
> replace a serial printer which print log from an external system by a
> computer, Logs are two lines long and end with a ^M character on each
> line.
I'm not clear on what you're trying to do but I have some suggestions
and even more questions.
> first script ( launched at boot time )
>
> #! /bin/sh
> echo "emulation imprimante alarme"
> stty raw 2400 oddp istrip < /dev/ttyS0 cat /dev/ttyS0 >
> /home/christophe/alarme/imp.txt & /usr/bin/triep.sh &
In the last line of code do you mean && just before /usr/bin/
triep.sh ?
>
> Second one (triep.sh, called by the first)
>
> #! /bin/sh
> while true ; do
> NBLIGNE=$(cat /home/christophe/alarme/imp.txt | wc -l)
NBLIGNE=$( wc -l < /home/christophe/alarme/imp.txt )
is simpler.
> if [ "$NBLIGNE" != 0 ] ; then
You want numbers comparison right ? If yes better to do
if [ $NBLIGNE" -ne 0 ] ; then
> sleep 3
Why are you sleeping here ?
> NBLIGNE=$(cat /home/christophe/alarme/imp.txt | wc -l)
Again NBLIGNE=$( wc -l < /home/christophe/alarme/imp.txt )
But doesn't NBLIGNE already have the number of lines in the file
/home/christophe/alarme/imp.txt ?
> head -n $NBLIGNE /home/christophe/alarme/imp.txt >
> /home/christophe/alarme/imp.ok
Isn't this the same as
cat /home/christophe/alarme/imp.txt > /home/christophe/alarme/imp.ok
> RESTE=0
> while [ "$RESTE" != "$NBLIGNE" ] ; do
> let "RESTE += 1"
> sed -i '1'd /home/christophe/alarme/imp.txt
> done
> awk '{gsub(/\015/, "", $0); printf("%s%s", $0, /:/?FS:RS )}'< /home/christophe/alarme/imp.ok >> /home/christophe/alarme/$(date +%Y_%m)
> rm /home/christophe/alarme/imp.ok
> fi
> sleep 10
> done
>
> Hum, I got help for awk ( remove ^M and get one line per log of two lines,
> one of the line contains ":")
>
> The problem is that this script die somewhere! while i get the first log
> in the right format and file (even if there are lot of lines), the new one
> never appears, neither in the imp.txt or 2007_10 (today...) files
>
> I suspect the
>
> while ;do
> if
> while ;do
> done
> fi
> done
>
> structure
> but how to get the expected result?
>
> So, i need your help !
If you could explain the logic of your script I might be able to
offer more help.
Re: Why do those loops freezing or ending? (long)
am 10.10.2007 11:39:36 von unknown
Le Tue, 09 Oct 2007 17:19:56 +0000, Spiros Bousbouras a écrit:
>
> I'm not clear on what you're trying to do but I have some suggestions
> and even more questions.
Copy and wrap error
In fact the lines are:
stty raw 2400 oddp istrip < /dev/ttyS0
cat /dev/ttyS0 > /home/christophe/alarme/imp.txt &
/usr/bin/triep.sh &
> NBLIGNE=$( wc -l < /home/christophe/alarme/imp.txt )
> is simpler.
OK
>
>> if [ "$NBLIGNE" != 0 ] ; then
>
> You want numbers comparison right ? If yes better to do
> if [ $NBLIGNE" -ne 0 ] ; then
OK
>> sleep 3
>
> Why are you sleeping here ?
I don't know if the last line of the file is completed or not.
The message waited are 2 lines long and there are large gap
between two messages.
So I wait here to get the whole message
>> NBLIGNE=$(cat /home/christophe/alarme/imp.txt | wc -l)
>
> Again NBLIGNE=$( wc -l < /home/christophe/alarme/imp.txt ) But doesn't
> NBLIGNE already have the number of lines in the file
> /home/christophe/alarme/imp.txt ?
Yes, but because i was sleeping to be "sure" to get the whole message,
perhaps is there 1 more ligne.
>
>> head -n $NBLIGNE /home/christophe/alarme/imp.txt >
>> /home/christophe/alarme/imp.ok
>
> Isn't this the same as
> cat /home/christophe/alarme/imp.txt > /home/christophe/alarme/imp.ok
>
Yes, but i would like to "treat" whole message after and I don't know if
there is something "new" (and not complete) at the end of the imp.txt
whitch get all data from /dev/ttyS0
I agree that it isn't satisfying but i haven't other idea to get whole
messages whitout loosing data..
The initial purpose is to replace an old serial printer witch get data
from an external device and to log messages in files (one per month)
And more, each logged message is two lines long and contains a ^M
character . The awk line issue a clean one line log and works.
The system is a Debian etch i386 old computer witch provides some services
on a small intranet (at home) and i cannot test from work...
Today, i'm with 3 scripts:
#! /bin/sh
# emulprinter.sh
echo "emulation imprimante alarme"
stty raw 2400 oddp istrip < /dev/ttyS0
cat /dev/ttyS0 > /home/christophe/alarme/imp.txt &
/usr/bin/triep.sh &
#! /bin/sh
# /usr/bin/triep.sh
while true ; do
NBLIGNE=$( wc -l < /home/christophe/alarme/imp.txt )
if [ "$NBLIGNE" -ne 0] ; then
sleep 3
/usr/bin/rangep.sh &
wait
fi
sleep 5
done
#! /bin/sh
# /usr/bin/rangep.sh
NBLIGNE=$( wc -l < /home/christophe/alarme/imp.txt )
head -n $NBLIGNE /home/christophe/alarme/imp.txt > \
/home/christophe/alarme/imp.ok
RESTE=0
while [ "$RESTE" != "$NBLIGNE" ] ; do
let "RESTE += 1"
sed -i '1'd /home/christophe/alarme/imp.txt
done
awk '{gsub(/\015/, "", $0); printf("%s%s", $0, /:/?FS:RS )}'< \
/home/christophe/alarme/imp.ok >> /home/christophe/alarme/$(date +%Y_%m)
rm /home/christophe/alarme/imp.ok
exit 0
Thanks for further help.
Christophe
Re: Why do those loops freezing or ending? (long)
am 10.10.2007 20:02:28 von Spiros Bousbouras
On Oct 10, 10:39 am, Christophe Maquaire
w...@nerim.net.invalid> wrote:
>
> The initial purpose is to replace an old serial printer witch get data
> from an external device and to log messages in files (one per month)
>
> And more, each logged message is two lines long and contains a ^M
> character . The awk line issue a clean one line log and works.
>
> The system is a Debian etch i386 old computer witch provides some services
> on a small intranet (at home) and i cannot test from work...
>
> Today, i'm with 3 scripts:
>
> #! /bin/sh
> # emulprinter.sh
> echo "emulation imprimante alarme"
> stty raw 2400 oddp istrip < /dev/ttyS0
> cat /dev/ttyS0 > /home/christophe/alarme/imp.txt &
Ok , I'm beginning to get the picture. Reading from /dev/ttyS0 will
produce at irregular intervals 2 lines of output which will contain
(among other things) ^M. This will continue for as long as the
computer
is on. So for each month you want all the output read from /dev/ttyS0
during that month to be placed in a file, after ^M has been stripped
from
each line. Have I got it right ?
Here's my suggestion: I will *assume* that the line
tty raw 2400 oddp istrip < /dev/ttyS0
does what you want because I'm not sure why it's there.
>From there on you can do
while true ; do
read line1
read line2
line1=$( echo "$line1" | tr -d '\015' )
line2=$( echo "$line2" | tr -d '\015' )
file=$( date +%Y_%m )
echo "$line1" >> /home/christophe/alarme/"$file"
echo "$line2" >> /home/christophe/alarme/"$file"
done < /dev/ttyS0
I have no way to test it but it looks promising.
Re: Why do those loops freezing or ending? (long)
am 11.10.2007 11:41:08 von unknown
Le Wed, 10 Oct 2007 11:02:28 -0700, Spiros Bousbouras a écrit:
>
> Ok , I'm beginning to get the picture. Reading from /dev/ttyS0 will
> produce at irregular intervals 2 lines of output which will contain
> (among other things) ^M. This will continue for as long as the
> computer
> is on. So for each month you want all the output read from /dev/ttyS0
> during that month to be placed in a file, after ^M has been stripped
> from
> each line. Have I got it right ?
Yes, perfectly!
> Here's my suggestion: I will *assume* that the line
> stty raw 2400 oddp istrip < /dev/ttyS0
> does what you want because I'm not sure why it's there.
Yes, it was a long run for me to get the good characters from /dev/ttyS0...
>>From there on you can do
>
> while true ; do
> read line1
> read line2
> line1=$( echo "$line1" | tr -d '\015' )
> line2=$( echo "$line2" | tr -d '\015' )
> file=$( date +%Y_%m )
> echo "$line1" >> /home/christophe/alarme/"$file"
> echo "$line2" >> /home/christophe/alarme/"$file"
> done < /dev/ttyS0
>
> I have no way to test it but it looks promising.
Thanks a lot!
I'll try this tomorrow.
I didn't whell understood the man page of the read command (will i get a
FULL line?, how to set control characters (end of line) or does it
"magically" works fine here ?)
Re: Why do those loops freezing or ending? (long)
am 11.10.2007 16:11:12 von Spiros Bousbouras
On 11 Oct, 10:41, Christophe Maquaire
w...@nerim.net.invalid> wrote:
> I didn't whell understood the man page of the read command (will i get a
> FULL line?
Yes.
> how to set control characters (end of line) or does it
> "magically" works fine here ?)
I'm not sure what you mean "set control characters". When you do
read a
what goes inside a is the whole line read without the terminating
newline character.
Re: Why do those loops freezing or ending? (long)
am 12.10.2007 11:46:01 von Christophe Maquaire
Le Thu, 11 Oct 2007 07:11:12 -0700, Spiros Bousbouras a écrit:
> I'm not sure what you mean "set control characters". When you do
> read a
> what goes inside a is the whole line read without the terminating
> newline character.
After man read , man stty and other things, i was A LOT confusing...
Your script is as efficient as simple, it works perfectly!
Thanks for your GREAT help!
Christophe
PS:
I tried read from a regular file, but i get only the same first line,
whereas i get line after line from pseudo file /dev/ttyS0.
I'll not forget the usefull tr command.
Re: Why do those loops freezing or ending? (long)
am 13.10.2007 16:46:23 von Spiros Bousbouras
On Oct 12, 10:46 am, Christophe Maquaire
wrote:
> After man read , man stty and other things, i was A LOT confusing...
read is a shell builtin so for an explanation you probably
need to look in the man page of whichever shell you're
using. (Although on Solaris "man read" will give a page
on the read builtins of various shells)
> I tried read from a regular file, but i get only the same first line...
Then you're doing something wrong.
while read line ; do
echo "$line"
done < file
should print the whole file on the screen for a regular file.
Same with
cat file | while read line
do
echo "$line"
done