1

私はこのスクリプトを開発しています:

#!/bin/bash

# If no command line arguments, display a tiny usage message.
if [[ $# -eq 0 ]]; then
  echo "Usage: $0 files..." >&2
  exit 1
fi

# Destroy child processes, when exiting.
cleanup() {
  kill `jobs -p` 2>/dev/null
}

# Call cleanup function on SIGINT, ie. Ctrl-C.
trap "cleanup; exit 0" INT TERM EXIT

# Start child processes continously outputting from the files given as command
# line arguments. The filename is prepended on the line.
for f in $*; do
  tail -f "$f" | awk '{ print "'"$f"':"$0 }' &
done

# Wait for child processes to exit. Just to be sure, kill them all afterwards.
wait
cleanup

私はこのように使用します:

tailfiles *.log

それが行うことは、すべてのファイルからの末尾の出力をインターリーブし、ファイル名を先頭に追加することです (少し似grep -Hています)。ただし、スクリプトの出力をパイプできません。それは単純に私に何も与えません:

tailfiles *.log | grep error

bash で、複数の子プロセスからのストリームを 1 つの出力に収集することは可能ですか?

4

2 に答える 2

0

以下を試してください:

# ...
rm -f fifo
mkfifo fifo
for f in $*; do
  tail -f "$f" | awk '{ print "'"$f"':"$0 }'  > fifo  &
done

tail -f fifo
# ...
于 2013-08-30T15:12:45.620 に答える