Linux 如何在 bash ecript 中启用或禁用多个“echo 语句”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5972491/
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
How to enable or disable multiple "echo statements" in bash ecript
提问by Mahakaal
I have bash script where i have echo before every command showing what is happening.
我有 bash 脚本,在每个命令显示正在发生的事情之前,我都有 echo 。
But i need to disbale echo when setting as cron job and then enable again if do some testing.
但是我需要在设置为 cron 作业时禁用 echo,然后在进行一些测试时再次启用。
i find it very hard to go to each line and then add/remove comment
我发现很难转到每一行然后添加/删除评论
is there anything which i can include at top something like
有什么我可以在顶部包含的东西吗
enable echo or disable echo
so that i don't have to waste time
这样我就不用浪费时间了
采纳答案by pepoluan
The absolute easiest would be to insert the following line after the hashbang line:
最简单的方法是在 hashbang 行之后插入以下行:
echo() { :; }
When you want to re-enable, either delete the line or comment it out:
当您想重新启用时,请删除该行或将其注释掉:
#echo() { :; }
If you're not using echo
but printf
, same strategy, i.e.:
如果你没有使用echo
but printf
,同样的策略,即:
printf() { :; }
If you absolutelyneed to actuallyecho/printf something, prepend the builtin
statement, e.g.:
如果您绝对需要实际echo/printf 某些内容,请在builtin
语句前加上,例如:
builtin echo "This 'echo' will not be suppressed."
This means that you can do a conditional output, e.g.:
这意味着您可以进行条件输出,例如:
echo () {
[[ "$SOME_KIND_OF_FLAG" ]] && builtin echo $@
}
Set the SOME_KIND_OF_FLAG
variable to something non-null, and the overridden echo
function will behave like normal echo
.
将SOME_KIND_OF_FLAG
变量设置为非空值,被覆盖的echo
函数将表现得像 normal echo
。
EDIT:another alternative would be to use echo
for instrumenting(debugging), and printf
for the outputs (e.g., for piping purposes). That way, no need for any FLAG
. Just disable/enable the echo() { :; }
line according to whether you want to instrument or not, respectively.
编辑:另一种选择是echo
用于仪表(调试)和printf
输出(例如,用于管道目的)。这样,就不需要任何FLAG
. 只需echo() { :; }
根据您是否想要检测分别禁用/启用该线路。
Enable/Disable via CLI Parameter
通过 CLI 参数启用/禁用
Put these lines right after the hashbang line:
将这些行放在 hashbang 行之后:
if [[ debug == "" ]]; then
INSTRUMENTING=yes # any non-null will do
shift
fi
echo () {
[[ "$INSTRUMENTING" ]] && builtin echo $@
}
Now, invoking the script like this: script.sh debug
will turn on instrumenting. And because there's the shift
command, you can still feed parameters. E.g.:
现在,像这样调用脚本:script.sh debug
将打开检测。而且因为有shift
命令,您仍然可以提供参数。例如:
- Without instrumenting:
script.sh param1 param2
- With instrumenting:
script.sh debug param1 param2
- 不带仪器:
script.sh param1 param2
- 带仪表:
script.sh debug param1 param2
The above canbe simplified to:
以上可以简化为:
if [[ debug != "" ]]; then
echo () { :; }
shift
fi
if you needthe instrumenting flag (e.g. to record the output of a command to a temp file onlyif debugging), use an else
-block:
如果您需要检测标志(例如,仅在调试时将命令的输出记录到临时文件),请使用else
-block:
if [[ debug != "" ]]; then
echo () { :; }
shift
else
INSTRUMENTING=yes
fi
REMEMBER:in non-debug mode, allecho
commands are disabled; you have to either use builtin echo
or printf
. I recommend the latter.
记住:在非调试模式下,所有echo
命令都被禁用;您必须使用builtin echo
或printf
。我推荐后者。
回答by Matthew Scharley
You can do one better. If you setup your crontab as detailed in another answer, you can then check if you are running in cron and only print if you are not. This way you don't need to modify your script at all between different runs.
你可以做得更好。如果您按照另一个答案中的详细说明设置了 crontab ,则可以检查您是否在 cron 中运行,如果不是,则仅打印。这样你就不需要在不同的运行之间修改你的脚本。
You should then be able to use something like this (probably doesn't quite work, I'm not proficient in bash
):
然后你应该能够使用这样的东西(可能不太好用,我不精通bash
):
if [ ! "$CRON" ]; then
echo "Blah blah"
fi
回答by grok12
Try set -v
at the top to echo each command. To stop echoing change it to set +v
.
尝试set -v
在顶部回显每个命令。要停止回显,请将其更改为set +v
.
回答by QuantumMechanic
Building on Matthew's answer, how about something like this:
基于马修的回答,这样的事情怎么样:
myEcho = "/bin/true"
if [ ! "$CRON" ]: then
myEcho = "/bin/echo"
fi
and then use $myEcho
instead of echo
in your script?
然后在你的脚本中使用$myEcho
而不是echo
?
回答by Daenyth
If you're running it in cron, why not just dump the output? Change your crontab entry so that it has > /dev/null
at the end of the command, and all output will be ignored.
如果您在 cron 中运行它,为什么不直接转储输出?更改您的 crontab 条目,使其> /dev/null
位于命令末尾,并且所有输出都将被忽略。
回答by David W.
Several things:
几件事:
Don't use echo at all
根本不要使用 echo
Instead use set -xv
to set debug mode which will echo each and every command. You can set PS4
to the desired prompt: for example PS4='$LINENO: '
will print out the line number on each line. In BASH, I believe it's the same. Then, you don't have to clean up your script. To shut off, use set +xv
.
而是用于set -xv
设置调试模式,该模式将回显每个命令。您可以设置PS4
为所需的提示:例如PS4='$LINENO: '
将打印出每行的行号。在 BASH 中,我相信它是一样的。然后,您不必清理脚本。要关闭,请使用set +xv
。
Example:
例子:
foo=7
bar=7
PS4='$LINENO: '
set -xv #Begin debugging
if [ $foo = $bar ]
then
echo "foo certainly does equal bar"
fi
set +xv #Debugging is off
if [ $bar = $foo ]
then
echo "And bar also equals foo"
fi
Results:
结果:
$ myprog.sh
if [ $foo = $bar ]
then
echo "foo certainly does equal bar"
fi
5: [ 7 = 7 ]
7: echo 'foo certainly does equal bar'
foo certainly does equal bar
set +xv #Debugging is off
And bar also equals foo
Use a function
使用函数
Define a function instead of using echo:
定义一个函数而不是使用 echo:
Example:
例子:
function myecho {
if [ ! -z "$DEBUG" ]
then
echo "$*"
fi
}
DEBUG="TRUE"
my echo "Will print out this line"
unset DEBUG
myecho "But won't print out this line"
Use the nopcommand
使用nop命令
The colon (:) is the nop command in BASH. It doesn't do anything. Use an environment variable and define it as either echoor :. When set to a colon, nothing happens. When set to echo
, the line prints.
冒号 (:) 是 BASH 中的 nop 命令。它什么也不做。使用环境变量并将其定义为echo或:。当设置为冒号时,没有任何反应。设置为 时echo
,将打印该行。
Example:
例子:
echo=":"
$echo "This line won't print"
echo="echo"
$echo "But this line will."
回答by Nasri Najib
Not sure if I miss the below solution to use a variable (e.g. debug) at the start of the bash script.
不确定我是否错过了在 bash 脚本开始时使用变量(例如调试)的以下解决方案。
Once you set the debug=true, any conditional-if will enable or disable multiple “echo statements” in bash script.
设置 debug=true 后,任何条件 if 都将启用或禁用 bash 脚本中的多个“echo 语句”。
typeset debug=false # set to true if need to debug
...
if [ $debug == "true" ]; then
echo
echo "Filter"
read
fi
...
if [ $debug == "true" ]; then
echo
echo "to run awk"
fi