> grep xxx myfile > myfile.tmp > [ $? -eq 0 ] && cat myfile.tmp | mail someone > > Of course the following doesnt work but I hope there is a trick to write > something like > grep xxx myfile &&| mail someone > > "mail" is just an example. So I dont need special switches for the > mail-command, it is a shell question: > How to activate a pipe only if the exit status of the former command has > been oK.
I think the nearest is something like…
grep xxx myfile > myfile.tmp && cat myfile.tmp | mail someone
..if you don't want to create a "tmp" file. Adrian
One way to do it would be the following:
oIFS=$IFS;IFS='' RESULT=$(grep xxx myfile) [[ ! -z "$RESULT" ]] && echo $RESULT | mail someone IFS=$oIFS
That only works if (like grep) if the command fails and produces no output. You could change that to:
oIFS=$IFS;IFS='' RESULT=$(grep xxx myfile) [[ "$?" == 0 ]] && echo $RESULT | mail someone IFS=$oIFS
This might work also:
grep xxx myfile | ( [[ $? == 0 ]] && mail someone )
Be aware that that will run the mail command in a subshell (but you may not notice any difference if you use bash - bash runs the last command in a pipeline in a subshell anyway). What difference does that make? Well, consider he following:
grep xxx myfile | ( [[ $? == 0 ]] && mail someone || echo "Nothing found" )
You might reasonably expect the command to print out "Nothing found" if grep fails to find a match. It in fact produces no output since the echo prints to the subshell. The best way to achieve conditional piping (of a sort) is to use redirections as a kind of 'pipe emulator'. Search http://deja.com/ in comp.unix.shell for the subject "conditional piping" for previous discussions.
Dave. :-)
documented on: 2000.08.31 Thu 00:53:39