[Nottingham] bash syntax: elif

Mark O'Shea mark at musicalstoat.co.uk
Tue Jul 27 10:23:15 BST 2004


On Tue, 2004-07-27 at 16:59, David Wolfson wrote:
> In my continuing quest to automate some of the tasks on our shared machine, I've persuaded it to run a virus sweep all on its own (clever little chap isn't it!).  I can even get it to mail me if it works ok.  What I'd like it to do is send a different mail if detects a virus, or if it encounters errors.  I think this should be possible as the man file for the virus software lists diffent error codes for different outcomes:
> 
>        0      If no errors are encountered and no viruses are found.
>        1      If  the user interrupts SWEEP (usually by pressing control-C) or
>               kills the process.
>        2      If some error preventing further execution is discovered.
>        3      If viruses or virus fragments are discovered.
> 
> At the moment I use the following mail loop:
> 
> if [ $? -eq 0 ]
> then 
> echo 'pepdm1 virus sweep clear'|mail -s 'sweep' eaxdrw at nottingham.ac.uk 
> else
> echo 'pepdm1 virus sweep error'|mail -s 'sweep' eaxdrw at nottingham.ac.uk 
> fi
> 
> but tried changing it to this:
> 
> if [ $? -eq 0 ]
> 	then 
> 	echo 'pepdm1 virus sweep clear'|mail -s 'sweep' eaxdrw at nottingham.ac.uk 
> elif [$? -eq 1 ]
> 	then
> 	echo 'pepdm1 virus sweep interupted'|mail -s 'sweep' eaxdrw at nottingham.ac.uk 
> elif [$? -eq 2 ]
> 	then
> 	echo 'pepdm1 virus sweep error'|mail -s 'sweep' eaxdrw at nottingham.ac.uk 
> elif [$? -eq 3 ]
> 	then
> 	echo 'pepdm1 virus detected'|mail -s 'sweep' eaxdrw at nottingham.ac.uk 
> fi
> 
> and got no mail at all.  I'm guessing that I have something wrong in the elif syntax but a quick google only seemed to get me very specific questions on user forums.  Can anyone either point out the obvious errors, or suggest an intermediate bash help site please?
> 
Well, the only thing I note Dave is that it you don't seem to have a
space after the opening square bracket of any of the elif statements. 
This will cause it to fail.

You might also like to consider using the 'case' instead of lots of
elifs.  See the scripts in /etc/init.d/ or /etc/rc.d/init.d/ for
details.

Regards,
-- 
Mark O'Shea
(In a very good mood as he's getting married soon!)




More information about the Nottingham mailing list