3

たとえば、10 個の BIG ファイルの行数を数えて合計を出力したいとします。

for f in files
do
    #this creates a background process for each file
    wc -l $f | awk '{print $1}' &
done

私は次のようなことを試みていました:

for f in files
do
    #this does not work :/
    n=$( expr $(wc -l $f | awk '{print $1}') + $n ) &
done

echo $n
4

3 に答える 3

3

最終的に、匿名パイプと bash を使用した実用的なソリューションを見つけました。

#!/bin/bash

# this executes a separate shell and opens a new pipe, where the 
# reading endpoint is fd 3 in our shell and the writing endpoint
# stdout of the other process. Note that you don't need the 
# background operator (&) as exec starts a completely independent process.
exec 3< <(./a.sh 2&1)


# ... do other stuff


# write the contents of the pipe to a variable. If the other process
# hasn't already terminated, cat will block.
output=$(cat <&3)
于 2013-08-09T00:04:43.753 に答える
0

それをファイルに書き込むか、データが到着したらすぐに fifo を聞くことができます。

それらがどのように機能するかについての小さな例を次に示します。

# create the fifo
mkfifo test

# listen to it
while true; do if read line <test; then echo $line; fi done

# in another shell 
echo 'hi there'

# notice 'hi there' being printed in the first shell

だからあなたはできる

for f in files
do
    #this creates a background process for each file
    wc -l $f | awk '{print $1}' > fifo &
done

fifo でサイズを聞いてください。

于 2013-08-09T00:13:30.397 に答える