diff --git a/sequencer.sh b/sequencer.sh index 4b7ad28..5c4d1be 100755 --- a/sequencer.sh +++ b/sequencer.sh @@ -68,7 +68,7 @@ set -o pipefail _sqr_debug=0 _sqr_dry=0 _sqr_editor= - _sqr_errno=0 + _sqr_errno= _sqr_interactive=1 _sqr_single=0 readonly _sqr_stepMax=512 @@ -246,15 +246,7 @@ USAGE_API echo -e "$(col green) echoinfo [...]$(col off)" cat < [DESCRIPTION]$(col off)" @@ -266,12 +258,15 @@ USAGE_API USAGE_API echo -e "$(col green) endReturn [OPTIONS] [MESSAGE]$(col off)" cat < + 2) previously saved by saveReturn + 3) \$? not 0 + and asks to continue if run interactively or ends the sequence. Always exits with evaluated error code. [OPTIONS] - -f : force exit without user input, if error code is not 0 - -o ERRORCODE : override stored error code and check ERRORCODE + -f : force exit without user input, if error code is not 0 + -o : override stored error code and check ERRORCODE [MESSAGE] String which is displayed in the error output @@ -371,7 +366,7 @@ USAGE_API USAGE_API echo -e "$(col green) silent$(col off)" cat <$(col off)" @@ -740,16 +735,17 @@ rmold() { done < <(ls ${hideOpt} -rt1 "${target}" | head -$((total - retain))) } -# saveReturn -# Function returns with in case step wants additional evaluation +# saveReturn [ERRNO] +# Function returns with detected return value in case step wants additional evaluation saveReturn() { - (( ${1:-"0"} )) && _sqr_errno=${1} - return "${_sqr_errno}" + local lerrno=${1:-$?} + (( lerrno )) && _sqr_errno=${lerrno} + return "${_sqr_errno:=${lerrno}}" } # getReturn # Returns latest saved $_sqr_errno -getReturn() { return "${_sqr_errno}"; } +getReturn() { return "${_sqr_errno:-0}"; } # endReturn [-f] [-o ERRORCODE] [MESSAGE] # -f : force exit with $_sqr_errno without user input @@ -757,8 +753,9 @@ getReturn() { return "${_sqr_errno}"; } # [MESSAGE] : Custom error message # endReturn() { + # Use last return code if nothing was saved before + local errorCode=${_sqr_errno:-$?} local forceExit=0 - local errorCode=${_sqr_errno} local endMessage="" for _ in "$@" ; do @@ -766,12 +763,11 @@ endReturn() { -f) forceExit=1 && shift ;; -o) shift - local rex='^[-]*[0-9]+$' # Check if string is a number or alias - if [[ "${1:?}" =~ ${rex} ]] ; then - errorCode="${1:?}" + if [[ "${1:?}" =~ ^[0-9]+$ ]] ; then + errorCode="${1}" else - warning "Ignoring invalid error code: $1" + warning "Ignoring invalid error code: ${1}" fi shift ;; "") break ;; @@ -803,7 +799,7 @@ endReturn() { exit "${errorCode}" else # reset saved error code if user chooses to continue - _sqr_errno=0 + _sqr_errno= sqr::echo warning "Continuing sequence..." fi @@ -1395,7 +1391,8 @@ execute() { exit 1 ;; esac else - "step_$1" "$1" "${seq_args[@]}" + true # reset return value from "if interactive ; then" + "step_${1}" "${1}" "${seq_args[@]}" _seq_stepReturn="$?" fi color none