如何将脚本导入bash并使其输出静音

  • 本文关键字:输出 bash 脚本 导入 bash
  • 更新时间 :
  • 英文 :


我有一个脚本,它使用当前流行的方法安装一些下载bash脚本并将其管道传输到shell的包:

curl --silent https://sdk.cloud.google.com | bash

我想抑制由上述命令生成的对stdout的所有输出。我尝试了显而易见的:

curl --silent https://sdk.cloud.google.com | bash > /dev/null

但是,下载的脚本中的命令生成的输出仍然会返回到终端。这里需要使用一些稍微不同的语法吗?

阅读讨论,我建议进行一定程度的验证,特别是如果这是连续集成系统的一部分。在这种情况下,你最不需要的就是假阳性。我建议使用我编译的脚本来捕获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 %dn' {1..10}
echo >&2 'example output to stderr'
return 42
}

##############################
### using the dot operator ###
if [ "${BASH_VERSINFO}" -lt 4 ]; then
printf '%sn' "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%sn" "SOURCE VERSION : STDOUT & STDERR ONLY"
printf "%sn" "${cmd_out[@]}"
printf "%sn" "${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%sn" "SOURCE VERSION : STDOUT, STDERR & RETURN CODE"
printf '%sn' "${cmd_out[@]}"
# alternative version
# declare -p cmd_out 
printf '%sn' "${cmd_err}"
printf '%sn' "${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%sn" "EVAL VERSION : STDOUT & STDERR ONLY"
printf '%sn' "${cmd_out}"
printf '%sn' "${cmd_err}"
printf '%sn' "${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%sn" "EVAL VERSION : STDOUT, STDERR & RETURN CODE"
printf '%sn' "${cmd_out}"
printf '%sn' "${cmd_err}"
printf '%sn' "${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 succeededn' 
# 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 failedn'
# might should do some logging of the output for further reference
# or fail the integration check
fi
printf 'n%snn' "Example output of STDOUT, STDERR & RETURN CODE"
printf 'STDOUT: %snn' "${cmd_out[@]}"
printf 'STDERR: %snn' "${cmd_err}"
printf 'RETURN CODE: %snn' "${cmd_rtn}"

相关内容

  • 没有找到相关文章

最新更新