Taking error message from XML file, amending to script

Hello all, I have a question about creating a script that will look for messages on one of our MQ series systems, and fix them.

Currently, if we issue a command for example

Command.sh errors

it gives us:

ID:c3e2d840d4f3f3d74040404040404040cb2ef4e62f70f702 <?xml version="1.0" encoding="UTF-8"?>
<XML><DATE>04/08/2013 16:09:57.000</DATE><LOCATION>/</LOCATION><NAME>report.report1.ZIP</NAME><SERVER>archive</SERVER><SIZE>737</SIZE><USERID>user</USERID><PASSWORD>userpw</PASSWORD><TIMESTAMP>1365451799978</TIMESTAMP><GROUPNAME>agroup</GROUPNAME><SYSTEM>od</SYSTEM></XML>

Manually, we usually type:

Command.sh fix c3e2d840d4f3f3d74040404040404040cb2ef4e62f70f702 

I have never done this type of scripting before. Can someonepoint me in the right direction of how I can manually script it so that I don't have to manually fix the queues each message by message, sometimes there can be hundreds and it can be quite tedious. Thanks!

You could code something like:

#!/bin/bash

Command.sh errors | while read line
do
        if [[ "$line" =~ ^ID: ]]                # Check if line starts with pattern - "ID:"
        then
                id=${line#ID:}                  # Remove string - "ID:" from beginning of line
                id=${id%%<*}                    # Remove string - < followed by all chars - "<*"
                Command.sh fix "$id"            # Run fix script by passing "$id" as argument
        fi
done
1 Like

Thank you for pointing me in the right direction. This is KSH, not sure if it makes a difference but this is the error I am getting.

mqfix.sh[3]: syntax error at line 5 : `=~' unexpected

Heres where I was going and it is not working.

cd /jeff/mq/
var1=`Command.sh fix | awk '{print substr($0,4,48);exit}' '`
echo $var1
if [ -n $var1 ]
then
 echo "There are errors on the queue..fixing"
 for i in `Command.sh fix | awk '{print substr($0,4,48);exit}' '`
 do
 Command.sh fix $1
 done
fi
 

In KSH try this:

#!/bin/ksh

Command.sh errors | while read line
do
        if echo "$line" | egrep -q '^ID:'
        then
                id=${line#ID:}                  # Remove string - "ID:" from beginning of line
                id=${id%%\<*}                   # Remove string - < followed by all chars - "<*"
                Command.sh fix "$id"            # Run fix script by passing "$id" as argument
        fi
done