xargs input involving spaces

て烟熏妆下的殇ゞ 提交于 2020-01-05 10:07:54

问题


I am working on a Mac using OSX and I'm using bash as my shell. I have a script that goes something to the effect of:

VAR1="pass me into parallel please!"
VAR2="oh me too, and there's actually a lot of us, but its best we stay here too"
printf "%s\n" {0..249} | xargs -0 -P 8 -n 1 . ./parallel.sh

I get the error: xargs: .: Permission denied. The purpose is to run a another script in parallel (called parallel.sh) which get's fed the numbers 0-249. Additionally I want to make sure that parallel can see and us VAR1 and VAR2. But when I try to source the script parallel with . ./parallel, xargs doesn't like that. The point of sourcing is because the script has other variables I wish parallel to have access to.

I have read something about using print0 since xargs separates it's inputs by spaces, but I really didn't understand what -print0 does and how to use it. Thanks for any help you guys can offer.


回答1:


If you want the several processes running the script, then they can't be part of the parent process and therefore they can't access the exact same variables. However, if you export your variables, then each process can get a copy of them:

export VAR1="pass me into parallel please!"
export VAR2="oh me too, and there's actually a lot of us, but its best we stay here too"
printf "%s\n" {0..249} | xargs -P 8 -n 1 ./parallel.sh

Now you can just drop the extra dot since you aren't sourcing the parallel.sh script, you are just running it.

Also there is no need to use -0 since your input is just a series of numbers, one on each line.




回答2:


i think you have permission issues , try getting a execute permission for that file "parallel.sh"

command works fine for me :

Kaizen ~/so_test $ printf "%s\n" {0..4} | xargs -0 -P 8 -n 1 echo
0
1
2
3
4

man find :

-print0

True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the -0 option of xargs.

for print0 use : check the link out : there is a question for it in stack overflow

Capturing output of find . -print0 into a bash array




回答3:


The issue of passing arguments is related to xarg's interpretation of white space. From the xargs man page:

-0      Change xargs to expect NUL (``\0'') characters as separators, instead of spaces and newlines.

The issue of environment variables can be solved by using export to make the variables available to subprocesses:

say.sh

echo "$1 $V"

result

bash$ export V=whatevs
bash$ printf "%s\n" {0..3} | xargs -P 8 -n 1 ./say.sh
1 whatevs
2 whatevs
0 whatevs
3 whatevs



回答4:


To avoid the space problem I'd use new line character as separator for xargs with the -d option:

xargs -d '\n' ...


来源:https://stackoverflow.com/questions/16952744/xargs-input-involving-spaces

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!