bash:捕获 set -v 的输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9828023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash: capturing the output of set -v
提问by sorpigal
How many times have you seen someone trying to "Log the command I run and the output of the command"? It happens often, and for seeingthe command you're running set -vis nice, (set -xis nice, too, but it can be harder to read), but what happens when you want to capture the command being run... but not allcommands being run?
你见过多少次有人试图“记录我运行的命令和命令的输出”?它经常发生,并且看到您正在运行的命令set -v很好,(set -x也很好,但可能更难阅读),但是当您想捕获正在运行的命令时会发生什么......但不是所有命令正在运行?
Running interactively I don't see a way to capture the set -voutput at all.
以交互方式运行我根本看不到捕获set -v输出的方法。
set -v
echo a 1>/dev/null # 'echo a 1>/dev/null' is printed to the screen
echo a 2>/dev/null # 'echo a 2>/dev/null\na' is printed to the screen
I can put this in a script and things get better:
我可以把它放在一个脚本中,事情会变得更好:
echo 'set -v'$'\n''echo a' > setvtest.sh
bash setvtest.sh 1>/dev/null # 'echo a' is printed to the screen
bash setvtest.sh 2>/dev/null # 'a' is printed to the screen
Aha, so from a script it goes to stderr. What about inline?
啊哈,所以从脚本到 stderr。内联呢?
set +v
{ set -v ; echo a ; } 1>/dev/null # no output
set +v
( set -v ; echo a ; ) 1>/dev/null # no output
Hmm, no luck there.
嗯,那里没有运气。
Interestingly, and as a side note, this produces no output:
有趣的是,作为旁注,这不会产生任何输出:
echo 'set -v ; echo a' > setvtest.sh
bash setvtest.sh 1>/dev/null
I'm not sure why, but perhaps that's also why the subshell version returns nothing.
我不确定为什么,但也许这也是 subshell 版本什么都不返回的原因。
What about shell functions?
shell函数呢?
setvtest2 () {
set -v
echo a
}
setvtest2 # 'a'
set +v
setvtest2 1>/dev/null # nothing
set +v
setvtest2 2>/dev/null # nothing
Now the question: Is there a nice way to capture the output of set -v?
现在的问题是set -v:有没有一种很好的方法来捕获 的输出?
Here's my not-nice hack, so I'm looking for something less insane:
这是我不太好的黑客,所以我正在寻找不那么疯狂的东西:
#!/usr/bin/env bash
script=/tmp/$$.script
output=/tmp/$$.out
echo 'set -v'$'\n'"" >"$script"
bash "$script" 1>"$output"
cat "$output"
rm -f "$script" "$output"
Now I can execute simple scripts
现在我可以执行简单的脚本
bash gen.sh 'echo a' 1>/dev/null # prints 'echo a'
bash gen.sh 'echo a' 2>/dev/null # prints 'a'
But surely there are better ways.
但肯定有更好的方法。
采纳答案by Micha? ?rajer
You can run bash with option -vinstead of turning on and off via set:
您可以使用选项运行 bash-v而不是通过set以下方式打开和关闭:
bash -v -c "echo a" 1>/dev/null # prints 'echo a'
bash -v -c "echo a" 2>/dev/null # prints 'a'
The dark side of this solution is that each such line will require to create new bash process, but you will not have to remember to switch off the voption back, since it's switched on only in a child process.
该解决方案的阴暗面是,每个这样的行都需要创建新的 bash 进程,但您不必记住关闭该v选项,因为它仅在子进程中打开。
回答by pizza
how about
怎么样
#!/bin/bash
set -o xtrace
Stuff.....

