Regex para correspondência de frase com instrução case em ksh

2

Estou tentando iniciar ou interromper um serviço no AIX no ksh.

Eu estou tentando começar a construir uma prova de erros para verificar coisas como se os serviços já estivessem inativos, etc.

Aqui está o meu 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

Parece que não consigo corresponder às duas frases DBI. Ambas são saídas possíveis de seu script de parada. Eu quero considerar esses sucessos, mas qualquer outra falha possível.

Eu ainda estou aprendendo regex (e suas diferenças em Java vs ksh, etc.), então você terá que desculpar meu rustiness. O que estou fazendo errado? E na sua resposta você poderia por favor explicar porque o meu não funciona e a resposta certa. (Eu sou um proponente do todo, se você ensinar alguém a pescar coisa ...)

EDITAR: Estou pensando que tem algo a ver com os parênteses. Eu tenho outro script que tinha o mesmo problema de enforcamento, mas quando eu removi os parênteses, a função ficou bem suspensa. Então meu truque ainda é como fazer correspondência em frases ....

    
por Chris Aldrich 07.12.2012 / 22:21

2 respostas

0

Bem, eu me afastei das declarações de caso para o egrep, pois isso parecia me ajudar. Eu tive problemas com a passagem de parâmetros para a minha função ... então eu apenas parei de fazer isso. Não é o melhor, mas eu tenho que trabalhar. Alguma outra opinião sobre isso?

#!/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
    
por 14.12.2012 / 00:37
1

Como você fez para "START" e "STOP", você precisa adicionar aspas ao redor da string que está tentando corresponder.

Se o script produzir "DBI. (Coletor não está em execução)", então você deve ter como a primeira instrução de caso:

    case "${RESULT}" in 
        "DBI.(Collector is not running)")
    
por 08.12.2012 / 01:32