Expand bash script for linux into threads

This is a follow up to a question I had last night, where I got pretty good help right away(make a long story short, login to a webserver was solved using PERL)

What I want to expand on now, is as following; we have Nagios 3 running, and AFAIK it uses the exit codes to determine wether or not to raise an alarm.

It is in that spirit I've written this script as posted below.
It works now that if it finds one of the combinations of username/password incorrect in the external script it calls, it will then return an error code, move to the "else" and then post a log, before it exits with code 2 for ERROR

What I want todo, is if it is down, it should exit with error code 2, but before that, also check the 3 others.

How can I tell what error code it is to use at the end if it is to check all 3. Now it stops at the first that fails.

#!/bin/sh
 
echo ""
echo "||----------------------------------------||"
echo "|| GroupWise WebAccess sjekker            ||"
echo "|| Laget av Rune Darrud, BKB Data         ||"
echo "|| Versjon 4 - 18 november, 2008          ||"
echo "||----------------------------------------||"
echo ""
 
LOGGFIL=./logg.txt
GWWEBACC=./webacc
TEMPSAK=./temp.temp
PK1=./postkontor1.sh
PK2=./postkontor2.sh
PK3=./postkontor3.sh
 
# Date
DATE=$(date "+%Y.%m.%d %T")
 
TEXTTING=Login
PK1NAVN="****"
PK2NAVN="****"
PK3NAVN="****"
 
# Checking if the front page exists.
wget -q --no-check-certificate https://webmail/gw/webacc -O $GWWEBACC > $TEMPSAK
grep -q "$TEXTTING" $GWWEBACC
if [ $? -eq 0 ]
then 
 rm $GWWEBACC
 rm $TEMPSAK
else
 rm $GWWEBACC
 rm $TEMPSAK
 exit 2
fi
 
# Checking the first login
perl $PK1 > $TEMPSAK
grep -q "$PK1NAVN" $TEMPSAK
if [ $? -eq 0 ]
then
 rm $TEMPSAK
else
 rm $TEMPSAK
 exit 2
fi
 
# Checking the second login
perl $PK2 > $TEMPSAK
grep -q "$PK2NAVN" $TEMPSAK
if [ $? -eq 0 ]
then
 rm $TEMPSAK
else
 rm $TEMPSAK
 exit 2
fi
 
# Checking the third login
perl $PK3 > $TEMPSAK
grep -q "$PK3NAVN" $TEMPSAK
if [ $? -eq 0 ]
then
 rm $TEMPSAK
else
 rm $TEMPSAK
 exit 2
fi
echo $DATE - OK : ALT OK! >> $LOGGFIL
 
# Rydder opp
exit 0

Open in new window

LVL 2
TheFlyingCorpseAsked:
Who is Participating?
 
amit_gCommented:
The last line

echo $DATE - OK : ALT OK! >> $LOGGFIL

should be changed to

if [ $ExitCode -eq 0 ]
then
 echo $DATE - OK : ALT OK! >> $LOGGFIL
else
 exit $ExitCode
fi
0
 
amit_gCommented:
Add

ExitCode=0

in the beginning and then change all

exit 2

to

ExitCode=2

then in the end add

exit ExitCode
0
 
TheFlyingCorpseAuthor Commented:
Thanks for your quick reply amit_g,

I was unable to get it to work at first, but after I saw that I had used ExitCode AND EXITCODE (I use variables in capital letters to see them easier), the problem got solved.


Thank you very much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.