閱讀討論,我會推薦一些驗證水平,尤其是如果這是持續集成系統的一部分。在這種情況下,你最後需要的是一個誤報。我建議使用我編譯的腳本,以便捕獲stderr,stdout和返回代碼,以便測試正確完成或記錄輸出以進一步診斷。我在下面包括它供您參考:
# #!/bin/bash
#
# based on posts from stackexchange:
# http://stackoverflow.com/a/26827443/171475
# http://stackoverflow.com/a/18086548/171475
# http://stackoverflow.com/a/28796214/171475
function example_function {
printf 'example output to stdout %d\n' {1..10}
echo >&2 'example output to stderr'
return 42
}
##############################
### using the dot operator ###
if [ "${BASH_VERSINFO}" -lt 4 ]; then
printf '%s\n' "The source version of this script requires Bash v4 or higher."
else
# stdout & stderr only
source <({ cmd_err=$({ mapfile -t cmd_out < <(example_function); } 2>&1; declare -p cmd_out >&2); declare -p cmd_err; } 2>&1)
printf "\n%s\n" "SOURCE VERSION : STDOUT & STDERR ONLY"
printf "%s\n" "${cmd_out[@]}"
printf "%s\n" "${cmd_err}"
unset cmd_out
unset cmd_err
# stdout & stderr only as well as return code:
source <({ cmd_err=$({ mapfile -t cmd_out< <(\
example_function \
; cmd_rtn=$?; declare -p cmd_rtn >&3); } 3>&2 2>&1; declare -p cmd_out >&2); declare -p cmd_err; } 2>&1)
printf "\n%s\n" "SOURCE VERSION : STDOUT, STDERR & RETURN CODE"
printf '%s\n' "${cmd_out[@]}"
# alternative version
# declare -p cmd_out
printf '%s\n' "${cmd_err}"
printf '%s\n' "${cmd_rtn}"
unset cmd_out
unset cmd_err
unset cmd_rtn
fi
##############################
######### using exeC#########
# stdout & stderr only
eval "$({ cmd_err=$({ cmd_out=$(\
example_function \
); } 2>&1; declare -p cmd_out >&2); declare -p cmd_err; } 2>&1)"
printf "\n%s\n" "EVAL VERSION : STDOUT & STDERR ONLY"
printf '%s\n' "${cmd_out}"
printf '%s\n' "${cmd_err}"
printf '%s\n' "${cmd_rtn}"
unset cmd_out
unset cmd_err
# stdout & stderr only as well as return code:
eval "$({ cmd_err=$({ cmd_out=$(\
example_function \
); cmd_rtn=$?; } 2>&1; declare -p cmd_out cmd_rtn >&2); declare -p cmd_err; } 2>&1)"
printf "\n%s\n" "EVAL VERSION : STDOUT, STDERR & RETURN CODE"
printf '%s\n' "${cmd_out}"
printf '%s\n' "${cmd_err}"
printf '%s\n' "${cmd_rtn}"
unset cmd_out
unset cmd_err
unset cmd_rtn
適用於您的特定情況下,它可能看起來像:
source <({ cmd_err=$({ mapfile -t cmd_out< <(\
curl --silent https://sdk.cloud.google.com | bash \
; cmd_rtn=$?; declare -p cmd_rtn >&3); } 3>&2 2>&1; declare -p cmd_out >&2); declare -p cmd_err; } 2>&1)
# return code 0 indicates the command executed fully
if [ "${cmd_rtn}" -eq "0" ]; then
# the command succeeded
printf 'The command succeeded\n'
# might should do some additional checking of the contents of
# cmd_out or cmd_err for expected results
elsethen
# the command failed, do some checking
printf 'The command failed\n'
# might should do some logging of the output for further reference
# or fail the integration check
fi
printf '\n%s\n\n' "Example output of STDOUT, STDERR & RETURN CODE"
printf 'STDOUT: %s\n\n' "${cmd_out[@]}"
printf 'STDERR: %s\n\n' "${cmd_err}"
printf 'RETURN CODE: %s\n\n' "${cmd_rtn}"
'>'只是重定向標準輸出。你也想重定向stderr,所以你想說'&>/dev/null'或'>/dev/null 2>&1'。 – fedorqui
我實際上想保留'stderr',這樣我就可以看到任何錯誤。輸出到'stdout'雖然相當健談,所以我想抑制這一點。使用這種方法,我仍然可以看到所有典型的輸出到'stdout'。 –
我不確定抑制標準輸出是否是個好主意。如果下載腳本要求輸入,會怎麼樣? –