Skip to main content
deleted 2 characters in body
Source Link
Ed Morton
  • 36.2k
  • 6
  • 25
  • 60

I'd separate identifying the condition that is true given the possible values of the action variable from the functionality to be executed when that condition is true and do the former in awk and the latter in shell, e.g.

actionType=$condition=$(
    action="$action" awk '
        BEGIN {
            lc = tolower(ENVIRON["action"])
            if      ( lc == "" )                         { print "absent" }
            else if ( lc ~ /^-{0,2}h(elp)?$/ )           { print "help" }
            else if ( lc ~ /^(decode|redact|service)$/ ) { print "valid" }
            else                                         { print "invalid" }
        }
    '
)

case $actionType$condition in
    absent  ) echo '... must be specified ...'; exit 1 ;;
    help    ) echo '... the help message ...' ;;
    invalid ) echo '... must be decode ...'; exit 3 ;;
esac

Obviously in this case you could print those messages from inside the awk script but you still need to handle the differing exit statuses and this shows how to handle the general case where there could be other commands to be called based on whatever is being validated by the awk script.

I'd separate identifying the condition that is true given the possible values of the action variable from the functionality to be executed when that condition is true and do the former in awk and the latter in shell, e.g.

actionType=$(
    action="$action" awk '
        BEGIN {
            lc = tolower(ENVIRON["action"])
            if      ( lc == "" )                         { print "absent" }
            else if ( lc ~ /^-{0,2}h(elp)?$/ )           { print "help" }
            else if ( lc ~ /^(decode|redact|service)$/ ) { print "valid" }
            else                                         { print "invalid" }
        }
    '
)

case $actionType in
    absent  ) echo '... must be specified ...'; exit 1 ;;
    help    ) echo '... the help message ...' ;;
    invalid ) echo '... must be decode ...'; exit 3 ;;
esac

Obviously in this case you could print those messages from inside the awk script but you still need to handle the differing exit statuses and this shows how to handle the general case where there could be other commands to be called based on whatever is being validated by the awk script.

I'd separate identifying the condition that is true given the possible values of the action variable from the functionality to be executed when that condition is true and do the former in awk and the latter in shell, e.g.

condition=$(
    action="$action" awk '
        BEGIN {
            lc = tolower(ENVIRON["action"])
            if      ( lc == "" )                         { print "absent" }
            else if ( lc ~ /^-{0,2}h(elp)?$/ )           { print "help" }
            else if ( lc ~ /^(decode|redact|service)$/ ) { print "valid" }
            else                                         { print "invalid" }
        }
    '
)

case $condition in
    absent  ) echo '... must be specified ...'; exit 1 ;;
    help    ) echo '... the help message ...' ;;
    invalid ) echo '... must be decode ...'; exit 3 ;;
esac

Obviously in this case you could print those messages from inside the awk script but you still need to handle the differing exit statuses and this shows how to handle the general case where there could be other commands to be called based on whatever is being validated by the awk script.

added 12 characters in body
Source Link
Ed Morton
  • 36.2k
  • 6
  • 25
  • 60

I'd separate identifying the condition that is true given the possible values of the action variable from the functionality to be executed when that condition is true and do the former in awk and the latter in shell, e.g.

actionType=$(
    action="$action" awk '
        BEGIN {
            lc = tolower(ENVIRON["action"])
            if      ( lc == "" )                         { print "absent" }
            else if ( lc ~ /^-{0,2}h(elp)?$/ )           { print "help" }
            else if ( lc ~ /^(decode|redact|service)$/ ) { print "valid" }
            else                                         { print "invalid" }
        }
    '
)

case $actionType in
    absent  ) echo '... must be specified ...'; exit 1 ;;
    help    ) echo '... the help message ...' ;;
    invalid ) echo '... must be decode ...'; exit 3 ;;
esac

Obviously in this case you could print those messages from inside the awk script but you still need to handle the differing exit statuses and this shows how to handle the general case where there could be other commands to be called based on whatever is being validated by the awk script.

I'd separate identifying the condition that is true given the possible values of the action variable from the functionality to be executed when that condition is true and do the former in awk and the latter in shell, e.g.

actionType=$(
    action="$action" awk '
        BEGIN {
            lc = tolower(ENVIRON["action"])
            if      ( lc == "" )                         { print "absent" }
            else if ( lc ~ /^-{0,2}h(elp)?$/ )           { print "help" }
            else if ( lc ~ /^(decode|redact|service)$/ ) { print "valid" }
            else                                         { print "invalid" }
        }
    '
)

case $actionType in
absent  ) echo '... must be specified ...'; exit 1 ;;
help    ) echo '... the help message ...' ;;
invalid ) echo '... must be decode ...'; exit 3 ;;
esac

Obviously in this case you could print those messages from inside the awk script but you still need to handle the differing exit statuses and this shows how to handle the general case where there could be other commands to be called based on whatever is being validated by the awk script.

I'd separate identifying the condition that is true given the possible values of the action variable from the functionality to be executed when that condition is true and do the former in awk and the latter in shell, e.g.

actionType=$(
    action="$action" awk '
        BEGIN {
            lc = tolower(ENVIRON["action"])
            if      ( lc == "" )                         { print "absent" }
            else if ( lc ~ /^-{0,2}h(elp)?$/ )           { print "help" }
            else if ( lc ~ /^(decode|redact|service)$/ ) { print "valid" }
            else                                         { print "invalid" }
        }
    '
)

case $actionType in
    absent  ) echo '... must be specified ...'; exit 1 ;;
    help    ) echo '... the help message ...' ;;
    invalid ) echo '... must be decode ...'; exit 3 ;;
esac

Obviously in this case you could print those messages from inside the awk script but you still need to handle the differing exit statuses and this shows how to handle the general case where there could be other commands to be called based on whatever is being validated by the awk script.

added 103 characters in body
Source Link
Ed Morton
  • 36.2k
  • 6
  • 25
  • 60

I'd separate identifying the typecondition that is true given the possible values of action requiredthe action variable from the functionality to be executed forwhen that type of actioncondition is true and do the former in awk and the latter in shell, e.g.

actionType=$(
    action="$action" awk '
        BEGIN {
            lc = tolower(ENVIRON["action"])
            if      ( lc == "" )                         { print "absent" }
            else if ( lc ~ /^-{0,2}h(elp)?$/ )           { print "help" }
            else if ( lc ~ /^(decode|redact|service)$/ ) { print "valid" }
            else                                         { print "invalid" }
        }
    '
)

case $actionType in
absent  ) echo '... must be specified ...''; exit 1 ;;
help    ) echo '... the help message ...' ;;
invalid ) echo '... must be decode ...''; exit 3 ;;
esac

Obviously in this case you could print those messages from inside the awk script but you still need to handle the differing exit statuses and this shows how to handle the general case where there could be other commands to be called based on whatever is being validated by the awk script.

I'd separate identifying the type of action required from the functionality to be executed for that type of action, e.g.

actionType=$(
    action="$action" awk '
        BEGIN {
            lc = tolower(ENVIRON["action"])
            if      ( lc == "" )                         { print "absent" }
            else if ( lc ~ /^-{0,2}h(elp)?$/ )           { print "help" }
            else if ( lc ~ /^(decode|redact|service)$/ ) { print "valid" }
            else                                         { print "invalid" }
        }
    '
)

case $actionType in
absent  ) echo '... must be specified ...' ;;
help    ) echo '... the help message ...' ;;
invalid ) echo '... must be decode ...' ;;
esac

Obviously in this case you could print those messages from inside the awk script but this shows how to handle the general case where there could be other commands to be called based on whatever is being validated by the awk script.

I'd separate identifying the condition that is true given the possible values of the action variable from the functionality to be executed when that condition is true and do the former in awk and the latter in shell, e.g.

actionType=$(
    action="$action" awk '
        BEGIN {
            lc = tolower(ENVIRON["action"])
            if      ( lc == "" )                         { print "absent" }
            else if ( lc ~ /^-{0,2}h(elp)?$/ )           { print "help" }
            else if ( lc ~ /^(decode|redact|service)$/ ) { print "valid" }
            else                                         { print "invalid" }
        }
    '
)

case $actionType in
absent  ) echo '... must be specified ...'; exit 1 ;;
help    ) echo '... the help message ...' ;;
invalid ) echo '... must be decode ...'; exit 3 ;;
esac

Obviously in this case you could print those messages from inside the awk script but you still need to handle the differing exit statuses and this shows how to handle the general case where there could be other commands to be called based on whatever is being validated by the awk script.

added 231 characters in body
Source Link
Ed Morton
  • 36.2k
  • 6
  • 25
  • 60
Loading
Source Link
Ed Morton
  • 36.2k
  • 6
  • 25
  • 60
Loading