mercredi 15 juillet 2020

Else Condition in If Else Bash Script not Working

Good day,

Your kind help on this issue will be highly appreciated. Im new to this stuff and I did try to research on how to correctly execute this but I cant make it to work so far. I have several map files which I filter using grep commands to extract violation messages like so:

grep "SoftBin 404" wmap*CP2
wmap_01_CP2:DISP_OB:    SoftBin 404 is 11 dice exceeded Bin Reject Control limit of 11 dice
wmap_03_CP2:DISP_OB:    SoftBin 404 is 11 dice exceeded Bin Reject Control limit of 11 dice
wmap_17_CP2:DISP_OB:    SoftBin 404 is 13 dice exceeded Bin Reject Control limit of 11 dice
grep "SoftBin 418" wmap*CP2
wmap_01_CP2:DISP_OB:    SoftBin 418 is 22 dice exceeded Bin Reject Control limit of 20 dice
wmap_02_CP2:DISP_OB:    SoftBin 418 is 32 dice exceeded Bin Reject Control limit of 20 dice
wmap_03_CP2:DISP_OB:    SoftBin 418 is 48 dice exceeded Bin Reject Control limit of 20 dice
wmap_04_CP2:DISP_OB:    SoftBin 418 is 43 dice exceeded Bin Reject Control limit of 20 dice

Now I want to filter only those messages with fail count of more than 25. I was able to do that using awk :

grep "SoftBin 404" wmap*CP2 | awk '$5>25'

The above returned no result since the maps has fail qty (on column 5) of only 11,11 and 13 which is less than 25.

grep "SoftBin 418" wmap*CP2 | awk '$5>25'
wmap_02_CP2:DISP_OB:    SoftBin 418 is 32 dice exceeded Bin Reject Control limit of 20 dice
wmap_03_CP2:DISP_OB:    SoftBin 418 is 48 dice exceeded Bin Reject Control limit of 20 dice
wmap_04_CP2:DISP_OB:    SoftBin 418 is 43 dice exceeded Bin Reject Control limit of 20 dice

Now I want to put this in a bash script using if else statement but im having trouble getting the else part to work. Here is my script:

more test.sh 
#! /bin/bash 

printf "\n"
SB404=$(grep -q "Softbin 404" wmap*CP2 | awk '$5>25')
SB404_stat="$?"
if [ "$SB404_stat" -eq 0 ] ; then
   echo "Softbin 404 for reprobe" ; grep "SoftBin 404" wmap*CP2 | awk '$5>25'
else
   echo "No Softbin 404 for reprobe"
fi
printf "\n"
SB418=$(grep -q "Softbin 418" wmap*CP2 | awk '$5>25')
SB418_stat="$?"
if [ "$SB418_stat" -eq 0 ] ; then
   echo "Softbin 418 for reprobe" ; grep "SoftBin 418" wmap*CP2 | awk '$5>25'
else
   echo "No Softbin 418 for reprobe"
fi
printf "\n"

I got the grep -q part on one of forums I read and applied it, but Im not sure if its enough since I still have the awk part which will dictate the final result of that line. I know Im missing something or I just had it plain wrong. Here's my output:

./test.sh 

Softbin 404 for reprobe

Softbin 418 for reprobe
wmap_02_CP2:DISP_OB:    SoftBin 418 is 32 dice exceeded Bin Reject Control limit of 20 dice
wmap_03_CP2:DISP_OB:    SoftBin 418 is 48 dice exceeded Bin Reject Control limit of 20 dice
wmap_04_CP2:DISP_OB:    SoftBin 418 is 43 dice exceeded Bin Reject Control limit of 20 dice

Apology if this is too long, I just want to make my issue as clear as possible. Appreciate in advance all the help you can share. Thanks again.

Mike

Aucun commentaire:

Enregistrer un commentaire