Bash 脚本打印命令,但不打印 echo 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33411737/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:50:25  来源:igfitidea点击:

Bash script print commands, but do not print echo command

bashcommand-lineecho

提问by Gerardo Navarro Suarez

I would like to write a bash script the prints the commands. But for readability purposes, I do not want it to print the echo commands. Unfortunately, I cannot find the correct settings for my bash script to achieve this. I need help?

我想编写一个 bash 脚本来打印命令。但出于可读性目的,我不希望它打印 echo 命令。不幸的是,我无法为我的 bash 脚本找到正确的设置来实现这一点。我需要帮助?

#!/bin/bash

# Makes the bash script to print out every command before it is executed
set -v

echo "Cleaning test database"
RAILS_ENV=test bundle exec rake db:drop
echo "************************************************************"
echo ""

echo "Setting up the test database"
RAILS_ENV=test bundle exec rake db:setup
echo "************************************************************"
echo ""

The output looks like:

输出看起来像:

echo "Cleaning test database"
Cleaning test database
RAILS_ENV=test bundle exec rake db:drop
echo "************************************************************"
************************************************************
echo ""


echo "Setting up the test database"
Setting up the test database
RAILS_ENV=test bundle exec rake db:setup

As you can see it prints out all the commands including the echo command, which I do not want to see.

如您所见,它打印出所有命令,包括我不想看到的 echo 命令。

Do you have any ideas?

你有什么想法?

Cheers,

干杯,

Gerardo

杰拉尔多

回答by 123

You could use trap DEBUGinstead of set -vas one option.

您可以使用trap DEBUG代替set -v作为一种选择。

For example

例如

#!/bin/bash


# Makes the bash script to print out every command before it is executed except echo
trap '[[ $BASH_COMMAND != echo* ]] && echo $BASH_COMMAND' DEBUG

echo "Cleaning test database"
RAILS_ENV=test bundle exec rake db:drop
echo "************************************************************"
echo ""

echo "Setting up the test database"
RAILS_ENV=test bundle exec rake db:setup
echo "************************************************************"
echo ""

Debug is executed after every command.
$BASH_COMMAND is currently running command.

调试在每个命令之后执行。
$BASH_COMMAND 当前正在运行命令。

BASH_COMMAND The command currently being executed or about to be executed, unless the shell is executing a command as the result of a trap, in which case it is the command executing at the time of the trap.

BASH_COMMAND 当前正在执行或即将执行的命令,除非 shell 正在执行命令作为陷阱的结果,在这种情况下,它是在陷阱时间执行的命令。

So the trap just checks if the last command did not start with echo and prints it.

所以陷阱只是检查最后一个命令是否没有以 echo 开头并打印它。

回答by Noam Manos

Thanks @123 !

谢谢@123!

To filter multiple strings from the DEBUG trap (not just echo commands), we can use regexp match too.

要从 DEBUG 陷阱中过滤多个字符串(不仅仅是 echo 命令),我们也可以使用正则表达式匹配。

For example, to filter out any command that starts with "echo" or "read" or "if", I use:

例如,要过滤掉以“echo”或“read”或“if”开头的任何命令,我使用:

trap '! [[ "$BASH_COMMAND" =~ ^(echo|read|if) ]] && echo $PS4$BASH_COMMAND' DEBUG

Update:

更新:

In order to show bash ${variable}values expended, I now use:

为了显示花费的bash ${variable}值,我现在使用:

trap '! [[ "$BASH_COMMAND" =~ ^(echo|read|if) ]] && \
cmd=`eval echo "$BASH_COMMAND" 2>/dev/null` && echo "$cmd"' DEBUG