quick unix help - cat'ing and mailing a results file
Discussion
I have a quick unix Korn shell request....
I have a results file that I want to grep for a key string "(0 row affected)". If the result matches "(0 row affected)" then do nothing and end, if it is anything other than that, eg - "(100 row affected)", I want to cat the entire file and then mail it to myself.
Syntax I've been playing with:-
if [ `grep "(0 row affected)" inputfile` = "(0 row affected)" ];
then
exit
else
if [ `grep "(0 row affected)" inputfile` != "(0 row affected)" ];
then
cat inputfile | mailx -s "test file" xxxx@yyyy.zzz
fi
cheers,
Ian
I have a results file that I want to grep for a key string "(0 row affected)". If the result matches "(0 row affected)" then do nothing and end, if it is anything other than that, eg - "(100 row affected)", I want to cat the entire file and then mail it to myself.
Syntax I've been playing with:-
if [ `grep "(0 row affected)" inputfile` = "(0 row affected)" ];
then
exit
else
if [ `grep "(0 row affected)" inputfile` != "(0 row affected)" ];
then
cat inputfile | mailx -s "test file" xxxx@yyyy.zzz
fi
cheers,
Ian
Assuming you're always going to have a "row affected" entry..
#!/bin/ksh
# spunky.sh
if ! grep -q "(0 row affected)" $1;
then
mailx -s "Results" xxx@yyy.zz < $1;
fi
then just
$ ./spunky.sh inputfile
Lots of room for fubars there tho
Cheers,
Matt
#!/bin/ksh
# spunky.sh
if ! grep -q "(0 row affected)" $1;
then
mailx -s "Results" xxx@yyy.zz < $1;
fi
then just
$ ./spunky.sh inputfile
Lots of room for fubars there tho
Cheers,
Matt
Edited by MooSchmoo on Monday 19th February 23:25
still having problems.
this cat'ing and mailing is the final part of a shell script that queries a database and outputs the results to a formated file which I want to mail if the query returned one or more rows of data.
If no rows were returned, the last line of the output file is - (0 row affected)
If more than one row was returned, it will give a count of how many rows were returned as - (XXX row affected)
If there is at least one row returned, I want to mail the file, but if no rows were returned I don't want to mail anything.
I'm still trying to get the logic for the if statements to work correctly.
banging head against wall......
cheers,
Ian
if ! grep -q "(0 row affected)" 'inputfile';
then
mailx -s "Results" xxx@yyy.zz < 'inputfile';
fi
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .
If I change the original sql query to either return no rows or to return data, it is still mailing the output file on both occasions.
any suggestions for the logic of pattern testing and then either mailing or not mailing.
this cat'ing and mailing is the final part of a shell script that queries a database and outputs the results to a formated file which I want to mail if the query returned one or more rows of data.
If no rows were returned, the last line of the output file is - (0 row affected)
If more than one row was returned, it will give a count of how many rows were returned as - (XXX row affected)
If there is at least one row returned, I want to mail the file, but if no rows were returned I don't want to mail anything.
I'm still trying to get the logic for the if statements to work correctly.
banging head against wall......
cheers,
Ian
if ! grep -q "(0 row affected)" 'inputfile';
then
mailx -s "Results" xxx@yyy.zz < 'inputfile';
fi
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .
If I change the original sql query to either return no rows or to return data, it is still mailing the output file on both occasions.
any suggestions for the logic of pattern testing and then either mailing or not mailing.
Edited by sybaseian on Tuesday 20th February 00:51
MooSchmoo said:
You can drop the -q option if your version of grep doesn't support it.. it just stop's matches printed to stdout.. try redirecting to dev/null..
if ! grep "(0 row affected)" >/dev/null .. etc
are you sure it's not "rows" rather than "row" ?
Matt
if ! grep "(0 row affected)" >/dev/null .. etc
are you sure it's not "rows" rather than "row" ?
Matt
yep - your right it's plural. Long day and missed the syntax. I've been scripting since 9am yesterday morning and the brain has started to meltdown.
Cheers for the sanity check Matt.
Ian
sybaseian said:
MooSchmoo said:
You can drop the -q option if your version of grep doesn't support it.. it just stop's matches printed to stdout.. try redirecting to dev/null..
if ! grep "(0 row affected)" >/dev/null .. etc
are you sure it's not "rows" rather than "row" ?
Matt
if ! grep "(0 row affected)" >/dev/null .. etc
are you sure it's not "rows" rather than "row" ?
Matt
yep - your right it's plural. Long day and missed the syntax. I've been scripting since 9am yesterday morning and the brain has started to meltdown.
Cheers for the sanity check Matt.
Ian
Hehe I know where you're coming from Ian.. good luck with it anyway

Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff


