quick unix help - cat'ing and mailing a results file
quick unix help - cat'ing and mailing a results file
Author
Discussion

sybaseian

Original Poster:

1,826 posts

306 months

Monday 19th February 2007
quotequote all
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

rebelstar

1,146 posts

275 months

Monday 19th February 2007
quotequote all
Here you go.

#!/bin/ksh

grep "(0 row affected)" inputfile
if [ $? -eq 0 ];
then
cat inputfile | mailx -s "test file" xxx@yyy.com
fi

deckster

9,631 posts

286 months

Monday 19th February 2007
quotequote all
if [[ `grep -c 'row affected' inputfile` -ne 0 && `grep -c '(0 row affected)' inputfile` -eq 0 ]]
then
# mail file
else
# do nothing
fi

MooSchmoo

176 posts

269 months

Monday 19th February 2007
quotequote all
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



Edited by MooSchmoo on Monday 19th February 23:25

sybaseian

Original Poster:

1,826 posts

306 months

Tuesday 20th February 2007
quotequote all
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.

Edited by sybaseian on Tuesday 20th February 00:51

MooSchmoo

176 posts

269 months

Tuesday 20th February 2007
quotequote all
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

sybaseian

Original Poster:

1,826 posts

306 months

Tuesday 20th February 2007
quotequote all
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


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

MooSchmoo

176 posts

269 months

Tuesday 20th February 2007
quotequote all
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


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