加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 使用彩色线条将Bash脚本拖尾

发布时间:2020-12-14 01:23:03 所属栏目:Linux 来源:网络整理
导读:我尝试从 this suggestion创建一个脚本,如下所示: #!/bin/bashif [ $# -eq 0 ]; then tail -f /var/log/mylog.logfiif [ $# -eq 1 ]; then tail -f /var/log/mylog.log | perl -pe 's/.*$1.*/e[1;31m$e[0m/g'fi 当我没有向脚本传递任何参数时,它显示文件
我尝试从 this suggestion创建一个脚本,如下所示:

#!/bin/bash

if [ $# -eq 0 ]; then
        tail -f /var/log/mylog.log
fi


if [ $# -eq 1 ]; then
        tail -f /var/log/mylog.log | perl -pe 's/.*$1.*/e[1;31m$&e[0m/g'
fi

当我没有向脚本传递任何参数时,它显示文件的黑尾,但是当我传递参数时,每一行都是红色的.我希望它只为包含传递给脚本的单词的行着色.

例如,这将包含单词“info”的彩色线条:

./color_lines.sh info

如何更改脚本以使用一个参数?

解决方法

不要引用参数变量:

tail -f input | perl -pe 's/.*'$1'.*/e[1;31m$&e[0m/g'

您也可以使用grep:

tail -f input | grep -e $1 -e ''  --color=always

并使用grep为整行着色:

tail -f input | grep -e ".*$1.*" -e ''  --color=always

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读