4

この提案から次のようなスクリプトを作成しようとしました:

#!/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

1 つの引数で動作するようにスクリプトを変更するにはどうすればよいですか?

4

1 に答える 1

9

引数変数を引用しないでください。

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
于 2013-06-04T06:41:39.953 に答える