0

I need a bash script that takes the output of a shell command and parses that output to pull out the state. If the State is: Bad, an email should be sent.

~$ bucardo status
PID of Bucardo MCP: 2074
 Name     State       Last good               Time           Last I/D    Last bad                Time         
============================================+========+=======================+==============+===========+=======================+==============
 zenith | Good   | Jun 28, 2020 08:37:45 | 31h 13m 14s  | 1/1       | Jun 22, 2020 12:28:34 | 171h 22m 25s 
 zenith | Good   | Jun 23, 2020 19:12:42 | 140h 38m 17s | 4/4       | none                  |  

I have not been able to get the Regex right

#!/usr/bin/env bash

bucardo status

while read line; do
  if [[ ! ${line} =~ ^[\+\| ]]; then
    if [[ ${line} =~ \|[[:space:]]*([[:alpha:]]+)[[:space:]]*\ ]]; then
      state="${BASH_REMATCH[1]}"
      echo "${state}"
    fi
  fi
done
ruscom
  • 1
  • 1

1 Answers1

0
if [[ ${line} =~ \|[[:space:]]*([[:alpha:]]+)[[:space:]]*\ ]]; then

should be

if [[ ${line} =~ \|[[:space:]]*([[:alpha:]]+)[[:space:]]* ]]; then

(Remove the extraneous \). Works for me now.

Hint: if you use Vim the syntax highlighting makes this jump right out at you!

Chris Rees
  • 165
  • 7