Ignore empty results for xargs in Mac OS X

前端 未结 4 1992
迷失自我
迷失自我 2021-02-07 10:36

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

cmd = \'cd \' + checkout_dir + \' && \' +

4条回答
  •  后悔当初
    2021-02-07 11:14

    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.

提交回复
热议问题