1

I had the zsh script

printf '%s\0' www/scripts6/lib/* | xargs -0 -I{} -P 50 babel {} > {}

and it worked (i think.). But I needed to actually copy the files to the scripts directory instead of scripts6. Also, I needed to add an extra path to the list of paths manually, so I tried doing this:

paths=$(printf '%s\0' www/scripts6/lib/* | sed -e 's@www/scripts6/@@g')
paths=$paths'main.js'$'\0'
echo -n $paths | xargs -0 -I {} -P 50 babel www/scripts6/{} > www/scripts/{}

That was a BIG ACCOMPLISHMENT for me. Anyway, the script makes the file named {} in the scripts directory (and nothing in the scripts/lib directory). This leads me to believe that only the first {} in the code gets replaced by the argument, and not the second one (and I have no explanation for the absence of files in the scripts/lib directory).

How can I make the script work as expected?

1
  • I would like to see an xargs solution to this problem too, if anyone would like to share.
    – mareoraft
    Sep 4, 2015 at 20:35

1 Answer 1

2

Redirection happens before xargs executes. so your first example also creates a file named {}, because xargs never got the chance to replace the last {}.

Also, while you do try to separate the filenames by nulls to treat filenames "correctly", the effort is in vain once you use echo. echo in zsh interprets the C-style escapes(\t, \n, etc) by default. You would need the -E option to disable that.

Try this and see if it does what you want

setopt monitor
for f in www/scripts6/lib/*(e*'REPLY=${REPLY#www/scripts6/}'*) main.js; do;
  while (( $#jobstates >= 51 )); do 
    sleep 1
  done
  echo -E babel $f \> www/scripts/$f &
done

If the output seems ok, remove the echo -E and replace \> with >

2
  • This is a little over my head. I am not familiar with setopt, monitor, or jobstates. Does your script wait for all jobs to complete before moving on? Also, if other jobs were launched before the script, do they become one of the "jobstates" too?
    – mareoraft
    Sep 4, 2015 at 19:41
  • 1
    The loop does not wait until all children exit, you can add wait below the final done to achieve that. If you were to send additional processes into the background before the loop, inside of the script; they would be counted in jobstates.
    – llua
    Sep 4, 2015 at 22:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .