微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我想写一个打印命令的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 ""

输出如下:

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

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

你有什么想法?

干杯,

赫拉尔

您可以使用trap DEBUG而不是set -v作为一个选项.

例如

#!/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 ""

每次命令后都会执行调试.
$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.

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

原文地址:https://www.jb51.cc/bash/383376.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐