The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,466 other followers

-r argument to pipe (no argument for MacOS)- If no input is given to xargs, don’t let xargs run the utility – Unix & Linux Stack Exchange

Posted by jpluimers on 2021/07/28

TL;DR

There is a non-standard -r option to xargs that allows it to skip executing when there are no arguments at all.

On some operating systems, the -r is default.

MacOS has no -r, but does not execute xargs if there are no arguments given.

shell-tools to experiment with shell commands

Via the below answer:

  • [WayBack] shell-toolbox/INSTALL at develop · kusalananda/shell-toolbox · GitHub
  • at [WayBack] GitHub – kusalananda/shell-toolbox: Useful shell scripts for /bin/sh
    * shell:
    
        Creates a shell for testing things in.
    
        This script is useful for testing things in an interactive
        environment other than your usual shell, or for testing things in a
        clean environment with automatic cleanup of any files left behind.
    
        Creates a "temporary interactive shell" with a temporary working
        directory (unless the "-d" flag is used to specify an existing
        directory) and clean environment.  The working directory is removed
        when the shell exits (unless the "-k" or "-d" flag was used).
    
        The temporary working directory may also be pre-populated with the
        contents of an existing directory (using the "-s" flag).
    
        See "man 1 shell" after installation.
    

Long, maybe read

[WayBack] pipe – If no input is given to xargs, don’t let xargs run the utility – Unix & Linux Stack Exchange (thanks [WayBack] Kusalananda):

You are looking for the -r option. This is a non-standard option which is often implemented (check your xargs manual).

From the GNU xargs manual:

-r--no-run-if-empty

If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension.

From the manual on an OpenBSD system:

-r

Do not run the command if there are no arguments. Normally the command is executed at least once even if there are no arguments.

On FreeBSD and macOS, this is the default behaviour. xargs on AIX and Solaris does not have a -r option, and it is unknown what the default behaviour is (I don’t have access to an AIX or Solaris system).


One work-around on systems with no -r flag for xargs (and where running the utility at east once is the default behaviour) is to use

something | xargs sh -c '[ "$#" -gt 0 ] && utility "$@"' sh

(or something like it; note that the trailing sh is required) where utility is the utility that you’d like to run with xargs, and where something produces some arguments.

The short shell script [ "$#" -gt 0 ] && utility "$@" tests whether it was given any arguments, and if so, it runs the utility with those arguments.

On macOS, -r is not an accepted option, but it is the default behaviour, as on FreeBSD.

–jeroen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: