Solved

Bash script with OR statement

Posted on 2007-04-10
3
4,266 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wifi not working on Raspberry Pi 3? 2 56
Why isnt it sending mail from my php but is from my server 10 45
Choosing CentOS 16 80
awk sed 8 42
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

770 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