Domanda

I am counting the no. of lines in a file as a background process (I need parallel execution)

counting="wc -l < abc.xyz" &
`$counting`
counting_process_id=$!
wait $counting_process_id
echo $counting

This just returns a blank for $counting

When I do,

counting="wc -l < abc.xyz"
`$counting`
echo $counting

i.e count in the foreground. It returns the proper value.

So I guess this has to do with child variable not accessible in parent shell ? (and some answers suggest IPC) Are ther other ways to get around this.

È stato utile?

Soluzione

So I guess this has to do with child variable not accessible in parent shell ?

Absolutely correct.

One way would be to make use of a temporary file and read from it:

outfile=$(mktemp)
( wc -l < abc.xyz ) > "${outfile}" &
wait
echo $(<$outfile)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top