tips icon indicating copy to clipboard operation
tips copied to clipboard

Make xargs commands less dangerous

Open sente opened this issue 8 years ago • 4 comments

Make xargs commands less dangerous

sente avatar Dec 30 '16 07:12 sente

I think this is problematic because xargs does not seem to automatically eat the leading whitespace on each line if you specify the delimiter. Perhaps a more comprehensive solution would be to use the -Z parameter with grep, and eat leading whitespaces after each null in the result of that, before finally passing to xargs with -0? Just a thought.

For example, the current PR revision would do this on my end:

$ git branch --merged master | grep -v '^\*' | xargs -d '\n' -n 1 git branch -d
error: branch '  test1' not found.
error: branch '  test2' not found.

echuber2 avatar Dec 30 '16 07:12 echuber2

Reasonable, @echuber2.

grep -Z is on OSX (bsd grep) is shorthand for grep --decompress which is not the gnu grep -Z/--null functionality we're aiming for. I think the simplest solution here would be piping ... | sed 's/^ \+//g' | ... after the grep and before the xargs.

Thoughts?

sente avatar Dec 30 '16 08:12 sente

I've updated the pull request.

sente avatar Dec 30 '16 08:12 sente

This seems to be duplicating the default behavior (pre-request). Perhaps I'm misunderstanding what this is meant to do? (By the way, I tried a variation with grep --null and it seems that it does not work together with -v, for some reason. The result will still be delimited with newlines.)

echuber2 avatar Dec 30 '16 08:12 echuber2