I need some help. I want to obtain the number of mails with status
equal to O in my mail box. I'm using the next line to get it:
exec grep -c Status:.O $mailbox
the problem is that if the mailbox doesn't contain any message
with status= O, tcl reports:
% exec grep -c Status:.O /var/spool/mail/aguerra
0
child process exited abnormally
Child process exits abnormally only in this case, If there's at
least on mail with Status= O there's no problem. Why?
Thanks
Alejandro Guerra
Universidad Veracruzana
Maestria en Inteligencia Artificial
Sebastian Camacho No 5, Xalapa, Ver., Mexico, 91000
I have no idea why no matches is an error, but I ended up doing this:
catch {exec grep <string> <file>} message
if {$message == ""} {
# carry on
} else {
# give error message that no matches were found
}
Apologies that this is fairly hack code :-)
Cheers
Neil
You just need to know the behavior of the command you are exec'ing.
Tcl looks only at the return value from the exec'ed commmand to find
errors. So, if you read the man page for grep you would find:
EXIT STATUS
The following exit values are returned:
0 one or more matches were found
1 no matches were found
2 syntax errors or inaccessible files (even
if matches were found).
So, when no matches are found the return value from grep goes from 0
to 1 which likes like an abnormal exit value. I hope this helps
--Dan Kuchler
HP-Convex Division
> Alejandro Guerra was having problems with grep when it failed to
> match ...
> I have no idea why no matches is an error, but I ended up doing this:
> catch {exec grep <string> <file>} message
> if {$message == ""} {
> # carry on
> } else {
> # give error message that no matches were found
> }
Fairly straightforward reason, which can be found by looking at the
grep(1)
and exec(n) man pages... grep returns 0 if it finds a match, and 1 if it
doesn't.
When you exec a unix command, if it returns a non-zero value, it's a failure
that will get sent through the usual Tcl/Tk error-handling code. You do need
to do something like what Neil wrote to get around it.
--
Mitch Gorman (Modern, Noisy, and Effective)
em...@net-gate.com
http://www.net-gate.com/~emrys
mgo...@jcals.csc.com
I don't really see where the result of the grep command is being
stored. Is it being stored in the variable, message? I'm fairly new at
Tcl so please forgive my ignorance on this. I am having the same
problem with the grep command encountered by the originator of this
thread.
Arturo
That's because it wasn't, the code would read like this, where $value
holds the error value, and $message holds the result whether grep
returns the string you want, or an error message. Here's the code:
set value [catch {exec grep env /home/kuchler/.cshrc} message]
if {$value == 0} {
puts "No Errors Here: \n"
puts $message
} else {
puts "ERROR!- "
puts $message
}
--Dan Kuchler