Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to start or stop a service on AIX in ksh.

I am trying to start to build in fool proofing for checking things like if the services is already down, etc.

Here is my script

#!/bin/ksh


#################################################################################
#                        PARAMETERS & VARIABLES SECTION                         #
#################################################################################

DBI_LOC="DBI/InstallCollector/dbi_collector/bin"

#Put input option to upper case.
OPTION=$( echo "$1" | tr "[:lower:]" "[:upper:]" )

#################################################################################
#                              FUNCTION SECTION                                 #
#################################################################################

parseresults() {
    typeset RESULT="$1"

    if [ "${OPTION}" = "START" ] ; then
        case "${RESULT}" in 
            *)
                echo "Success"
            ;;
        esac
    else
        case "${RESULT}" in 
            DBI.(Collector is not running))
                echo "Collector is already down. Continuing..."
            ;;
            DBI.(performance data collector shutdown COMPLETE))
                echo "Success shutting down collector. Continuing..."
            ;;
            *)
                echo "Error shutting down the collector. Exiting with error."
                exit 1
            ;;
        esac
    fi
}


#################################################################################
#                             MAIN SCRIPT SECTION                               #
#################################################################################



case "${OPTION}" in
    "START")
        echo "Starting the collectors..."
        RESULT=$(ksh ~/${DBI_LOC}/start_dbi_collector.sh)
        ;;
    "STOP")
        echo "Stopping the collectors..."
        RESULT=$(ksh ~/${DBI_LOC}/stop_dbi_collector.sh)
        ;;
    *)
        echo "Error: No Parameter specified. Must be either start or stop"
        exit 1
        ;;
esac

echo ${RESULT}
parseresults ${RESULT}

echo
echo

I can't seem to get it to match on the two DBI phrases. Both of those are possible outputs from their stop script. I want to consider those successes, but anything else a possible failure.

I'm still learning regex (and its differences in Java vs ksh, etc.) so you'll have to excuse my rustiness. What am doing wrong? And in your answer could you please explain why mine doesn't work and the right answer does. (I'm a proponent of the whole if you teach someone to fish thing....)

EDIT: I'm thinking it has something to so with the parenthesis. I have another script which had the same problem of hanging, but when I removed the parens, the function quite hanging. So my trick is still how to do matching on phrases....

share|improve this question

2 Answers 2

up vote 0 down vote accepted

Well, I moved away from case statements toward egrep as that seemed to help me. I had issues with passing parameters into my function...so I just quit doing that. Not the best but I got it to work. Any further thoughts on it?

#!/bin/ksh


#################################################################################
#                        PARAMETERS & VARIABLES SECTION                         #
#################################################################################

DBI_LOC="DBI/InstallCollector/dbi_collector/bin"

#Put input option to upper case.
OPTION=$( echo "$1" | tr "[:lower:]" "[:upper:]" )

#################################################################################
#                              FUNCTION SECTION                                 #
#################################################################################

parseresults() {

    if [ "${OPTION}" = "START" ] ; then
        echo "Success"
        return 0
    else
        echo ${RESULT} | egrep -s ".*DBI.*not\srunning.*"
        if [ $? = 0 ] ; then
            echo "Collector is already down. Continuing..."
            return 0
        fi

        echo ${RESULT} | egrep -s ".*DBI.*COMPLETE.*"
        if [ $? = 0 ] ; then
            echo "Success shutting down collector. Continuing..."
            return 0
        fi

        echo "Error shutting down the collector. Exiting with error."
        return 1
    fi
}

#################################################################################
#                             MAIN SCRIPT SECTION                               #
#################################################################################



case "${OPTION}" in
    "START")
        echo "Starting the collectors..."
        RESULT=$(ksh ~/${DBI_LOC}/start_dbi_collector.sh)
        ;;
    "STOP")
        echo "Stopping the collectors..."
        RESULT=$(ksh ~/${DBI_LOC}/stop_dbi_collector.sh)
        ;;
    *)
        echo "Error: No Parameter specified. Must be either start or stop"
        exit 1
        ;;
esac

echo ${RESULT}
parseresults 

if [ ! $? = 0 ] ; then
    exit 1
fi

echo
echo
share|improve this answer

As you did for "START" and "STOP", you need to add quotes around the string you're trying to match.

If the script outputs "DBI.(Collector is not running)" then you should have as the first case statement:

    case "${RESULT}" in 
        "DBI.(Collector is not running)")
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.