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?
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 GNUxargs
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 …).Here's a quick and dirty
xargs-r
using a temporary file.There is no standard way to determine if the xargs you are running is GNU or not. I set
$gnuargs
to either "true" or "false" and then have a function that replaces xargs and does the right thing.On Linux, FreeBSD and MacOS this script works for me. The POSIX standard for xargs mandates that the command be executed once, even if there are no arguments. FreeBSD and MacOS X violate this rule, thus don't need "-r". GNU finds it annoying, and adds
-r
. This script does the right thing and can be enhanced if you find a version of Unix that does it some other way.You can use
test
or[
:You could make sure that the input always has at least one line. This may not always be possible, but you'd be surprised how many creative ways this can be done.