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

前端之家收集整理的这篇文章主要介绍了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.f2er.com/bash/383376.html

猜你在找的Bash相关文章