[英]How can I change a bash script's `echo` output?
My problem consists that I need to change a bash script's output, printed with echo
, using a tool (like sed
or awk
) that works on most of all the GNU/Linux distributions and, if it is possible, on MacOS X too. 我的问题在于,我需要使用在大多数GNU / Linux发行版以及可能的情况下也可以在MacOS X上运行的工具(如sed
或awk
)更改以bash脚本输出的,用echo
打印的内容。
The script is available on the CS50 IDE, it is called apache50
and it's usage output is: 该脚本在CS50 IDE上可用,称为apache50
,其用法输出为:
Usage: apache50 [start VHOST_DIR|restart|reload|stop|status]
The output I want to print is: 我要打印的输出是:
Usage: cli webserver [start VHOST_DIR|restart|reload|stop|status]
To execute apache50
I pass the instruction as argument to a function and then I try to change the output produced: 要执行apache50
我将指令作为参数传递给函数,然后尝试更改产生的输出:
optional_args(){
${@:2} | sed 's/apache50/cli webserver/g'
}
case $opt in:
webserver)
optional_args $opt "apache50" $@
break;
;;
esac
## output:
## Usage: apache50 [start VHOST_DIR|restart|reload|stop|status]
My script is not printing the wanted output. 我的脚本未打印所需的输出。
Here is a part of apache50
: 这是apache50
的一部分:
usage() {
echo "Usage: `basename $0` [start VHOST_DIR|restart|reload|stop|status]" 1>&2
exit 1
}
case "$1" in
*)
usage
;;
esac
If you know how to make it in a better way, please make me a suggestion! 如果您知道如何做得更好,请给我一个建议!
The usage information might be printed to standard error, while sed
processes only standard input (on the other side of the pipe, the command's standard output ). 使用情况信息可能会打印为标准错误,而sed
仅处理标准输入(在管道的另一侧,命令的标准输出 )。
You might redirect standard error to standard output: 您可以将标准错误重定向到标准输出:
${@:2} 2>&1 | sed 's/apache50/cli webserver/g'
But that would conflict with the original intention (showing an error message along with usage info, for instance.) 但这会与最初的意图相冲突(例如,显示一条错误消息以及使用情况信息。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.