[英]How to enable or disable multiple “echo statements” in bash ecript
I have bash script where i have echo before every command showing what is happening. 我有bash脚本,在每个命令显示正在发生的事情之前,我都有回声。
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 这样我就不必浪费时间
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, ie: 如果您不使用echo
而是printf
,则采用相同的策略,即:
printf() { :; }
If you absolutely need to actually echo/printf something, prepend the builtin
statement, eg: 如果您绝对需要实际回显/打印某些内容,请在builtin
语句之前添加例如:
builtin echo "This 'echo' will not be suppressed."
This means that you can do a conditional output, eg: 这意味着您可以执行条件输出,例如:
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
变量设置为非null值,被覆盖的echo
函数的行为将类似于正常echo
。
EDIT: another alternative would be to use echo
for instrumenting (debugging), and printf
for the outputs (eg, for piping purposes). 编辑:另一种替代方法是将echo
用于检测 (调试),并将printf
用于输出(例如,出于管道目的)。 That way, no need for any FLAG
. 这样,就不需要任何FLAG
。 Just disable/enable the echo() { :; }
只需禁用/启用echo() { :; }
echo() { :; }
line according to whether you want to instrument or not, respectively. echo() { :; }
根据您是否要进行测量而定。
Put these lines right after the hashbang line: 将这些行放在hashbang行之后:
if [[ debug == "$1" ]]; 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. 现在,调用如下脚本: script.sh debug
将打开检测。 And because there's the shift
command, you can still feed parameters. 而且由于有shift
命令,您仍然可以输入参数。 Eg: 例如:
script.sh param1 param2
没有检测: script.sh param1 param2
script.sh debug param1 param2
使用工具: script.sh debug param1 param2
The above can be simplified to: 上面可以简化为:
if [[ debug != "$1" ]]; then
echo () { :; }
shift
fi
if you need the instrumenting flag (eg to record the output of a command to a temp file only if debugging), use an else
-block: 如果您需要检测标志(例如, 仅在调试时才将命令的输出记录到临时文件中),请使用else
-block:
if [[ debug != "$1" ]]; then
echo () { :; }
shift
else
INSTRUMENTING=yes
fi
REMEMBER: in non-debug mode, all echo
commands are disabled; 记住:在非调试模式下, 所有 echo
命令都被禁用; you have to either use builtin echo
or printf
. 您必须使用builtin echo
或printf
。 I recommend the latter. 我推荐后者。
Several things: 几件事:
Instead use set -xv
to set debug mode which will echo each and every command. 而是使用set -xv
设置调试模式,该模式将回显每个命令。 You can set PS4
to the desired prompt: for example PS4='$LINENO: '
will print out the line number on each line. 您可以将PS4
设置为所需的提示符:例如PS4='$LINENO: '
将在每行上打印PS4='$LINENO: '
号。 In BASH, I believe it's the same. 我相信在BASH中是一样的。 Then, you don't have to clean up your script. 然后,您不必清理脚本。 To shut off, use set +xv
. 要关闭,请使用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
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"
The colon (:) is the nop command in BASH. 冒号(:)是BASH中的nop命令。 It doesn't do anything. 它什么也没做。 Use an environment variable and define it as either echo or : . 使用环境变量并将其定义为echo或: 。 When set to a colon, nothing happens. 设置为冒号时,什么也不会发生。 When set to echo
, the line prints. 设置为echo
,将打印该行。
Example: 例:
echo=":"
$echo "This line won't print"
echo="echo"
$echo "But this line will."
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
?
Not sure if I miss the below solution to use a variable (eg 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
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. 如果按另一个答案中的详细说明设置了crontab,则可以检查您是否在cron中运行,如果不是,则仅打印。 This way you don't need to modify your script at all between different runs. 这样,您无需在不同的运行之间完全修改脚本。
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
Try set -v
at the top to echo each command. 尝试在顶部set -v
以回显每个命令。 To stop echoing change it to set +v
. 要停止回显, set +v
其更改为set +v
。
If you're running it in cron, why not just dump the output? 如果您在cron中运行它,为什么不转储输出呢? Change your crontab entry so that it has > /dev/null
at the end of the command, and all output will be ignored. 更改您的crontab条目,使其在命令末尾具有> /dev/null
,并且所有输出都将被忽略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.