Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a script that may in some circumstances have nothing on stdout. At the end of the script, I pass the lines I have to another command using xargs. My problem is, when there is nothing on stdout, I get the typical "Usage: program arg1 arg2" error message.

How can I avoid this?

share|improve this question
8  
Move your "EDIT" in an answer and accept your own answer. This way your discoveries will be useful for other persons. meta.unix.stackexchange.com/questions/809/… –  andcoz Jan 18 '13 at 21:34
1  
Didn't have enough points at the time. Will do it now. –  Richard Jan 19 '13 at 2:01
add comment

2 Answers 2

up vote 7 down vote accepted

You need an -r or --no-run-if-empty options.

Keep in mind that this particular behavior is hard to make cross-platform. BSD versions of xargs run with -r by default. GNU version needs it. FreeBSD version of xargs ignores -r flag for compatibility with GNU. Mac OS X version does not even accept the flag and throws an error illegal option.

You might then choose to use an OS detection based on $OSTYPE to write a cross-platform script. Even better, try to detect the behavior of xargs itself. Run it with -r and if that fails (status code > 0), run it without -r.

share|improve this answer
add comment

A lesson in why you should read the man pages…

The -r or --no-run-if-empty flags are my friend.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.