2

以下を考えると:

text="The quick $color fox jumps over the $adjective dog"

変数を変更せずに、値をbashで色付けしてエコー$textするにはどうすればよいですか?$color$adjective

例:

$ echo -e --highlight-variables $text
The quick \e[00;31mbrown\e[00m fox jumps over the \e[00;31mlazy\e[00m dog

(実際の色を除く)

しかしその後:

$ echo -e $text
The quick brown fox jumps over the lazy dog

色の変化はありません。

ユーザーがコンソールで確認できるようにテキストを出力できるようにしたいのですlessが、変数をファイルに書き込みますが、ファイルには色情報が含まれていてはなりません。私が実行している実際のコードは次のようになります。

echo -e $text | less
echo "Is this acceptable?"
read accept
if [ "$accept" == "y" ]
  then
  echo -e $text > output.txt
fi

変数を色で囲むと、エスケープされたテキストがファイルに入れられます。

4

1 に答える 1

1

印刷する前に ansi シーケンスを削除するだけです。

nocolor()
{
perl -pe "s/\e\[\d+(?>(;\d+)*)m//g;"
}

echo -e $text | less
echo "Is this acceptable?"
read accept
if [ "$accept" == "y" ]
  then
  echo -e $text | nocolor > output.txt
fi
于 2012-06-28T16:38:04.730 に答える