linux - Sending command output to a unique file using xargs in Bash -


i trying run number of processes dynamically on fixed number of processors. want print output unique file each process there problem xargs not using in-place filename create separate file each process.

the bash script calls csh script , below:

$ cat temp | xargs -p 8 % csh '%'.csh >& '%'.log 

where temp text file of csh command names.

my problem xargs takes %.log literally , overwrites file processes write it, rather having seperate .log files desired.

i run script $ bash run.bash &

in general, using string replacement substitute code bad idea -- in case, if had script malicious name, name used run arbitrary commands. (sure, you're executing script, same apply in situations purely dealing data , output file names -- it's best make habit of robust approach regardless).

pass names as parameters script, rather substituting them into script (as xargs doing if fixed usage adding -i or -j parameters:

# best-practice approach: run fixed shell script, passing arguments on # command line. xargs -p 8 -n 1 \   sh -c 'for x; csh "${x}.csh" >"${x}.log" 2>&1; done' _ 

you'll note there's sh -c instance invoked: needed because xargs doesn't understand shell operations such redirections; if want redirection performed, need shell it.


now, let's go little more why original code behaved did:

xargs -p 8 % csh '%'.csh >& '%'.log 

...first performs redirection %.log, then runs command

xargs -p 8 % csh '%'.csh 

there's no opportunity xargs replace %.log string, because redirection performed enclosing shell before xargs command run @ all.


Comments