Flow control based on grep match pattern or string

R

raajesh

Guest
Hi,

Looking to create a flow control based on a string or pattern matched in a log file.

The log file may contain either of these

1 CONFSUCCESS
2 CONFFAIL
3 CONFPARTIAL

Example:
cat System.log

dsfasdfasfas
adfsfasfa
safafaf sdfaasfsa CONFSUCCESS
adsfasdfadsfadsf fafasfasdfasdf fafasfafsa
sadfafasfasdfadss fdsafadff fasfadfaf asff

So idea is to grep the message from log, based on pattern run a flow control as below.

if

1 Look in System.log - CONFSUCCESS - show message as - SUCCESSFUL - Ask user to - Press ENTER to continue

if

2 Look in System.log - CONFFAIL - show message as - FAILURE - Exit (come out of shell)

if

3 Look in System.log - CONFPARTIAL - Show message as - PARTIALSUCCESS - Exit (come out of shell)

Thanks
 


Aloha and welcome to Linux.org! Feel free to ask any other questions about Linux here on this site. The main page displays the newest articles/tutorials. If you see a post that you like or found helpful, click "Like". If you ask a question and someone answers it to your satisfaction, click "Best Answer". Enjoy the site!

I can write a Python3 script that would do this more efficiently than a shell script.


Code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
def cat(openfile): #Emulates the shell's cat command#
  with open(openfile) as file:
    lines = file.readlines()
    return ''.join(lines)
try:
  LOG = cat('System.log')
except:
  print('System.log not found or failed to open!')
  exit()
if re.search('CONFSUCCESS', LOG):
  print('SUCCESSFUL')
  VOIDVAR = input('Press ENTER to continue')
elif re.search('CONFFAIL', LOG):
  print('FAILURE')
  exit()
elif re.search('CONFPARTIAL', LOG):
  print('PARTIALSUCCESS')
  exit()
else: #If none of the three messages are found
  print('No message found or some unknown error occured')
  exit()
 

Members online


Top