Solved

Bash script with OR statement

Posted on 2007-04-10
3
4,263 Views
Last Modified: 2013-12-16
I am trying to write an OR statement in the nested IF statement that will look for various user emails.  My question here is, is there a more efficient way?  Also, does the statement look ok?

Here is what i have, i did not write the original script, i just expanded on it:

#!/bin/sh
SM="/usr/sbin/sendmail -i"

cd /var/spool/filter
trap "rm -f out.$$" 0 1 2 3 15
cat | /usr/bin/spamc > out.$$
if egrep -q "X-Spam-Flag: YES" < out.$$
then
    if [egrep -q "a.tester@test.com" < out.$$] || [egrep -q "b.tester@test,com" < out.$$]
    then
        $SM "$@" < out.$$
    else
        $SM quarantine@test.com < out.$$
    fi
else
        $SM "$@" < out.$$
fi
exit $?
0
Comment
Question by:Icetoad
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
Tintin earned 125 total points
Comment Utility
I'd write it as:

#!/bin/sh

SM="/usr/sbin/sendmail -i"
TEMP=/tmp/out.$$
trap "rm -f $TEMP" 0 1 2 3 15

cat | /usr/bin/spamc >$TEMP

if egrep -q "X-Spam-Flag: YES" $TEMP
then
    if egrep -q "a.tester@test.com|b.tester@test.com" $TEMP
    then
        $SM "$@" <$TEMP
    else
        $SM quarantine@test.com <$TEMP
    fi
else
    $SM "$@" <$TEMP
fi


Note that to test multiple patterns with egrep, you use the '|' sign, which means OR.
0
 
LVL 8

Author Comment

by:Icetoad
Comment Utility
ahh i was wondering about the | vs || one was noted as bolean and the other as logic (?).

I like that better than running egrep multiple times.  I will leave this open till tomorrow just incase anyone else has some input.
0
 
LVL 8

Author Comment

by:Icetoad
Comment Utility
Looks good, thanks for your help!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now