Is it possible to change the default delimiter xargs?

I need the following behavior without explicitly specifying it using options:

xargs -d '\n' 

Unlike most commands, you cannot simply use an alias because aliases are not recognized in the channels (as a side note, why is it designed this way?). I also tried to create my own ~/bin/xargs script, but I think it is not as simple as reading "$ @" as a string inside a script.

Any suggestions on how to make a separator a newline by default? I don't want to get a bunch of errors when I have a space in the path (and using find ... -print0 | xargs -0 has other undesirable effects).

UPDATE

My shell script is as follows:

 /usr/local/bin/xargs -d '\n' " $@ " 
+8
source share
3 answers

I tested xargs -d '\n' -n1 echo on the command line and worked as advertised; breaking the input into separate lines and echoing it.

You may be bitten by assumptions about escaping the shell during variable assignment. For example, if your shell script looks like this:

CMD = "xargs -d '\ n' -n1 echo"
input cat file | $ CMD

then an error occurs because double quotes around the entire CMD line store both single quotes and backslashes in a delimiter.

If you insert the xargs command in quotation marks, you can change it to the following (without quotation marks)

CMD = "xargs -d \ n -n1 echo"
input cat file | $ CMD

and it will most likely function as needed.

Tested on CentOS 6.4, xargs version 4.4.2

+6
source

I have ~/bin in my PATH and keep a short script with my favorite defaults available for all shells of my system (at different times I use Dash, Bash and Fish). This is tiny:

 !/usr/bin/env sh exec xargs -rd\\n " $@ " 

The script is called x to avoid conflicts with scripts that expect standard xargs to default. If you try to replace xargs with yourself, which I do not recommend, make sure your ~/bin displayed higher in your PATH than in the xargs system. which -a xargs tells me that the only xargs exists in /usr/bin/xargs on my system, so I know that my home directory, /home/stephen/bin , should appear before it like this:

 $ echo "$PATH" /home/stephen/bin:...:/usr/bin:... 

In any case, as a script available for all programs, this means that you can do things like find|x grep and sh -c 'x ...' .

If you use Bash and prefer an alias, you can also just use:

 alias x=xargs -rd\\n\ # \n delim, don't run on empty in, + alias expansion 

Note the trailing space for the alias extension. This allows you to bind aliases. For example, if, in addition to the above alias, I had an alias for grep called g , the following:

 # extended regex, skip binaries, devices, sockets, & dirs, colored, & line # -buffered. use a non-canonical alias instead of GREP_OPTIONS which may wreck # assuming scripts alias g='grep -EID skip -d skip --color=auto --line-buffered' $ find|xg foo 

The x / xargs script approach cannot do this effectively on its own.

Since I switch between shells and use one computer mostly, I save several aliases that I need as separate scripts in ~/bin and agnostic shell aliases in the script helper, ~/.sh_aliases , which is created by ~/.shrc , ~/.bashrc and ~/.config/fish/config.fish as they all support Bash syntax syntax. If I regularly worked on several PCs, I would most likely try to combine them into ~/.bashrc .

+1
source

If your version of xargs does not support the installation of another separator (for example, on the 2011 FreeBSD server), you can use tr to turn your separators into something, xargs will work with xargs . You may need to use several translations before and after if your input has characters that your xargs consider as delimiters (e.g. spaces), or you can just translate your delimiters to NULL / 0x0 and pass the -0 argument to xargs .

Here is an example of using xargs to search for any file in $ PATH, which might be a possible wrapper to use (the reason I am here, then I came up with a different answer):
echo $PATH | tr ':' '\0' | xargs -0 ls | grep sh | sort

0
source

Source: https://habr.com/ru/post/955328/


All Articles