tim
tim

Reputation: 407

Using 'xargs' in GIT for Windows bash

I am trying to automate my work with some nice command line scripts. I have one script (jenkins_cli.sh) that communicates with a Jenkins Server and which looks like this:

cd $1

if [ -z ${JENKINS_CREDENTIALS+x} -o -z ${JENKINS_SERVER+x} ]
  then
    JENKINS_SERVER=$(<jenkins_url.txt)
    JENKINS_CREDENTIALS=$(<credentials.txt)
fi

java -jar jenkins-cli.jar -s $JENKINS_SERVER -auth $JENKINS_CREDENTIALS ${@:2}

This works fine.

For example if I call ./jenkins_cli.sh localhost/ list-jobs I get a list of Jobs currently running on my local Jenkins Server. However, if I try to do something more fancy (e.g.: ./jenkins_cli.sh localhost/ list-jobs | xargs -L1 ./jenkins-cli localhost/ delete-job I get the following error: 'RROR: No such job 'test_job. The job test_job does exist. The job 'test_job does not exist. How does this character ' end up in my command?

I am guessing this has something to do with the fact that I am using the bash that shipped with GIT for Windows or how it deals with newlines or how I am handling parameters in my script/etc.. If I execute ./jenkins_cli.sh localhost/ list-jobs | xargs -L1 echo I get the expected result without any quotes.. I really don't know how to debug this any further. I have a solution for this problem where I just write the job-list to a file and then read each line and pass it to my custom script, but I really want to implement it with xargs :)

Upvotes: 0

Views: 3299

Answers (1)

rodrigo
rodrigo

Reputation: 98486

I think that the problem is that the output from your jenkins commands uses the DOS end-of-line style (\r\n) of output, but the git shell commands expect the classic UNIX end-of-line style (\n).

The thing is that xargs sees the stray \r as special characters at the end of the lines and try to escape them. But then, even if all goes well, the argument to the next command will not be test_job but test_job\r (the \r may be invisible).

The easy solution is to use dos2unix:

java -jar jenkins-cli.jar -s $JENKINS_SERVER -auth $JENKINS_CREDENTIALS ${@:2} | dos2unix

Upvotes: 3

Related Questions