単一の出力を端末に書き込み、他のすべてをファイルに送信します。

単一の出力を端末に書き込み、他のすべてをファイルに送信します。

execスクリプトのすべての出力をファイルに送信するために使用しますexec > file.errout 2>&1。しかし、スクリプトの途中で端末とファイルに複数のメッセージを送信したいと思います。このコードが見つかりました。ここただし、各端末メッセージの両側に2行のコードを追加し、次にパイプする必要がありますtee

exec > file.errout 2>&1
...
echo "messages only directed to the file"
...
exec >/dev/tty
echo "message i want directed to the terminal" | tee file.errout
exec > file.errout 2>&1
...
echo "more messages only directed to the file"
...

これを行うより簡単な方法はありますか?

ベストアンサー1

これを行う必要はありません。以下を使用してすべての標準出力内容をファイルに入れたexectee/dev/tty

exec > file.errout 2>&1

echo "messages only directed to the file"

{
    echo "message i want directed to the terminal and file" ;
    echo "more more message i want directed to the terminal and file" ;
} | tee /dev/tty

echo "more messages only directed to the file"

exec >/dev/tty初期exec行では、すべての標準出力がすでにファイルに書き込まれているため、毎回これを行う必要はありません。

おすすめ記事