简体   繁体   English

如何更改bash脚本的`echo`输出?

[英]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上运行的工具(如sedawk )更改以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.

相关问题 在bash脚本中,如何判断脚本输出是否重定向到文件? - In a bash script, how can I tell if the script output is redirected to a file? 如何强制 echo 的输出颜色遵循 bash 脚本中定义的颜色? - How to force the output color of echo to follow colors defined in bash script? 初学者bash:如何在字符串比较测试中使用echo的输出? - Beginner bash: How can I use the output of echo in a string comparison test? 为什么我不能在bash脚本中添加带有“ echo >>”的行? - Why I can't add lines with “echo >>” from bash script? 如何使用bash脚本更改iptable条目? - How can I change iptable entries using bash script? 如何在打印 bash 脚本的 arguments 时保留引号 - How can I preserve quotes in printing a bash script's arguments 我如何在bash中将此echo语句传递给我的函数? - How can I pass in this echo statement into my function in bash? 将命令输出回显到终端的 shell 脚本 - A shell script that echo’s the commands output to the terminal 如何在bash脚本中将echo结果分配给变量 - How to assign the result of echo to a variable in bash script 在BASH中,我需要根据perl脚本的输出来更改一些环境变量。 在tcsh中,我可以使用别名eval组合。 不能狂欢 - In BASH I need to change some environment variables based on output of a perl script. In tcsh I can use an alias eval combo. Can't in bash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM