I'm attempting to run a bash script in a Jenkins freestyle job, but getting a strange inclusion of extra quotes that's throwing errors in the script.
I finally found the extra quotes by adding what ought to be unnecessary amounts of debugging, so I'm still unsure if it's actually what's causing the issue or not.
Jenkins script (the label
parameter in the job is either empty or populated from upstream):
#!/usr/bin/env bash
set -ex
if [[ -z $label ]]; then
echo "label not provided"
labelSend=" "
else
echo "label provided"
labelSend="--label \"$label\" "
fi
./label-script.sh --stack-id $stack_id --update $update "$labelSend"
and the label-script
script has a parameter parser like so:
#!/usr/bin/env bash
set -e
print_usage () {
echo
echo "Usage: label-script.sh [OPTIONS]"
echo
echo "Does stuff."
echo
echo " Options:"
echo " --stack-id ID"
echo " --update Use 'true' if this is an update"
echo " --label Optional. Overrides the label"
echo
}
parse_args () {
if [[ $# -eq 0 ]]; then
print_usage
exit 0
fi
while [[ $# -gt 0 ]]
do
key="$1"
case ${key} in
--stack-id) stack_id=$2; shift;;
--update) update=$2; shift;;
--label) label=$2; shift;;
*) print_usage; exit 1;;
esac
shift
done
}
parse_args "$@"
# further script things
Now if I run the Jenkins job, I'm seeing this output:
+ [[ -z testing woo ]]
+ echo 'label provided'
label provided
+ labelSend='--label "testing woo" '
+ ./label-script.sh --stack-id 1 --update false '--label "testing woo" '
and the script prints the help menu rather than continuing with the code.
Note specifically the extra '
around the label parameter when calling the script. I'm thinking this is what's causing my script to fail, as it's not able to parse the given parameter. I have to include label
in some form of quotes because it could be a multi-word string that needs to be appropriately quoted for the script to parse.
I've tried every variation of the labelSend=--label $label
line that I can think of - nested quotes, no quotes, escaped quotes, etc, with no luck.
Has anyone run into something similar? Is there any quoting method that will get me past this?