Michael Martinez
Michael Martinez

Reputation: 2853

Sharing a writable variable between bash scripts

Using bash on Linux.

I'm running find piped into xargs which launches a second bash script to do some processing on each file. I would like to maintain a "running tally" of file sizes and how many errors are encountered by the second script. In other words, every time the second script runs, it calculates the file size and adds it to the total so far, and also same thing if it encounters an error in its processing of the file. And I need this information to be available to the parent script after the find | xargs finishes.

I can do this by having the second script save and update a text file — a crude way to maintain a "global variable" — but I'm wondering if there's a nicer, more efficient way.

Upvotes: 0

Views: 89

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754520

Can you use a pipe or Process Substitution to get the information back from the second script?

find ... | xargs second_script |
while read information
do
    something useful with it
done

Or:

while read information
do
    something useful with it
done < <(find ... | xargs second_script)

Upvotes: 1

Related Questions