Home > Software Deployment > Parsing Tomcat Catalina values with Grep/AWK

Parsing Tomcat Catalina values with Grep/AWK

November 17, 2011 Leave a comment Go to comments

Blog Update

I’ve started a new job at a large travel company based in the Pacific North West.  My job functions have shifted from software deployment on Windows, to release engineering in Linux for tomcat web servers.  It’s a completely different world, but I want to continue blogging, so the theme of this blog will be changing to general release engineer topics in Linux.

Parsing Tomcat configuration settings:

In my new position I’ve inherited some bash scripts of which need a major face lift.  One problem I noticed was that between their servers, the deployment directories differ and they were hard coding these values in the script.
Instead of hard coding the values I thought it would be a good idea to simply parse the tomcat configuration files for these values.  This allows the script to be more flexible and run on all their servers.
First I’m parsing the single instance tomcat startup script:
Tomcat=/etc/init.d/tomcat
This file contains the CATALINA_BASE location which allows me to build the path to server.xml catalina.properties.
Here’s the grep and awk statement I’m using to find CATALINA_BASE:
CATALINA_BASE=`grep 'CATALINA_BASE=' $Tomcat | awk -F"=" '{print $2}'`
serverXMLFile=$CATALINA_BASE/conf/server.xml
catalinaPropertiesFile=$CATALINA_BASE/conf/catalina.properties
Now that I have the config file locations, I parse each of them for the the Tomcat’s appBase folder (where the .war file gets deployed to)
appBase=`grep 'appBase=' $serverXMLFile | awk -F"=" '{print $3}' | tr -d '"'`                               # ex: /app/webapps
Now that these paths are dynamically built in a script, I can re-use this script in each environment regardless of what customized directory structure they are using for Tomcat.
I love the elegance here.  Think about doing this in any other language and most of the time you’d have to open a file handle and parse the file… this is one line of text. Bash with Sed/Awk is some Very powerful stuff!
  1. JP
    December 12, 2011 at 7:45 pm

    Nice to see you’re still blogging Nick. Glad you dig the new job. I’ll keep reading

  1. No trackbacks yet.

Leave a reply to JP Cancel reply