Solved

Bash script with OR statement

Posted on 2007-04-10
3
4,268 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
ID: 18885706
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
ID: 18885829
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
ID: 18889533
Looks good, thanks for your help!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

679 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