shuttsy
shuttsy

Reputation: 1645

Jenkins adding single quotes to bash shell script

My shell script looks like this:

#!/bin/bash
USER=$1
sudo rm -rf /home/$USER/system/logs/*
exit 0

It's checked into cvs in a shell folder, Jenkins is configured to execute it on a Linux machine via a job with 'Execute Shell' build step:

bash -ex shell/clear-logs.sh myuser

But Jenkins is wrapping the whole sudo line in single quotes which results in my log files not been deleted (although the Jenkins job passes successfully):

[workspace] $ /bin/sh -xe /tmp/hudson7785398405733321556.sh
+ bash -ex shell/clear-logs.sh myuser
+ USER=myuser
+ sudo rm -rf '/home/myuser/system/logs/*'
+ exit 0

Any ideas why Jenkins is doing this? If I call the script from the Jenkins workspace location as the root user, then it works fine.

EDIT:

I have the same shell script, in different cvs modules, being executed by Jenkins on the same linux server. Have created a new job, either as freestyle or by copying an existing job where this works, but makes no difference.

Upvotes: 10

Views: 7108

Answers (3)

javabrett
javabrett

Reputation: 7618

Jenkins is not doing anything with your quotation marks, such as changing double to single - you are seeing the output of set -x. Try this in your shell:

set -x
ls "some string with spaces"

Output will be something like:

+ ls --color=auto 'some string with spaces'

bash is just showing you debug output of its interpretation and tokenization of your command.

Upvotes: 2

Gerold Broser
Gerold Broser

Reputation: 14762

Adapt the permissions of /home/$USER/... I got the following in the Console Output at first:

+ USER=geri
+ rm -rf '/home/geri/so-30802898/*'
rm: cannot remove ‘/home/geri/so-30802898/*’: Permission denied
Build step 'Execute shell' marked build as failure

After adapting the permissions the build/deletion succeeded.

Upvotes: 0

shuttsy
shuttsy

Reputation: 1645

Okay, seemed to have resolved this by adding the 'jenkins' user to the 'myuser' group and restarting the jenkins service. If the logs directory is empty, then Jenkins console output does report the path in single quotes, as no files found. But run the job a second time where there are files, and no single quotes, files correctly deleted.

Upvotes: 3

Related Questions