Multithreading shell for loop with find with filenames containing spaces -
I write a shell script to simulate multithreading like this:
#! / Bin / sh PROCESS_NUM = 5 FIFO = / tmp / $$ FIFO Mkfifo $ fifo order 3 & lt; & Gt; $ FIFO for $ i (CEC 1 $ PROCESS_NUM); Echoes $ i & gt; & Amp; & Amp; 3, I read $ ("$ 1" - $ in "-iname" * .jpg ") -u3p {md5sum" $ i "echo $ p & gt; for" start = = start "3 } & Amp; Waiting Executive 3 & gt; & Amp; - Rm $ FIFO echo "= end =" It was fine and was produced like this:
= start = 2f6add89a29b1315166255c41899744b ./img. Jpg ... ... ... = end = But when the file path satisfies the free space, then this error and output was like this:
= start = md5sum: ./my: No such file or directory md5sum: img.jpg: No such file or directory ... ... ... = end = So I change the loop in such a way:
find "$ 1" - "name * * .jpg". I read -o3p {md5sum "$ i" echo $ p & gt; & Amp; 3} & amp; It was ok but will not wait for the background process:
= start = ... ... = end = 2f6add89a29b1315166255c41899744b ./my img .jpg ... How should I solve the problem?
The purpose of job control is not intended for anti-national use - for that matter, the job table It is very limited size and when it overflows it leaves the material.
Collect PID, and wait for them individually.
pids = () while IFS = '' read -r -d '' file name; Do {your_stuff_here; } & Amp; Pids + = ("$?") & Lt; & Lt; For pid in "$ {pids [@]}" ("$ 1" - find 'search' * .jpg '-print0); Wait "$ pid"; Note the use of IFS = '' read -r -d '' and find -print0 - otherwise, you Backslash Escape will be a bad day with sequins or filenames that contain newlines (both, which is, yes, fully valid on the POSIX file system).
Comments
Post a Comment