我有一个脚本,它使用当前流行的方法安装一些下载bash
脚本并将其传输到shell中的软件包:
curl --silent https://sdk.cloud.google.com | bash
我想将上述命令生成的所有输出抑制为stdout
。我尝试了显而易见的事实:
curl --silent https://sdk.cloud.google.com | bash > /dev/null
但是,下载脚本中的命令生成的输出仍然会回显给终端。我需要在这里使用稍微不同的语法吗?
答案 0 :(得分:0)
阅读讨论,我建议进行一定程度的验证,特别是如果这是持续集成系统的一部分。在这种情况下你需要的最后一个是误报。我建议使用我编译的脚本来捕获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}"