2

bashスクリプトでは、次のことを行います。

  1. すべてのログをファイルに出力します(loglowlevel.txt)、
  2. ただし、ターミナル(高レベル)に表示されるのはそのうちのいくつかだけにします。
  3. たとえば、20個の高レベルログと60個の低レベルログがあります。したがって、低レベルのログ「redirection-command」を無料に保ち、高レベルのログのみにリダイレクトするものを用意したいと思います。

1を取る

FD3にリダイレクトstdoutして正しく入力される基本的なスクリプトを作成しました。しかし、私は高レベルのログのオプションを指定することに固執しています。sterrloglowlevel.txt

#!/bin/bash - 

# create fd 3
exec 3<> loglowlevel.txt
# redirect stdout and stderr to fd 3
exec 1>&3
exec 2>&3

# high-level logs' redirection below is wrong
echo "high-level comment" 3>&1

# low-level logs should remain redirection-free, as below
echo "low-level comment"
ls notafile

# close fd 3
3>&-

これが何をするかです:

$ redirect.sh
$ cat loglowlevel.txt 
low-level comment
ls: cannot access notafile: No such file or directory

high-level commentターミナルにも印刷される予定でした。

2を取る

2番目のスクリプト、異なる戦略:

#!/bin/bash - 

function echolowlevel() {
    echo $1 &>loglowlevel.txt
}

function echohighlevel() {
    echo $1 |& tee loglowlevel.txt
}

echohighlevel "high-level comment 1"
echolowlevel "low-level comment 1"
echohighlevel "high-level comment 2"
ls notafile

これが何をするかです:

$ redirect.sh 
high-level comment 1
high-level comment 2
ls: cannot access notafile: No such file or directory
$ cat loglowlevel.txt 
high-level comment 2

ここでの2つの問題:

  1. からのエラーメッセージlsはターミナルに出力されますが、私はでのみ必要loglowlevel.txtです。
  2. high-level comment 1で食べられましたloglowlevel.txt

質問

Take1の背後にあるアイデアが好きです。しかし、2つのコマンドhigh-level commentを保持したままstdoutに出力するにはどうすればよいですか?exec

4

2 に答える 2

3
#!/bin/sh

FIFO=/tmp/fifo.$$  # or use tmpfile, or some other mechanism to get unique name
trap 'rm -f $FIFO' 0
mkfifo $FIFO
tee -a loglowlevel.txt < $FIFO &

exec >> loglowlevel.txt
exec 3> $FIFO

echo high-level >&3  # Appears on original stdout and in loglowlevel.txt
echo low-level       # Appears only in loglowlevel.txt
于 2013-02-11T18:10:58.997 に答える
1

プロセス置換をサポートするシェルとオペレーティングシステムに関するWilliamPursellの回答の短いバージョン:

exec 3>> >(tee -a loglowlevel.txt)
exec >> loglowlevel.txt

echo high-level >&3  # Appears on original stdout and in loglowlevel.txt
echo low-level       # Appears only in loglowlevel.txt

この例では、ファイル記述子3に書き込むとtee、ファイル「loglowlevel.txt」に追加されるバックグラウンドプロセスの標準入力に効果的に書き込まれます。

この機能のサポートはさまざまです。これはPOSIX標準の一部ではありませんが、少なくとも、、、およびによって提供さbashkshますzsh。各シェルには、ある程度のオペレーティングシステムのサポートが必要です。bashたとえば、このバージョンでは、名前付きパイプ( mkfifoWilliamのソリューションで作成されたオブジェクト)を使用できるか、を介してファイルを開くことができます/dev/fd

于 2013-02-11T19:29:13.320 に答える