bash 在 shell 脚本中回显一些命令行(对单个命令回显)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12718801/
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
Echo some command lines in a shell script (echo on for single command)
提问by Den
In shell scripts I would like to echo some of the major (long running) commands for status and debug reason. I know I can enable an echo for all commands with set -xor set -v. But I don't want to see all the commands (specially not the echo commands). Is there a way to turn on the echo for just one command?
在 shell 脚本中,我想为状态和调试原因回显一些主要(长时间运行)命令。我知道我可以使用set -x或为所有命令启用回显set -v。但我不想看到所有的命令(尤其不是 echo 命令)。有没有办法只为一个命令打开回声?
I could do like this, but that's ugly and echoes the line set +xas well:
我可以这样做,但这很丑陋,并且也与这条线相呼应set +x:
#!/bin/sh
dir=/tmp
echo List $dir
set -x
ls $dir
set +x
echo Done!
Is there a better way to do this?
有一个更好的方法吗?
回答by Jonathan Leffler
At the cost of a process per occasion, you can use:
以每个场合的流程为代价,您可以使用:
(set -x; ls $dir)
This runs the command in a sub-shell, so the set -xonly affects what's inside the parentheses. You don't need to code or see the set +x. I use this when I need to do selective tracing.
这会在子 shell 中运行命令,因此set -x只会影响括号内的内容。您无需编码或查看set +x. 当我需要进行选择性跟踪时,我会使用它。
回答by Jo So
How about using this function?
使用这个功能怎么样?
runtraced() {
echo "$@"
"$@"
}
dosomething
runtraced dosomethingelse
回答by Den
Based on Jonathan Leffler answer, this works the same way, just a little more clear because there is noting needed after the command. But you need to specify which shell should be used. This is a example for sh:
根据 Jonathan Leffler 的回答,这以相同的方式工作,只是更清楚一点,因为在命令之后需要注意。但是您需要指定应该使用哪个shell。这是 sh 的示例:
sh -xc ls $dir
回答by mikeserv
An easy way to do this is with a heredocand an uninterpreted string. It is POSIX portable and fast:
一个简单的方法是使用 aheredoc和一个未解释的字符串。它是 POSIX 便携且快速的:
...
% cmd='ls ${dir}'
% sh -x <<_EOF_
> ${cmd}
> _EOF_
...
You can build out entire scripts in this way, parsing and/or modifying them programmatically as needed, saving them to and calling them from shell variables, and running them all from within another script or shell function:
您可以通过这种方式构建整个脚本,根据需要以编程方式解析和/或修改它们,将它们保存到 shell 变量中并从 shell 变量中调用它们,并从另一个脚本或 shell 函数中运行它们:
...
% script="$(cat </some/entire/script.sh)"
% script="$(pipeline | processing | on | ${script})"
% sh -x <<_EOF_ 2>&1 | grep ${specific_cmds_Im_looking_for}
> ${script}
> _EOF_
<desired output>
In my answer to POSIX compliant way to see if a function is defined in an sh scriptI describe the hows and whys of this in greater detail. And at Stack Exchange I discuss pretty thoroughly how the heredoccan be used to solve some annoying quoting problems in answer to Is there a way to get actual(uninterpreted) shell arguments in a function or script?.
在我对POSIX 兼容方式的回答中,以查看是否在 sh 脚本中定义了一个函数,我更详细地描述了这样做的方法和原因。在 Stack Exchange 中,我非常彻底地讨论了如何heredoc使用它来解决一些烦人的引用问题,以回答是否有办法在函数或脚本中获取实际的(未解释的)shell 参数?.
-Mike
-麦克风

