Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore empty results for xargs in Mac OS X

The code for my website uses this piece of code for automatic deployment on the server (Ubuntu).

cmd = 'cd ' + checkout_dir + ' && ' + svn_command + " st | awk '{print $2}' | grep -v ^deploy | tac | xargs -r" + svn_command + " revert -R && " + svn_command + ' up -r ' + options.revision

What this command does is it cd into checkout directory, runs svn status, and then extracts the filename ($2), removes the deploy directory and all its files from the list (I don't want to revert it). If there are no argument it does not run the svn revert command, else it does.

Unfortunately the xargs -r does not work on my machine (Mac OS X 10.8). So I am stuck here, can anyone help?

like image 536
Anubhav Agarwal Avatar asked Jul 01 '13 10:07

Anubhav Agarwal


2 Answers

Indeed, the BSD implementation of xargs doesn't have the -r flag (--no-run-if-empty). The GNU version in Linux has it.

Here's one way to work around the issue in a way that works in both Linux and BSD:

... | (grep -v ^deploy || echo :) | xargs svn revert

The grep ... || echo : in the middle will generate a line with a : in it in case the output of grep is empty. It's a bit dirty, because xargs will still run the command svn revert :. If your repository doesn't contain the file : then this should have no effect, so it can be acceptable. The : could be anything else, as long as there is no such file in your repository.

Finally, as @tripleee pointed out, the grep ... || echo : must be enclosed within (...), because:

the || has higher precedence than |, and thus terminates the (first) pipeline.

Your code looks like a Python string. It will be more readable this way:

kwargs = {
  'svn': svn_command,
  'dir': checkout_dir,
  'revno': options.revision,
}
cmd = "cd {dir} && {svn} st | awk -v r=1 '$2 ! ~ /deploy/ {{ print $2; r=0 }} END {{ r || print \":\" }}' | xargs {svn} revert && {svn} up -r {revno}".format(**kwargs)

I made some changes to your original:

  • Moved the logic of the grep inside awk, as @tripleee suggested. Notice that since the grep hack is not needed anymore, there is also no more need to wrap within (...)
  • Dropped the tac, as I don't see the point in it
  • Dropped the -R from svn revert, because I don't think you need it
like image 168
janos Avatar answered Oct 22 '22 17:10

janos


Not pretty, but hopefully a workaround.

cmd = 'cd ' + checkout_dir + ' && ' + 
    'r=$(' svn_command + ' st | ' +
        "awk '$2 !~ /^deploy/{print $2}' | tac) && " +
    'test "$r" && ' +
    svn_command + ' revert -R $r && ' +
    svn_command + ' up -r ' + options.revision

I'm not convinced that the tac is necessary or useful. I refactored the first grep into the Awk script for efficiency and aesthetic reasons.

To solve the general "my xargs lacks -r" problem, the gist of the solution is to convert

stuff | xargs -r cmd

into

var=$(stuff)
test "$var" && cmd $var

The unquoted $var will only work if it doesn't contain file names with spaces or other surprises; but then bare-bones xargs without the GNU extensions suffers from the same problem.

like image 3
tripleee Avatar answered Oct 22 '22 17:10

tripleee