Skip to content
Advertisement

sh/awk (partly) script returning a null value but want it to return a value

I have the following sh script:

#!/bin/sh


function usage(){
echo ""
echo " use like this: cat ../mfshangingsoctelsim.2015-07-28.*.log | ./kevin.sh ms504201"
echo " use one of the following scanners for alh: ms504201 ms503201 ms502201 ms501201"
echo "now exiting..."
exit 1
}


#[[ $# -eq 1 ]] || usage
#[[ $1 == "attack" ]] || usage


date=$(date --date="-2 days" +%F)


clear
echo "==============================================================="
echo "    RED status on BCS loops on $date"
echo "==============================================================="
echo


grep -E 'SCS([0-9]*)_ORDER(_OUTER|)_LOOP' $(ls -tr ../mfsBoxedOutboundService.$date.*.log) |
awk -v scanner="$1" '
begin{
indexx = 0
outerred = 0  # is it time to check all SCS? - 1 yes as OUTER is red
scs01=0;scs02=0;scs03=0;scs04=0;scs05=0;scs07=0;scs08=0;scs09=0;scs10=0
scs11=0;scs12=0;scs13=0;scs14=0;scs15=0;scs17=0;scs18=0;scs19=0;scs20=0
t = 0
tt = 0
}

{
printf($3);
if (match($0,"state=RED")&&match($0,"SCS_ORDER_OUTER_LOOP")&&outerred==0) {scsouterred[indexx]=substr($3,1,8);
                                                                                scsouterbreachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);
                                                                                outerred=1;
                                                                                scs01=1;scs02=1;scs03=1;scs04=1;scs05=1;scs07=1;scs08=1;scs09=1;scs10=1;
                                                                                scs11=1;scs12=1;scs13=1;scs14=1;scs15=1;scs17=1;scs18=1;scs19=1;scs20=1;
                                                                        }

if (outerred==1) {

        if (match($0,"state=GREEN")&&match($0,"SCS_ORDER_OUTER_LOOP")) {scsoutergreen[indexx]=substr($3,1,8);indexx++;outerred=0}

        if (scs01=1&&match($0,"SCS01_ORDER_LOOP")) {scs01breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs01=0;}
        if (scs02=1&&match($0,"SCS02_ORDER_LOOP")) {scs02breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs02=0;}


                        }  # main loop to collect scs levels and wait for green

printf("rrrrrrrrrrrrr");
}

END{
print "";print "-------------scs------------------------------------------------";
i = (substr(scsoutergreen[a],1,2)-substr(scsouterred[a],1,2))*60+(substr(scsoutergreen[a],4,2)-substr(scsouterred[a],4,2))
for (a in scsouterred) {
t = (substr(scsoutergreen[a],1,2)-substr(scsouterred[a],1,2))*60+(substr(scsoutergreen[a],4,2)-substr(scsouterred[a],4,2))
print scsouterred[a],"-",scsoutergreen[a],"-",t,"min","outer:",scsouterbreachlevel[a],"scs01:",scs01breachlevel[a],"scs02:",scs02breachlevel[a],"scs03:",scs03breachlevel[a],"scs04:",scs04breachlevel[a],"scs05:",scs05breachlevel[a],"scs07:",scs07breachlevel[a],"scs08:",scs08breachlevel[a],"scs09:",scs09breachlevel[a],"scs10:",scs10breachlevel[a],"scs11:",scs11breachlevel[a],"scs12:",scs12breachlevel[a],"scs13:",scs13breachlevel[a],"scs14:",scs14breachlevel[a],"scs15:",scs15breachlevel[a],"scs17:",scs17breachlevel[a],"scs18:",scs18breachlevel[a],"scs19:",scs19breachlevel[a],"scs20:",scs20breachlevel[a];
tt = tt + t;
}
print "total number of incidents: ",indexx," for ",tt," minutes"



}

'

the values that it is outputting looks like the following:

===============================================================
    RED status on BCS loops on 2015-12-17
===============================================================

23:59:59.969
-------------scs------------------------------------------------
16:08:32 - 16:18:54 - 10 min outer: 550 scs01: 4 scs02: 2 scs03: 5 scs04: 9 scs05: 6 scs07: 7 scs08: 8 scs09: 9 scs10: 6 scs11: 3 scs12: 4 scs13: 24 scs14: 4 scs15: 5 scs17: 7 scs18: 5 scs19: 6 scs20: 9
12:33:15 - 12:36:33 - 3 min outer: 550 scs01: 2 scs02: 9 scs03: 13 scs04: 4 scs05: 8 scs07: 2 scs08: 7 scs09: 7 scs10: 10 scs11: 5 scs12: 6 scs13: 5 scs14: 4 scs15: 6 scs17: 7 scs18: 11 scs19: 11 scs20: 9
16:20:34 - 16:25:44 - 5 min outer: 550 scs01: 8 scs02: 6 scs03: 10 scs04: 7 scs05: 7 scs07: 21 scs08: 0 scs09: 10 scs10: 3 scs11: 3 scs12: 6 scs13: 6 scs14: 3 scs15: 5 scs17: 9 scs18: 4 scs19: 14 scs20: 11
16:41:57 - 16:51:02 - 10 min outer: 550 scs01: 1 scs02: 4 scs03: 8 scs04: 4 scs05: 2 scs07: 0 scs08:  scs09: 8 scs10: 7 scs11: 7 scs12: 8 scs13: 1 scs14: 3 scs15: 1 scs17: 6 scs18: 4 scs19:  scs20: 25
18:22:54 - 18:44:08 - 22 min outer: 550 scs01: 4 scs02: 5 scs03: 9 scs04: 5 scs05: 11 scs07: 1 scs08: 3 scs09: 7 scs10: 13 scs11: 5 scs12: 8 scs13: 4 scs14: 8 scs15: 7 scs17: 10 scs18: 10 scs19: 15 scs20: 9
19:02:09 - 19:08:17 - 6 min outer: 550 scs01: 3 scs02: 5 scs03: 6 scs04: 1 scs05: 6 scs07: 3 scs08: 12 scs09: 3 scs10:  scs11: 1 scs12: 5 scs13: 4 scs14: 6 scs15: 4 scs17: 6 scs18: 2 scs19: 12 scs20: 8
19:28:21 - 19:33:24 - 5 min outer: 551 scs01: 4 scs02: 3 scs03: 5 scs04: 5 scs05: 7 scs07: 1 scs08:  scs09: 0 scs10: 2 scs11: 1 scs12: 1 scs13: 0 scs14: 2 scs15: 4 scs17: 3 scs18: 4 scs19: 17 scs20:
23:19:04 - 23:55:56 - 36 min outer: 550 scs01: 3 scs02: 3 scs03: 6 scs04: 5 scs05: 4 scs07: 4 scs08: 13 scs09: 2 scs10: 7 scs11: 5 scs12: 7 scs13: 7 scs14: 5 scs15: 4 scs17: 2 scs18: 8 scs19: 7 scs20: 9
23:59:05 -  - -1439 min outer: 550 scs01: 1 scs02: 5 scs03: 6 scs04: 5 scs05: 13 scs07: 2 scs08: 11 scs09: 7 scs10: 6 scs11: 3 scs12: 12 scs13: 6 scs14: 4 scs15: 6 scs17: 6 scs18: 7 scs19: 6 scs20: 8
12:39:56 - 12:43:15 - 4 min outer: 550 scs01: 6 scs02: 14 scs03: 14 scs04: 2 scs05: 5 scs07: 5 scs08: 10 scs09: 3 scs10: 11 scs11: 6 scs12: 4 scs13: 7 scs14: 9 scs15: 10 scs17: 3 scs18: 7 scs19: 5 scs20: 9
12:43:38 - 12:48:34 - 5 min outer: 550 scs01: 2 scs02: 1 scs03: 7 scs04: 8 scs05: 7 scs07: 4 scs08: 8 scs09: 4 scs10: 13 scs11: 4 scs12: 3 scs13: 4 scs14: 9 scs15: 7 scs17: 4 scs18: 1 scs19: 3 scs20: 0
12:56:44 - 13:20:07 - 24 min outer: 550 scs01: 1 scs02: 1 scs03: 10 scs04: 6 scs05: 6 scs07: 0 scs08: 6 scs09: 3 scs10: 0 scs11: 1 scs12: 3 scs13: 3 scs14: 4 scs15: 4 scs17: 5 scs18: 2 scs19: 8 scs20: 9
total number of incidents:  11  for  -1309  minutes

As you can see it is returning a null value against the following line:

23:59:05 -  - -1439 min outer: 550 scs01: 1 scs02: 5 scs03: 6 scs04: 5 scs05: 13 scs07: 2 scs08: 11 scs09: 7 scs10: 6 scs11: 3 scs12: 12 scs13: 6 scs14: 4 scs15: 6 scs17: 6 scs18: 7 scs19: 6 scs20: 8

This should look like the lines above and below. ([time] – [time] – [duration] etc)

Which is in turn effecting the time calculation ( negative number)

I need to put something around the scsoutergreen call to stop it returning a null value. I have tried the following:

if [ -n "scsoutergreen[a]" ]; then "23:55:99" fi

but this returns a syntax error on if, ] and then.

any ideas?

Thanks in advance

Advertisement

Answer

I managed to get this working.

I did the following:

#!/bin/sh


function usage(){
echo ""
echo " use like this: cat ../mfshangingsoctelsim.2015-07-28.*.log | ./kevin.sh ms504201"
echo " use one of the following scanners for alh: ms504201 ms503201 ms502201 ms501201"
echo "now exiting..."
exit 1
}


#[[ $# -eq 1 ]] || usage
#[[ $1 == "attack" ]] || usage



date=$(date --date="-1 days" +%F)


clear
echo "==============================================================="
echo "    RED status on BCS loops on $date"
echo "==============================================================="
echo


grep -E 'SCS([0-9]*)_ORDER(_OUTER|)_LOOP' $(ls -tr ../mfsBoxedOutboundService.$date.*.log) |
awk -v scanner="$1" '
begin{
indexx = 0
outerred = 0  # is it time to check all SCS? - 1 yes as OUTER is red
scs01=0;scs02=0;scs03=0;scs04=0;scs05=0;scs07=0;scs08=0;scs09=0;scs10=0
scs11=0;scs12=0;scs13=0;scs14=0;scs15=0;scs17=0;scs18=0;scs19=0;scs20=0
t = 0
tt = 0
}

{
printf($3);
if (match($0,"state=RED")&&match($0,"SCS_ORDER_OUTER_LOOP")&&outerred==0) {scsouterred[indexx]=substr($3,1,8);
                                                                                scsouterbreachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);
                                                                                outerred=1;
                                                                                scs01=1;scs02=1;scs03=1;scs04=1;scs05=1;scs07=1;scs08=1;scs09=1;scs10=1;
                                                                                scs11=1;scs12=1;scs13=1;scs14=1;scs15=1;scs17=1;scs18=1;scs19=1;scs20=1;
                                                                        }

if (outerred==1) {

        if (match($0,"state=GREEN")&&match($0,"SCS_ORDER_OUTER_LOOP")) {scsoutergreen[indexx]= substr($3,1,8);indexx++;outerred=0}

        if (scs01=1&&match($0,"SCS01_ORDER_LOOP")) {scs01breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs01=0;}
        if (scs02=1&&match($0,"SCS02_ORDER_LOOP")) {scs02breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs02=0;}
        if (scs03=1&&match($0,"SCS03_ORDER_LOOP")) {scs03breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs03=0;}
        if (scs04=1&&match($0,"SCS04_ORDER_LOOP")) {scs04breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs04=0;}
        if (scs05=1&&match($0,"SCS05_ORDER_LOOP")) {scs05breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs05=0;}
        if (scs07=1&&match($0,"SCS07_ORDER_LOOP")) {scs07breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs07=0;}
        if (scs08=1&&match($0,"SCS08_ORDER_LOOP")) {scs08breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs08=0;}
        if (scs09=1&&match($0,"SCS09_ORDER_LOOP")) {scs09breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs09=0;}
        if (scs10=1&&match($0,"SCS10_ORDER_LOOP")) {scs10breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs10=0;}
        if (scs11=1&&match($0,"SCS11_ORDER_LOOP")) {scs11breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs11=0;}
        if (scs12=1&&match($0,"SCS12_ORDER_LOOP")) {scs12breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs12=0;}
        if (scs13=1&&match($0,"SCS13_ORDER_LOOP")) {scs13breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs13=0;}

        if (scs14=1&&match($0,"SCS14_ORDER_LOOP")) {scs14breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs14=0;}
        if (scs15=1&&match($0,"SCS15_ORDER_LOOP")) {scs15breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs15=0;}
        if (scs17=1&&match($0,"SCS17_ORDER_LOOP")) {scs17breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs17=0;}
        if (scs18=1&&match($0,"SCS18_ORDER_LOOP")) {scs18breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs18=0;}
        if (scs19=1&&match($0,"SCS19_ORDER_LOOP")) {scs19breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs19=0;}
        if (scs20=1&&match($0,"SCS20_ORDER_LOOP")) {scs20breachlevel[indexx]=substr($0,index($0,"occupancy=")+10,index($0,"]>")-index($0,"occupancy=")-10);scs20=0;}

                        }  # main loop to collect scs levels and wait for green

printf("rrrrrrrrrrrrr");
}

END{
print "";print "-------------scs------------------------------------------------";
i = (substr(scsoutergreen[a],1,2)-substr(scsouterred[a],1,2))*60+(substr(scsoutergreen[a],4,2)-substr(scsouterred[a],4,2))
for (a in scsouterred) {
if (((substr(scsoutergreen[a],1,2)-substr(scsouterred[a],1,2))*60)<=0&&(substr(scsoutergreen[a],4,2)-substr(scsouterred[a],4,2))<=0) {t = 0} #start of t = if
else if (((substr(scsoutergreen[a],1,2)-substr(scsouterred[a],1,2))*60)<= 0) {t = (0+(substr(scsoutergreen[a],4,2)-substr(scsouterred[a],4,2)))}
else if ((substr(scsoutergreen[a],4,2)-substr(scsouterred[a],4,2))<= 0) {t = (((substr(scsoutergreen[a],1,2)-substr(scsouterred[a],1,2))*60)+0)}
else {t = ((substr(scsoutergreen[a],1,2)-substr(scsouterred[a],1,2))*60+(substr(scsoutergreen[a],4,2)-substr(scsouterred[a],4,2)))} #end of t = if
print scsouterred[a],"-",scsoutergreen[a],"-",t,"min","outer:",scsouterbreachlevel[a],"scs01:",scs01breachlevel[a],"scs02:",scs02breachlevel[a],"scs03:",scs03breachlevel[a],"scs04:",scs04breachlevel[a],"scs05:",scs05breachlevel[a],"scs07:",scs07breachlevel[a],"scs08:",scs08breachlevel[a],"scs09:",scs09breachlevel[a],"scs10:",scs10breachlevel[a],"scs11:",scs11breachlevel[a],"scs12:",scs12breachlevel[a],"scs13:",scs13breachlevel[a],"scs14:",scs14breachlevel[a],"scs15:",scs15breachlevel[a],"scs17:",scs17breachlevel[a],"scs18:",scs18breachlevel[a],"scs19:",scs19breachlevel[a],"scs20:",scs20breachlevel[a];
tt = tt + t;
}
print "total number of incidents: ",indexx," for ",tt," minutes rr"
print "-----NOTE-----r"
print "If time is empty recovery was after 23:59:99 on ",$3

}

'

The changes I made from the original is that I modified the setting of the t variable in the lower half of the script from:

t = (substr(scsoutergreen[a],1,2)-substr(scsouterred[a],1,2))*60+(substr(scsoutergreen[a],4,2)-substr(scsouterred[a],4,2))

To the following:

if (((substr(scsoutergreen[a],1,2)-substr(scsouterred[a],1,2))*60)<=0&&(substr(scsoutergreen[a],4,2)-substr(scsouterred[a],4,2))<=0) {t = 0} #start of t = if
else if (((substr(scsoutergreen[a],1,2)-substr(scsouterred[a],1,2))*60)<= 0) {t = (0+(substr(scsoutergreen[a],4,2)-substr(scsouterred[a],4,2)))}
else if ((substr(scsoutergreen[a],4,2)-substr(scsouterred[a],4,2))<= 0) {t = (((substr(scsoutergreen[a],1,2)-substr(scsouterred[a],1,2))*60)+0)}
else {t = ((substr(scsoutergreen[a],1,2)-substr(scsouterred[a],1,2))*60+(substr(scsoutergreen[a],4,2)-substr(scsouterred[a],4,2)))}

This resolves the null time issue and returns a 0 if the time works out to be <= 0 or the actual calculation if it isn’t.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement