How to use a string in the for loop?

Hi Guys,

I have the below 2 exception which I want to monitor and subsequently many more. My problem is since the For loop takes the syntax for variable in word1 word2 as separate entities and my error strings are consisting of many words , how to treat the whole error string as a single entity.

 
Exception 1: org.com.Exception JMS MQ Error
Exception 2: org.db.Exception Db2 SQL Error

What can I do to modify the below code so that I can include individual exceptions in a for loop? The below code will treat each word as a different variable

for exception in org.com.Exception JMS MQ Error org.db.Exception Db2 SQL Error

---------- Post updated at 01:55 AM ---------- Previous update was at 01:43 AM ----------

Ok. I got it .. It shud be in the following format.

for exception in "org.com.Exception JMS MQ Error" "org.db.Exception Db2 SQL Error"

 

---------- Post updated at 03:59 AM ---------- Previous update was at 01:55 AM ----------

Now I am facing another issue where the Awk script is not recognizing the variable passed in for loop i.e. in the below script, it is probably searching for $error string instead of the value of error variable.

How to make awk script recognize the value of the error variable?

 
rm /tmp/jvm1error.txt
echo "Error" >/tmp/jvm1error.txt
for error in "org.com.Exception JMS MQ Error" "org.db.Exception Db2 SQL Error"
do
a=$(awk -vDT=$(date +"%y%m%d%H%M" -d"- 3 hour") '
                     {sub(/^\[/,"")
                      split ($1, D, "/")
                      split ($2, T, ":")
                      AT=sprintf ("%02d%02d%02d%02d%02d", D[3], D[1], D[2], T[1], T[2])}
             AT > DT && /$error/
        ' /logs/websphere/*jvm1*/SystemOut.log)
 
echo "$a" >> /tmp/jvm1error.txt
done

Why don't you use the same mechanism as you do for DT? And, why passing the whole through the "a" variable?

Yeah, a the "a" variable is redundant. I dint get the using the same mechanism part. Are you saying I should store the error value in a user defined awk variable and then use it?

Yes, you can pass the $error variable to awk as you did for DT

awk -vDT=$(date +"%y%m%d%H%M" -d"- 3 hour") -vERROR=$error
1 Like

And, don't use /ERROR/ (which is a regex constant) but $0 ~ ERROR in lieu.

1 Like