Operating System - HP-UX
1748149 Members
3529 Online
108758 Solutions
New Discussion юеВ

Where to keep 'for loop' in given script ?

 
SOLVED
Go to solution
Archana1
Frequent Advisor

Where to keep 'for loop' in given script ?

# /usr/local/bin/sh

. /usr/local/bin/oraenv_DVEC6FIN

Declare
expected_count=50
current_count=`echo "psc" | tmadmin 2>/dev/null | grep ASYNCDECISI | wc -l`
if [ $current_count -lt $expected_count ]
then
echo alert > alert.log
fi

prev_count=$current_count

while [ 1 ]
do
current_count=`echo "psc" | tmadmin 2>/dev/null | grep ASYNCDECISI | wc -l`

if [ $current_count -lt $prev_count ] && [ $current_count -lt $expected_count ]
then
echo alert > alert.log
fi

if [ $prev_count -ne $current_count ]
then
prev_count=$current_count
fi
sleep 5
done
_______________________________________________

The above script is perfectly working (infinite) script. In which I wanted to include below for loop.
so that I can reduce number of scripts.

I tried keeping for loop after above while statement which didnt work. Pelase advice where I can keep this for for loop staement in the above scritp.

_______________________________________________

#Monitor count 12 services
SERVICE12="CANCELAPPLI FULLBATCHAPP GETBATCHDEC GETMULTIROWS OPTPAPERTCS PERSONALPRI TERMSIGNED"

for i in ${SERVICE12}
do
current_count=$(echo "psc" | tmadmin 2>/dev/null | grep -s -c ${i})

done
3 REPLIES 3
Earl_Crowder
Trusted Contributor

Re: Where to keep 'for loop' in given script ?

Is there a different expected count for each service?

The logic could be:

Initialize
Establish intial coutn for each service

While true;
for x y z ; do
get count
compare count to previous count
if conditions aren't favorable, log message
set previous to this
done
delay
done

James R. Ferguson
Acclaimed Contributor

Re: Where to keep 'for loop' in given script ?

Hi:

It would seem that you want to include the same logic for the "SERVICE12" list as you do for "ASYNCDECISI". If that's correct, I would declare your 'count' variables as arrays to match their elements with the list of "services' (including ASYNCDECISI).

The syntax:

# current_count=$(echo "psc" | tmadmin 2>/dev/null | grep -s -c ${i})

...is the modern syntax to replace archaic backticks like:

# current_count=`echo "psc" | tmadmin 2>/dev/null | grep ASYNCDECISI | wc -l`

Equally important, the first variation eliminates an extra process of 'wc' since 'grep' can return a count of matches it finds (as shown). Thus for two reasons, the first variation is far preferable to the second.

There is no sense to doing:

if [ $prev_count -ne $current_count ]
then
prev_count=$current_count
fi

...Simply do:

prev_count=$current_count

Thus, in all, you might do:

while [ 1 ]
do
set -A current_count 0 0 0 0 0 0 0 0
set -A prev_count 0 0 0 0 0 0 0 0
which=0
SERVICE12="ASYNCDECISI CANCELAPPLI FULLBATCHAPP GETBATCHDEC GETMULTIROWS OPTPAPERTCS PERSONALPRI TERMSIGNED"
for i in ${SERVICE12}
do
which=$((which+1))
current_count[$which]=$(echo "psc" | tmadmin 2>/dev/null | grep -s -c ${i})
...
done # with for loop
sleep 5
done # with while loop

...

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: Where to keep 'for loop' in given script ?

Hi (again):

There's an obvious, silly mistake in the code snippet I suggested. We certainly don't want to initialize the count arrays every time. I meant to write:

...
set -A current_count 0 0 0 0 0 0 0 0
set -A prev_count 0 0 0 0 0 0 0 0
SERVICE12="ASYNCDECISI CANCELAPPLI FULLBATCHAPP GETBATCHDEC GETMULTIROWS OPTPAPERTCS PERSONALPRI TERMSIGNED"
while [ 1 ]
do
which=0
for i in ${SERVICE12}
do
which=$((which+1))
current_count[$which]=$(echo "psc" | tmadmin 2>/dev/null | grep -s -c ${i})
...
done # with for loop
sleep 5
done # with while loop

Regards!

...JRF...