xargs:用管道替换命令$(...)不起作用 [英] xargs: command substitution $(...) with pipe doesn't work
问题描述
我正在尝试编写简短的脚本以及以下命令:
I'm trying to write short script, and the following command:
echo "aaa111 bbb111" | xargs -I {} echo {} | sed 's/111/222/g'
返回aaa222 bbb222
,这就是我所期望的.
returns aaa222 bbb222
, which is what I expect.
我期望下一个命令:
echo "aaa111 bbb111" | xargs -I {} echo $(echo {} | sed 's/111/222/g')
返回相同,但返回aaa111 bbb111
!为什么会这样?
to return the same, but it returns aaa111 bbb111
! Why is that?
UPD:我要实现的目标:
我有很多文件,例如pic30-coff-gcc
,pic30-coff-ag
等,并且我需要为每个文件建立符号链接,例如pic30-gcc
-> pic30-coff-gcc
等.
I have many files like pic30-coff-gcc
, pic30-coff-ag
, etc, and I need to make a symlink for each file, like pic30-gcc
-> pic30-coff-gcc
, etc.
所以我写了这个:
ls|grep 'coff-'|xargs -I {} ln -s {} $(echo {} | sed 's/coff-//g')
它不起作用:对于每个文件,它报告该文件存在.我检查了这样的命令:
It doesn't work: for each file, it reports that file exists. I checked the command like this:
ls|grep 'coff-'|xargs -I {} echo "ln -s {} $(echo {} | sed 's/coff-//g')"
是的,sed
部分不起作用:
ln -s pic30-coff-gcc pic30-coff-gcc
ln -s pic30-coff-gcc-4.0.3 pic30-coff-gcc-4.0.3
...
但是,如果我只输入
echo "ln -s pic30-coff-gcc $(echo pic30-coff-gcc | sed 's/coff-//g')"
有效:
ln -s pic30-coff-gcc pic30-gcc
然后我已经用aaa111
编写了测试命令,但它也不起作用.还是不明白,为什么.
Then I've written test command with aaa111
, and it doesn't work too. Still can't understand, why.
推荐答案
似乎您所需要的只是一个简单的循环:
It seems that all you need is a simple loop:
for file in *coff*; do
ln -s "${file}" "${file/coff-/}"
done
这篇关于xargs:用管道替换命令$(...)不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!