spr*_* cc 8 bash shell popen xargs echo
我试图使用xargs
传递参数echo
:
[usr@linux scripts]$ echo {0..4} | xargs -n 1 echo
0
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
被-n 1
保险人认为xargs
通行证1
的时间到了echo
.
然后我想两次使用这个武器,但结果不是我想要的:
[usr@linux scripts]$ echo {0..4} | xargs -I@ -n 1 echo @,@
0 1 2 3 4,0 1 2 3 4
Run Code Online (Sandbox Code Playgroud)
-n 1
当我添加时,似乎禁用了-I@
,这是我想要的结果:
0,0
1,1
2,2
3,3
4,4
Run Code Online (Sandbox Code Playgroud)
我怎么能实现这一目标?
--------供应------------------我使用了@ 123推荐的方法,但还有另外一个问题:
test.sh:
#!/bin/bash
a[0]=1
a[1]=2
echo "a[0] and a[1] : "${a[0]}, ${a[1]}
echo -n {0..1} | xargs -I num -d" " echo num,${a[num]},num
Run Code Online (Sandbox Code Playgroud)
这是输出:
[usr@linux scripts]$ sh test.sh
a[0] and a[1] : 1, 2
0,1,0
1,1,1
Run Code Online (Sandbox Code Playgroud)
你可以看到数组a
没有返回我想要的值:<我如何解决这个问题?
如果无法更改输入格式,可以将分隔符设置为空格:
$ echo -n {0..4} | xargs -d " " -I@ echo @,@
0,0
1,1
2,2
3,3
4,4
Run Code Online (Sandbox Code Playgroud)
否则,更改输入以使用换行符分隔标记:
$ printf "%s\n" {0..4} | xargs -I@ echo @,@
0,0
1,1
2,2
3,3
4,4
Run Code Online (Sandbox Code Playgroud)
这种语法的原因在中解释 man xargs
-I replace-str
Replace occurrences of replace-str in the initial-arguments with names read from
standard input. Also, unquoted blanks do not terminate input items; instead the sep?
arator is the newline character. Implies -x and -L 1.
Run Code Online (Sandbox Code Playgroud)
因此,如果要分隔字段,则必须手动将分隔符设置为空格.