1829265 Members
9232 Online
109988 Solutions
New Discussion

Script with flags

 
SOLVED
Go to solution
Coolmar
Esteemed Contributor

Script with flags

Hi all,
I have a script that does a daily check of a file. If it finds a certain condition, it will email. However, I only want it to email twice. So it will check for flag1 and if it exists it checks for flag2. If both exist, I want it not to email. Here is what I have...it is part of a case statement:

ABCD)
if [ $pcent -lt 15 ]; then
find $TMPDIR -type f -mtime +30 -exec rm -f {} \; #if flags are older than a month remove
if [ -f $TMPDIR/$name.2 ]; then
HERE IS WHERE I AM STUCK
elif [ -f $TMPDIR/$name.1 ]; then
touch $TMPDIR/$name.2
else
touch $TMPDIR/$name.1
fi
mail -t $maila <Subject: $mailsubj
From: $name
Cc: $dbagroup
message body
EOF
fi
;;

DEFG)
same as above
EOF
;;

So my problem is that it works great if no flags exist or only flag1. I don't know what to do when it finds both flags. I had "exit" but then the script stops and it doesn't run through the other cases. I had echo "nothing" to /dev/null but then it still emails. I guess I could put the whole email spiel in each if statement, but just thought I would check with you guys to see if there is something much simpler I could be doing.

Thanks!
4 REPLIES 4
Peter Nikitka
Honored Contributor
Solution

Re: Script with flags

Hi,

I would just set a condition to send mail or not in the check routines and check that condition later. E.g.:
...
ABCD)
if [ $pcent -lt 15 ]; then
find $TMPDIR -type f -mtime +30 -exec rm -f {} \; #if flags are older than a month remove
sendit=y # preset condition
if [ -f $TMPDIR/$name.2 ]; then
sendit= # change condition
elif [ -f $TMPDIR/$name.1 ]; then
touch $TMPDIR/$name.2
else
touch $TMPDIR/$name.1
fi
# check condition
[ "$sendit" ] &&
mail -t $maila <Subject: $mailsubj
From: $name
Cc: $dbagroup
message body
EOF
fi
;;
...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
James R. Ferguson
Acclaimed Contributor

Re: Script with flags

Hi:

There are several ways here. One way is to rearrange your code similar to:

...
if [ ! -f "$name.1" ]; then
touch $name.1
elif [ ! -f "$name.2" ]; then
touch $name.2
else
mail ...
fi

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Script with flags

You need to discover the shell's logical and and logical or:

Classic syntax:

if [ -f ${TMPDIR}/${name}.1 -o -f ${TMPDIR}/${name}.2 ] # -o => OR
then
# at least one file was found
else
if [ -f ${TMPDIR}/${name}.1 -a -f ${TMPDIR}/${name}.2 ] # -a => AND
then
# both were found
else
if [ -f ${TMPDIR}/${name}.1 ]
then
# only file1 was found
else
# only file2 was found
fi
fi
else
# No files were found
fi


Newer more efficient equivalent syntax for Korn and POSIX shell syntax (Note the double ]]'s and [['s meaning the shell evaluates internally rather than using the external test command.):

if [[ -f ${TMPDIR}/${name}.1 || -f ${TMPDIR}/${name}.2 ]] # || => OR
then
# at least one file was found
else
if [[ -f ${TMPDIR}/${name}.1 && -f ${TMPDIR}/${name}.2 ]] # && => AND
then
# both were found
else
if [[ -f ${TMPDIR}/${name}.1 ]]
then
# only file1 was found
else
# only file2 was found
fi
fi
else
# No files were found
fi

If it ain't broke, I can fix that.
Coolmar
Esteemed Contributor

Re: Script with flags

Thanks guys...that helped alot!