用特殊字符分割数组元素

用特殊字符分割数组元素

我想编写一个 bash 脚本(如果脚本是 python 的话会更好),它执行一个Perl脚本(https://github.com/MangeshBiradar/Check_mk/blob/master/check_jenkins_jobs.pl) 。脚本输出的输出如下Perl

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

现在我要在该脚本中添加一项任务,bash/python即解析Perl脚本输出。根据解析输出中的第一个字段(CRITICAL、OK),我希望为 CRITICAL 返回 2,为 OK 返回 0 等返回一个适当的值。我可以通过哪些方式实现此目的?

答案1

稍微改变一下 for 循环:

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

相关内容