1

I'm trying to figure out a good way to disable xtrace before leaving a script. These are all being executed by Wercker, a continuous integration and deployment SaaS.

A previous script of mine has run enabling xtrace

+ echo 7ad27e6b-75d9-4e72-a9a7-8b0d6796bd75 0
source "/pipeline/maven-9ea06b71-4392-4fec-ab5a-db7389b49cf2/run.sh" < /dev/null
+ source /pipeline/maven-9ea06b71-4392-4fec-ab5a-db7389b49cf2/run.sh
++ set +o xtrace ## disabling here to keep other area's quiet
...
++ '[' -e settings.xml ']'
++ SETTINGS=--settings=settings.xml
++ mvn --update-snapshots --batch-mode -Dmaven.repo.local=/pipeline/cache --settings=settings.xml deploy
...
+ echo f5b142ac-a369-4166-967e-688d46c642c8 0

here's my actual code

if [ -n "$WERCKER_MAVEN_DEBUG" ]; then
    set -o xtrace
    case "$WERCKER_MAVEN_DEBUG" in
        [1-2]) env;;
        [1-3]) DEBUG="--debug";;
    esac
fi

if [ -e "$WERCKER_MAVEN_SETTINGS" ]; then
   SETTINGS="--settings=${WERCKER_MAVEN_SETTINGS}"
fi

mvn --update-snapshots \
    --batch-mode \
    -Dmaven.repo.local=${WERCKER_CACHE_DIR} \
    ${SETTINGS} ${DEBUG} \
    ${WERCKER_MAVEN_GOALS}

when I try to disable xtrace with set +o xtrace at the bottom of the file that changed the return to always be 0, and so even if maven was failing ci wasn't. I then tried to capture mavens return and call exit ${STATUS} but this caused it to fail even when maven was succeeding with status 0. I think that had something to do with calling exit, not with how I was capturing the code.

How can I disable xtrace after mvn has been executed while preserving mavens return status for the caller script?

1

I refactored the code into functions, the function that matters for this case is disable_xtrace_and_return_status and how it returns the status captured from running maven. Though I've deceived myself into thinking this was working before...

function disable_xtrace_and_return_status() {
    set +o xtrace
    return $1
}

function run() {
    if [ -n "$WERCKER_MAVEN_DEBUG" ]; then
        if [ "$WERCKER_MAVEN_DEBUG" -ge "1" ]; then
            set -o xtrace

            if [ "$WERCKER_MAVEN_DEBUG" -ge "2" ]; then
                env

                if [ "$WERCKER_MAVEN_DEBUG" -ge "3" ]; then
                    local debug="--debug"
                fi
            fi
        fi
    fi

    if [ -e "$WERCKER_MAVEN_SETTINGS" ]; then
       local settings="--settings=${WERCKER_MAVEN_SETTINGS}"
    fi

    mvn --update-snapshots \
        --batch-mode \
        -Dmaven.repo.local=${WERCKER_CACHE_DIR} \
        ${settings} ${debug} \
        ${WERCKER_MAVEN_GOALS}

   disable_xtrace_and_return_status $?
}

run;
  • 1
    Why did you use sh syntax in original script but using bash syntax here? – cuonglm Sep 6 '15 at 3:01
  • oh is something about this bash specific? I didn't check, and I don't really have control over the shell they get run in. :/. Though I did test them locally on zsh without issue... (asside from missing log functions) – xenoterracide Sep 6 '15 at 3:57
  • 1
    function name() is ksh syntax, copied by bash and zsh. It's not in POSIX sh. – cuonglm Sep 6 '15 at 4:04
0

You can wrap your function in a subshell, made the set -o xtrace only affected in subshell environment:

run() (
  if [ -n "$WERCKER_MAVEN_DEBUG" ]; then
      set -o xtrace
      case "$WERCKER_MAVEN_DEBUG" in
          [1-2]) env;;
          [1-3]) DEBUG="--debug";;
      esac
  fi

  if [ -e "$WERCKER_MAVEN_SETTINGS" ]; then
      SETTINGS="--settings=${WERCKER_MAVEN_SETTINGS}"
  fi

  mvn --update-snapshots \
      --batch-mode \
      -Dmaven.repo.local=${WERCKER_CACHE_DIR} \
      ${SETTINGS} ${DEBUG} \
      ${WERCKER_MAVEN_GOALS}
)

run
printf '%s\n' "$?"

Simple test:

$ cat test.sh
#!/bin/sh

run() (
  set -o xtrace
  return 1
)

run
printf '%s\n' "$?"

Run it:

$ ./test.sh
+ return 1
1
  • I now ask myself why they aren't subshelling these things to begin with... – xenoterracide Sep 6 '15 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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