Hi,
I'm working on a shell script that reports service status on a database server.
There are some services that are in disabled status that the script should ignore and only check the services that are in Enabled status.
I output the service configuration to a file and use that information to loop through the services. Unfortunately im not able to exclude the service that is in Disabled status.
Here's the content of the file -
Service name: Bot_Serv1
Service is disabled
Preferred instances: botqa2,botqa3
Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3
Each service configuration is separated by a blank line. As you can see the first service is in Disabled status and the 2nd service is Enabled .
How do i get the content of the file, excluding the configuration details for the Disabled service. Im looking to grep the file and output should be the following -
Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3
$ ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution : Debian 8.6 (jessie)
bash GNU bash 4.3.30
-----
Input data file data1:
Service name: Bot_Serv1
Service is disabled
Preferred instances: botqa2,botqa3
Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3
-----
Expected output:
Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3
-----
Results:
Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3
-----
Verify results if possible:
-----
Comparison of 4 created lines with 4 lines of desired results:
Succeeded -- files (computed) f1 and (standard) expected-output.txt have same content.
The heart of this solution is:
cgrep -D -1 +w='' "enabled"
search for enabled, copy one line before, and all lines after until an empty line occurs.
bash-3.2$ cat t.txt
Service name: Bot_Serv1
Service is disabled
Preferred instances: botqa2,botqa3
Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3
bash-3.2$ grep -C1 "Service is enabled" t.txt
Service name: Bot_Serv2
Service is enabled
Preferred instances: botqa2,botqa3