EOL 之前的流程
我有一个小脚本,可以对编译器(maven)输出进行着色,这样错误就不会再被忽视。
#!/bin/sh
export TEXT_YELLOW=`tput setaf 3`
export TEXT_RED=`tput setaf 1`
export RESET_FORMATTING=`tput sgr0`
mvn $* | sed \
-e "s/\(\[WARNING\].*\)/${TEXT_YELLOW}\1${RESET_FORMATTING}/g" \
-e "s/\(\[ERROR\].*\)/${TEXT_RED}\1${RESET_FORMATTING}/g"
问题是我的编译器具有 shell 中的 shell 功能(mvn cli:execute-phase
)。屏幕上应该会出现像 maven2>
这样的命令提示符,然后用户就可以输入命令。不幸的是,由于我的脚本,我只看到一个闪烁的光标。仅当我按 Enter 键时,才会弹出 maven2>
提示前缀文本。
我的猜测是 sed 正在等待 EOL,然后再在屏幕上打印内容。要修复它,我必须查看流是否以字符串 maven2> 开头。如果是,则直接打印到终端,否则将其转发到sed。在bash中可以吗?
I have a small script that colorizes compiler(maven) output so errors won't be overlooked anymore.
#!/bin/sh
export TEXT_YELLOW=`tput setaf 3`
export TEXT_RED=`tput setaf 1`
export RESET_FORMATTING=`tput sgr0`
mvn $* | sed \
-e "s/\(\[WARNING\].*\)/${TEXT_YELLOW}\1${RESET_FORMATTING}/g" \
-e "s/\(\[ERROR\].*\)/${TEXT_RED}\1${RESET_FORMATTING}/g"
Problem is the shell within a shell feature my compiler has (mvn cli:execute-phase
). There a command prompt like maven2>
should be visible on screen and the user is then able to type in commands. Unfortunately due to my script I see nothing but a blinking cursor. Only when I press the enter key the maven2>
prompt-prefix-text pops up.
My guess is that sed is waiting for an EOL before it prints something on screen. To fix it I would have to look if the stream starts with the string maven2>. If it does then print out directly to terminal, otherwise forward it to sed. Is it possible in bash?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我找到了解决方案。我尝试了很多方法,但最终无法纯粹用 bash 解决问题。所以我写了一个 python 脚本,它可以工作。只需将 Maven 输出通过管道传输到脚本,如 mvn cli:execute-phase | colorize.py。也可以在 bashrc 中编写别名,以便所有 Maven 调用都有彩色输出。
Okay, I found a solution. I tried a lot of things but was unable to solve the problem purely with bash in the end. So I wrote a python script instead and it works. Just pipe the maven output to the script like
mvn cli:execute-phase | colorize.py
. It is also possible to write an alias in your bashrc so all maven calls have colorized output.@mainbrain当显示提示“定义属性‘groupId’的值::”时,你的python脚本工作正常,除了“mvn archetype:generate”。
示例:
@mainbrain Your pythons's script works fine except for "mvn archetype:generate" when the prompt "Define value for property 'groupId': : " is showing.
example :