1753797 Members
8021 Online
108805 Solutions
New Discussion юеВ

Scripting Question

 
Malcolm Neo_1
New Member

Scripting Question

Below is the code fragment:
if [ $BINDIR/schedule $nodename = "A" ]
then
echo "Message 1"
exit 1
fi
if [ $BINDIR/schedule $nodename = "B" ]
then
echo "Message 2"
exit 0
fi
if [ $BINDIR/schedule $nodename = "C" ]
then
echo "Message 3"
exit 1
fi
sendsms "Message 4" "TT0001" "MYServer"

"schedule" is another script called to check a textfile for values.

Based on the code fragment, lets assuming that "schedule $nodename" is suppose to return "A". Is there any chance that the "sendsms" is run before the "schedule" could return the value? (esp in the scenario that the main script is triggered many times simultaneously)
13 REPLIES 13
Hein van den Heuvel
Honored Contributor

Re: Scripting Question

No. If schedule returns A then the script will exit before executing sendsms.

It would appear that 'schedule' returned something other then A, B, or C under those high contention situation. Maybe it failed outright?.

I would capture the return value in a variable and include that in the senssms text, as well as the nodename that triggered it.

fwiw,
Hein.
Malcolm Neo_1
New Member

Re: Scripting Question

On the script,

echo "Message #" is written to a test file.

i.e. echo "Message #" >> script.log

and for the last part of the script where the sendsms is run, I have something like this:

echo "sendsms Message 4 TT0001 MYServer" >> script.log

The funny thing is when I look back at the log.

I found the follow text in the follow order:
sendsms Message 4 TT0001 MYServer
Message #

In the normal situation, "Message #" should be returned and the script should end and "sendsms Message 4 TT0001 MYServer" should not appear.
Mel Burslan
Honored Contributor

Re: Scripting Question

your second message tells me that when you run your script, you are redirecting the output to this script.log or something you are telling is inconsistent.

if you can tell what you are really after, i.e., what you want this script to do, maybe the help gets better.

________________________________
UNIX because I majored in cryptology...
Mel Burslan
Honored Contributor

Re: Scripting Question


if [ $BINDIR/schedule $nodename = "A" ]
then
echo "Message 1"
exitFLAG=1
fi

if [ $BINDIR/schedule $nodename = "B" ]
then
echo "Message 2"
exitFLAG=0
fi

if [ $BINDIR/schedule $nodename = "C" ]
then
echo "Message 3"
exitFLAG=1
fi

sendsms "Message 4" "TT0001" "MYServer"
exit $exitFLAG

________________________________
UNIX because I majored in cryptology...
Bill Hassell
Honored Contributor

Re: Scripting Question

Your code really needs to use the case statement rather than if/else/fi. It's a lot easier to read and the last test catches all text not found in the previous tests:

RESULT=$($BINDIR/schedule $nodename)

case $RESULT in
A) echo "Message 1"
exit 1

B) echo "Message 2"
exit 0

C) echo "Message 3"
exit 1

*) sendsms "Message 4" "TT0001" "MYServer"


The case statement is doing the comparison and allows for multiple choices, longer strings, use of regular expressions like *, and so on.


Bill Hassell, sysadmin
Malcolm Neo_1
New Member

Re: Scripting Question

Sorry for the confusion. This is what it should look like:

if [ $BINDIR/schedule $nodename = "A" ]
then
echo "Message 1" >> script.log
exit 1
fi
if [ $BINDIR/schedule $nodename = "B" ]
then
echo "Message 2" >> script.log
exit 0
fi
if [ $BINDIR/schedule $nodename = "C" ]
then
echo "Message 3" >> script.log
exit 1
fi
sendsms "Message 4" "TT0001" "MYServer"
echo "sendsms Message 4 TT0001 MYServer" >> script.log

The "if" conditions above is to check if there is a scheduled downtime. If yes, depending of what type of downtime, it will write it to the log file and then exit.

In the case where there is no downtime recorded, all conditions should be invalid and a sms would be sent out and the message will be recorded in the same log file.

However, this is what is seen on the script.log:
sendsms Message 4 TT0001 MYServer
Message 2
Muthukumar_5
Honored Contributor

Re: Scripting Question

You can try as,

downtime_type=$($BINDIR/schedule $nodename)

if [ "${downtime_type}" = "A" ]
then
echo "Message 1" >> script.log
exit 1
elif [ "${downtime_type}" = "B" ]
then
echo "Message 2" >> script.log
exit 0
elif [ "${downtime_type}" = "C" ]
then
echo "Message 3" >> script.log
exit 1
else
sendsms "Message 4" "TT0001" "MYServer"
echo "sendsms Message 4 TT0001 MYServer" >> script.log
fi

More than this, use Bill's case method. It is neat.

hth.
Easy to suggest when don't know about the problem!
Hein van den Heuvel
Honored Contributor

Re: Scripting Question


Bill'ds case statement solution reminded me of an onder remark I forgot to make explicit...

What if the $BINDIR/schedule result changes exactly between two steps. That is, while it was testing for A is was C, and when it tested for B is had already become A, but neither triggered a match.

So in a concurrent environment you HAVE to first grab the result into a variable, then test it. It is not only the more efficient way to go about business, it is the only right way to do the job.

Regards,
Hein.
Howard Marshall
Regular Advisor

Re: Scripting Question

Are there any date codes or "begin ├в ┬жend" statements written to the script.log file?

Could it be that what you are seeing is yesterdays run of the script which sent the sms message and todays which generated message 2

I also agree that your best bet is to put it into a case statement.

H