Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore xargs commands if stdin input is empty?

Tags:

bash

xargs

centos

People also ask

How do I quit xargs?

From man xargs : If any invocation of the command exits with a status of 255, xargs will stop immediately without reading any further input.

Can we use xargs and standard input?

xargs is a Unix command which can be used to build and execute commands from standard input.

What xargs 0?

print0 | xargs -0 echo. The -0 tells xargs one thing: "Don't separate input with spaces but with NULL char". It is useful usually in combination with find, when you need handle files and/or directories that contain space in their name. There are more commands what can play with -print0 - for example grep -z .

Why is xargs necessary?

In this brief tutorial, we saw the use of xargs to build and execute commands from standard input. It is used in situations when a command can take input from other commands, only in the form of arguments. It is also particularly useful when we have a list of file paths on STDIN and want to do something with them.


For GNU xargs, you can use the -r or --no-run-if-empty option:

--no-run-if-empty
-r
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.

The BSD version of xargs, which is used on macOS, does not run the command for an empty input, so the -r option is not needed (nor is it accepted by that version of xargs).


Users of non-GNU xargs may take advantage of -L <#lines>, -n <#args>, -i, and -I <string>:

ls /empty_dir/ | xargs -n10 chown root # chown executed every 10 args or fewer
ls /empty_dir/ | xargs -L10 chown root # chown executed every 10 lines or fewer
ls /empty_dir/ | xargs -i cp {} {}.bak # every {} is replaced with the args from one input line
ls /empty_dir/ | xargs -I ARG cp ARG ARG.bak # like -i, with a user-specified placeholder

Keep in mind that xargs splits the line at whitespace but quoting and escaping are available; RTFM for details.

Also, as Doron Behar mentions, this workaround isn't portable so checks may be needed:

$ uname -is
SunOS sun4v
$ xargs -n1 echo blah < /dev/null
$ uname -is
Linux x86_64
$ xargs --version | head -1
xargs (GNU findutils) 4.7.0-git
$ xargs -n1 echo blah < /dev/null
blah

man xargs says --no-run-if-empty.


In terms of xargs, you can use -r as suggested, however it's not supported by BSD xargs.

So as workaround you may pass some extra temporary file, for example:

find /mydir -type f -name "*.txt" -print0 | xargs -0 chown root $(mktemp)

or redirect its stderr into null (2> /dev/null), e.g.

find /mydir -type f -name "*.txt" -print0 | xargs -0 chown root 2> /dev/null || true

Another better approach is to iterate over found files using while loop:

find /mydir -type f -name "*.txt" -print0 | while IFS= read -r -d '' file; do
  chown -v root "$file"
done

See also: Ignore empty results for xargs in Mac OS X


Also please note that your method of changing the permissions isn't great and it's discouraged. Definitely you shouldn't parse output of ls command (see: Why you shouldn't parse the output of ls). Especially when you're running your command by root, because your files can consist special characters which may be interpreted by the shell or imagine the file having a space character around /, then the results can be terrible.

Therefore you should change your approach and use find command instead, e.g.

find /mydir -type f -name "*.txt" -execdir chown root {} ';'

A cross-platform (Mac and Linux) alternative to using -r/--no-run-if-empty xargs' parameter:

Example with empty parameters (same result on Ubuntu 18.04 and Big Sur):

$ echo | xargs -I {}  echo "This is a test with '{}'"
$
$
$ cat /dev/null | xargs -I {}  echo "This is a test with '{}'"
$

Example with multiple lines:

$ seq 1 5  | xargs -I {}  echo "This is a test with '{}'"
This is a test with '1'
This is a test with '2'
This is a test with '3'
This is a test with '4'
This is a test with '5'
$

On OSX: Bash reimplementation of xargs dealing with the -r argument, put that e.g. in $HOME/bin and add it to the PATH:

#!/bin/bash
stdin=$(cat <&0)
if [[ $1 == "-r" ]] || [[ $1 == "--no-run-if-empty" ]]
then
    # shift the arguments to get rid of the "-r" that is not valid on OSX
    shift
    # wc -l return some whitespaces, let's get rid of them with tr
    linecount=$(echo $stdin | grep -v "^$" | wc -l | tr -d '[:space:]') 
    if [ "x$linecount" = "x0" ]
    then
      exit 0
    fi
fi

# grep returns an error code for no matching lines, so only activate error checks from here
set -e
set -o pipefail
echo $stdin | /usr/bin/xargs $@