Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'

returns aaa222 bbb222, which is what I expect.

I expected the next command:

echo "aaa111 bbb111" | xargs -I {} echo $(echo {} | sed 's/111/222/g')

to return the same, but it returns aaa111 bbb111! Why is that?


UPD: What I'm trying to achieve:

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.

So I wrote this:

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')"

And yep, the sed part doesn't work:

ln -s pic30-coff-gcc pic30-coff-gcc
ln -s pic30-coff-gcc-4.0.3 pic30-coff-gcc-4.0.3
...

But if I just type

echo "ln -s pic30-coff-gcc $(echo pic30-coff-gcc | sed 's/coff-//g')"

it works:

ln -s pic30-coff-gcc pic30-gcc

Then I've written test command with aaa111, and it doesn't work too. Still can't understand, why.

like image 947
Dmitry Frank Avatar asked Mar 23 '14 10:03

Dmitry Frank


People also ask

What does piping to xargs do?

Combine xargs with find The list of files is then piped to xargs , which uses the rm command to delete them. rm now deletes all the files with the . sh extension.

What can I use instead of xargs?

If you can't use xargs because of whitespace issues, use -exec . Loops are just as inefficient as the -exec parameter since they execute once for each and every file, but have the whitespace issues that xargs have.

Why is xargs necessary?

It converts input from standard input into arguments to a command. Some commands such as grep and awk can take input either as command-line arguments or from the standard input. However, others such as cp and echo can only take input as arguments, which is why xargs is necessary.

Is xargs built?

xargs cannot use a builtin, but it can use any external command such as ls or echo (which is also a built-in in many modern shells, but still available as /bin/echo in order for, well, this to work) or printf .


1 Answers

The for loop answer is exactly what I didn't come here to read. The whole purpose of xargs is avoiding this loop. xargs might not be the smartest tool for this particular case, but the question was about it.

Here is the solution I ended up with. It's probably not perfect, but works nevertheless :

echo "aaa111 bbb111" | xargs -I {} sh -c "echo \$(echo {} | sed 's/111/222/g')"
# Outputs aaa222 bbb222

You can come up with different variants of the same command :

echo "aaa111 bbb111" | xargs -I {} sh -c 'echo $(echo {} | sed "s/111/222/g")'
echo "aaa111 bbb111" | xargs -I {} sh -c "echo \`echo {} | sed 's/111/222/g'\`"

The main point here is just to invoke a new shell that will do the command substitution after xargs has replaced the replace-str(here {}) with the right content.

like image 139
Jerska Avatar answered Oct 19 '22 11:10

Jerska