【发布时间】:2019-08-14 18:41:32
【问题描述】:
我有一个 bash 脚本,它运行许多其他命令(例如 docker)。我希望脚本能够将所有输出捕获到一个变量中,然后在最后回显一个自定义返回。
例子:
#!/usr/bin/env bash
set -euo pipefail
# Capture into this (PSEUDO CODE)
declare CapturedOutput
$(Capture Output > CapturedOutput)
# Run some commands like...
docker-compose ... up -d
# Stop capturing (PSEUDO CODE)
$(Stop Capture Output > CapturedOutput)
echo "something"
因此,如果有人像 ./runit.sh 这样调用我的脚本并且 docker 命令有输出,他们不会看到它,而只会看到:
> ./runit.sh
something
【问题讨论】:
-
@LéaGris 这不是重复的。那是从脚本外部捕获输出。我想在里面捕捉它
-
它只是你想在变量中捕获的docker的输出吗?
-
你真的要使用
CapturedOutput吗?将命令组重定向到/dev/null可能就足够了。 -
@DavidC.Rankin 不,我想重定向我在脚本中运行的所有命令的输出