Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent to xargs -r under OsX

Tags:

bash

shell

macos

Are they any equivalent under OSX to the xargs -r under Linux ? I'm trying to find a way to interupt a pipe if there's no data.

For instance imagine you do the following:

touch test
cat test | xargs -r echo "content: "

That doesn't yield any result because xargs interrupts the pipe.

Is there either some hidden xargs option or something else to achieve the same result under OSX?

like image 508
Alex Avatar asked Jan 10 '12 13:01

Alex


People also ask

What is xargs option?

xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is echo) one or more times with any initial-arguments followed by items read from standard input.

What is xargs command in bash?

The xargs command builds and executes commands provided through the standard input. It takes the input and converts it into a command argument for another command. This feature is particularly useful in file management, where xargs is used in combination with rm , cp , mkdir , and other similar commands.

What is xargs grep?

xargs command in UNIX or Linux is a powerful command used in conjunction with find and grep command in UNIX to divide a big list of arguments into small list received from standard input.

Why do we need xargs?

The xargs command is used in a UNIX shell to convert input from standard input into arguments to a command. In other words, through the use of xargs the output of a command is used as the input of another command. We use a pipe ( | ) to pass the output to xargs .


1 Answers

The POSIX standard for xargs mandates that the command be executed once, even if there are no arguments. This is a nuisance, which is why GNU xargs has the -r option. Unfortunately, neither BSD (MacOS X) nor the other mainstream Unix versions (AIX, HP-UX, Solaris) support it.

If it is crucial to you, obtain and install GNU xargs somewhere that your environment will find it, without affecting the system (so don't replace /usr/bin/xargs unless you're a braver man than I am — but /usr/local/bin/xargs might be OK, or $HOME/bin/xargs, or …).

like image 123
Jonathan Leffler Avatar answered Oct 03 '22 23:10

Jonathan Leffler