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.

Have I got enough (or perhaps too many) ;; terminators in this bash script containing nested case's?

case "$1" in
OK)
    # Nothing needs done
    echo -n "OK:1" ;;

CRITICAL)

    case "$2" in
    SOFT)
        case "$3" in
        1|2) 
            soft_reset_pool.sh ;;
        esac ;;

    HARD)
        case "$3" in
        1|2)
            hard_reset_pool.sh ;;

        3|4)
            hard_reset_service ;;

        esac ;;
    esac ;;      
esac

It's been a few years since I did any bash in anger.

share|improve this question
add comment

1 Answer

up vote 12 down vote accepted

It's easier to keep track if you use sensible indentation on the ;;s.

case "$1" in
OK)
    # Nothing needs done
    echo -n "OK:1"
    ;;

CRITICAL)

    case "$2" in
    SOFT)
        case "$3" in
        1|2) 
            soft_reset_pool.sh
            ;;
        esac
        ;;

    HARD)
        case "$3" in
        1|2)
            hard_reset_pool.sh
            ;;

        3|4)
            hard_reset_service
            ;;

        esac
        ;;
    esac
    ;;
esac

They match up fine, every case label) having a terminating ;;

That said, sometimes it's easier and/or clearer to collapse everything into a non-nested case:

case "$1:$2:$3" in

OK:*)
    # Nothing needs done
    echo -n "OK:1"
    ;;

CRITICAL:SOFT:[12])
    soft_reset_pool.sh
    ;;

CRITICAL:HARD:[12])
    hard_reset_pool.sh
    ;;

CRITICAL:HARD:[34])
    hard_reset_service
    ;;

esac
share|improve this answer
add comment

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.