Ignore empty results for xargs in Mac OS X

前端 未结 4 1994
迷失自我
迷失自我 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:38

    Bash reimplementation of xargs dealing with the -r argument:

    #!/bin/bash
    stdin=$(cat <&0)
    if [[ $1 == "-r" ]] || [[ $1 == "--no-run-if-empty" ]]
    then
        # shift the arguments to get rid of the "-r" that is not valid on OSX
        shift
        # wc -l return some whitespaces, let's get rid of them with tr
        linecount=$(echo $stdin | grep -v "^$" | wc -l | tr -d '[:space:]') 
        if [ "x$linecount" = "x0" ]
        then
          exit 0
        fi
    fi
    
    # grep returns an error code for no matching lines, so only activate error checks from here
    set -e
    set -o pipefail
    echo $stdin | /usr/bin/xargs $@
    

提交回复
热议问题