14 Replies Latest reply on Oct 8, 2012 12:27 PM by Lazar NameToUpdate

    BLCLI command failure

      Hey Everyone,


      This may seem trivial but I am not sure how it works.


      I am writing a BLCLI script and I want to be able to exit the script if a blcli_execute command fails and log the error. If the command succeeds I want to just continue on with the script.


      What's the easiest way to do this? an if/then statement?

        • 1. Re: BLCLI command failure
          Bill Robinson

          i created a function like:





                  local varName="${1}"

          if [ -n ${BLCLICMD[@]} ]


            print_debug "blcli_execute ${BLCLICMD[@]}"

            blcli_execute "${BLCLICMD[@]}" > ${BLCLIOUT} 2> ${BLCLIERR}



                   if [ $RETCODE -eq 0 ]


                                  # set the command result to the variable we passed in

                                  if [ "${varName}x" != "x" ]


                                          blcli_storeenv ${varName} > ${NULL}


                                  return $RETCODE


                                  # there was some kind of error, print that and exit

                                  RESULT=`cat ${BLCLIERR} | cut -f2- -d:`

                                  print_error "${RESULT}"



            print_error "No BLCLI command supplied!"

            return 999




          then i do:

          BLCLICMD=(Server findByName serverName)



          to call it.  obviously i have some other function calls in there for logging and such.

          1 of 1 people found this helpful
          • 2. Re: BLCLI command failure
            Scott Dunbar

            And if you are not super human like bill, this works


            fatal() {

              echo "$*"

              exit 1



            debug() {

              echo "DEBUG : $*"



            blcli_execute RBACRole getDBKeyByName ${ROLE}_ADMIN 2>&1 >/dev/null

            if [ $? -ne 0 ]; then

                 fatal "${ROLE}_ADMIN already exists, adding the ADgroups to this role...."


                 debug "Some debug message"


            • 3. Re: BLCLI command failure
              Bill Robinson

              I wrapped that all in a function because I make a lot of blcli calls and got tired of copy-pasting the same block ☺

              • 4. Re: BLCLI command failure
                Lazar NameToUpdate

                May as well share my variation of this inspired by Bill's (only included variables that are used by the function, my template script has more)


                # if Debug set to 1, every blcli command and the output are printed, including the name of storeenv variable

                # if ExitOnCliError set to 1, then the script aborts if any blcli command fails




                echoFtl() { echoMsg "FATAL: $@"; exit 1; }
                echoErr() { echoMsg "ERROR: $@"; }
                echoInf() { echoMsg " INFO: $@"; }
                echoDbg() { [ $Debug -ge 1 ] && echoMsg "DEBUG: $@"; return 0; }


                blcliRun() {
                   echoDbg "blcli_execute $BlcliCmd[@]"
                   if ! blcli_execute "$BlcliCmd[@]" > $DEVNULL; then
                      echoErr "failed command | $BlcliCmd[@]"
                      [ $ExitOnCliError -eq 1 ] && echoFtl "abort on blcli error"
                      if [ -n "$1" ]; then
                         blcli_storeenv BlEnv
                         echoDbg "storeenv $1: $BlEnv"
                         eval $1="\"$BlEnv"\"

                BlcliCmd=(Server listAllServers)

                blcliRun ServersList


                # usage: BlcliCmd=(namespace command arg1 arg2)

                #            blcliRun [var] - "var" is optional, and only needed if you want to store the output of blcli.

                1 of 1 people found this helpful
                • 5. Re: BLCLI command failure

                  Thanks everyone!


                  Lazar - What's your echoMsg function?

                  • 6. Re: BLCLI command failure
                    Lazar NameToUpdate

                    oh sorry, in that case you will need two more variables:





                    echoMsg() { [ $Logger -eq 1 ] && echo "`date '+%D %T'`| $@" >> "$Log"; echo "$@"; }



                    if you set Logger to 1, then everything also gets logged to a file with prefixed time stamp, but if Logger is set to 0, then it's just an echo command.

                    • 7. Re: BLCLI command failure

                      Hey Bill,


                      I am trying this





                      blcli_execute "${BLCLICMD[@]}" > ${BLCLIOUT} 2> ${BLCLIERR}


                      With a command that I know will fail.


                      It fails and I get RETCODE=1 but there is nothing written to my logs.


                      Any idea why this would happen?

                      • 8. Re: BLCLI command failure
                        Lazar NameToUpdate

                        If you are on pre-8.2 and Windows, then blcli routing to a file does not work, and files remain empty.

                        • 9. Re: BLCLI command failure

                          Oh, perfect.


                          Ill keep working at this.



                          • 10. Re: BLCLI command failure
                            Lazar NameToUpdate

                            Here is how I redirect all errors (blcli and other syntax issues that you may get). I have the following line after all the functions, before I start the actual main body of the script:



                            exec 2> >(while read StdErr; do echoErr "$StdErr"; done;)



                            'exec 2' catches all the stderr, and it redirects it to echoErr() function, and you already know what echoErr() does. There is one problem with this approach, which I have not fixed yet.... sometimes when the output information is too much, due to some race condition some entires may get logged in incorrect order. It's rare, but happens. I plan to revisit it when I get a chance.

                            • 11. Re: BLCLI command failure

                              Something that might help in a situation like this is some ZSH functionality that NSH inherits.  If you create a function called TRAPZERR, it will run after any command has an error.  I use this quite a bit, especially on scripts that have a lot of actions that are dependent on the previous action being successful and it is nice for some quick debugging.




                              function TRAPZERR ()


                                   echo "There has been an error!"

                                   exit 1



                              It can get a little time consuming, but if you really want helpful output or have a great need for detailed debugging, you can use variables like ERROR_MESSAGE and ERROR_CODE that you set before each command that can contain helpful text that is printed if the command fails. 




                              function TRAPZERR ()


                                   echo "${ERROR_MESSAGE}"

                                   echo ${ERROR_MESSAGE} >> errorfile.txt

                                   exit ${ERROR_CODE}



                              ERROR_MESSAGE="Command one failed"



                              command1  #If this failed, TRAPZERR will run and print the defined ERROR_MESSAGE and exit with the ERROR_CODE


                              ERROR_MESSAGE="Command two failed"



                              command 2

                              • 12. Re: BLCLI command failure
                                Bill Robinson

                                TRAPZERROR will automatically after any error'd command?  is it doing that based on a non-zero exit from the command ?

                                • 13. Re: BLCLI command failure

                                  In my experience, yes, and per the ZSH doc, it is based on the commands exit code (although the ZSH doc mentions a minor exception)



                                  Executed whenever a command has a non-zero exit status.  However, the function is not executed if the command occurred in a sublist followed by ‘&&’ or ‘||’; only the final command in a sublist of this type causes the trap to be executed.  The function TRAPERR acts the same as TRAPZERR on systems where there is no SIGERR (this is the usual case).



                                  The other trap functions mentioned here could be pretty useful as well, but I don't have any experience with them.


                                  This might no be techinically accurate, but I think of it as like having:


                                  if test $? -ne 0





                                  after every command.

                                  • 14. Re: BLCLI command failure
                                    Lazar NameToUpdate

                                    Gave a little test run the trap functions and seems neat. So far haven't found a quick way to replace the 'exec 2' method that I'm using, but maybe just need more time with it...


                                    Anyway Brandon, share your golden template that has a whistle for every bell, when you get a chance : )