0

I would like to write a bash script (that would be better if the script is in python) which executes a Perl script ( https://github.com/MangeshBiradar/Check_mk/blob/master/check_jenkins_jobs.pl) within it. It is the output that Perl script outputs:

CRITICAL ~ First_run ~ Build stability: 3 out of the last 4 builds failed. ~ 25
CRITICAL ~ Mangesh_Testing ~ Build stability: All recent builds failed. ~ 0
CRITICAL ~ MKS_Integrity ~ Build stability: All recent builds failed. ~ 0
OK ~ MKS_TEST ~ Build stability: No recent builds failed. ~ 100
CRITICAL ~ Rest_api_testing ~  ~ no score
CRITICAL ~ Second_job ~  ~ no score
OK ~ Team_test ~ Build stability: No recent builds failed. ~ 100
OK ~ test ~ Build stability: No recent builds failed. ~ 100
CRITICAL ~ TEST_1 ~ Build stability: 2 out of the last 3 builds failed. ~ 33
OK ~ Update_Outlook ~ Build stability: No recent builds failed. ~ 100

Now I have a task to be added into that bash/python script is to parse the Perl script output. According to the first field(CRITICAL, OK) in the parsed output I would like to return an appropriate value for CRITICAL return 2, for OK return 0 etc. What are some ways I can achieve this?

MangeshBiradar
  • 1,820
  • 4
  • 16
  • 17
  • What is the output of the Perl program? – glenn jackman Jan 15 '13 at 15:05
  • Here is the output, when I double quoted the script to be executed in back ticks. its the output I was expecting. `CRITICAL First_run Build stability: 3 out of the last 4 builds failed. 25 CRITICAL Mangesh_Testing Build stability: All recent builds failed. 0 CRITICAL MKS_Integrity Build stability: All recent builds failed. 0 OK MKS_TEST Build stability: No recent builds failed. 100` – MangeshBiradar Jan 16 '13 at 05:42
  • Add that information to the question so you'll be able to format it properly. – glenn jackman Jan 16 '13 at 12:03
  • Updated the question with expected output. – MangeshBiradar Jan 16 '13 at 13:06
  • Is that the output of the Perl script that you now want to reformat? Or is that what you want to see and you have not shown what Perl outputs? I don't see any `~` characters there. – glenn jackman Jan 16 '13 at 14:33
  • Thanks @glennjackman. I have updated(I mean changed the question..I apologies for that) Hope this will make sense for You. and Hope I will get proper guidance on Thanks once again!! – MangeshBiradar Jan 17 '13 at 05:44
  • Are you looking for a "return code" for each line? – glenn jackman Jan 17 '13 at 11:42
  • Actually I want to split each output line by ~ character and store them into an array, so that I can use that array elements for further use. – MangeshBiradar Jan 17 '13 at 11:53

1 Answers1

2

Changing your for loop a bit:

for line in "${array[@]}"
do
   OIFS=$IFS                   # store old IFS in buffer
   IFS='~'                     # set IFS to '-'
   for i in $line
   do
      echo  $i
   done
  IFS=$OIFS
done
Guru
  • 254
  • 1
  • 2