How do I work around MacOS X not having xargs -d?
Asked Answered
M

2

8

I have the following command:

xargs -d '\n' -n 8 bash -c 'phpcs_element PSR2 "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8"' -- >&2 2>/dev/null

If I run this command into Linux it will work, if I try to run in into Mac OSX will not because the OSX xargs doesn't know about xargs -d (delimiter).

xargs: illegal option -- d
usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements] [-S replsize]]
             [-J replstr] [-L number] [-n number [-x]] [-P maxprocs]
             [-s size] [utility [argument ...]]

Have anyone found a workaround for this issue?

Xargs Version: src/usr.bin/xargs/strnsubst.c,v 1.7 2004/10/18 15:40:47

Thanks in advice.

Meredith answered 9/3, 2022 at 12:28 Comment(2)
Please paste error messages as text instead of using screenshots. And note that you need a fence of triple backticks to be on its own line for it to behave right.Globate
BTW, are you sure>&2 2>/dev/null is what you want? It first copies the original stderr file descriptor to stdout, and then makes stderr go to /dev/null -- so in the end, stdout still gets written (but to stderr), and stderr doesn't get written at all. If what you really want is neither stdout or stderr to be written, you need to do those same operations in the opposite order: >/dev/null 2>&1.Globate
G
9

Just use -0 instead (making the NUL character the delimiter), and convert your newlines to NULs (which are what you ought to be using to separate items in a list of file names in the first place: the NUL, not the newline, is the only character that cannot exist in a file name).

tr '\n' '\0' |
  xargs -0 -n 8 bash -c 'phpcs_element PSR2 "${@:1:8}"' -- >&2 2>/dev/null
Globate answered 9/3, 2022 at 12:38 Comment(0)
J
7

If you have Homebrew, you can install findutils with this command:

brew install findutils

Then, you will be able you use -d.

You just have to prepend xargs with a g (making it gxargs).

Reference: https://superuser.com/a/467284/529605

Jahdol answered 16/11, 2022 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.