0

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?

Alex
  • 2,555
  • 6
  • 30
  • 48
  • 1
    Does this answer your question? [Why does shell ignore quotes in arguments passed to it through variables?](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quotes-in-arguments-passed-to-it-through-variables) – oguz ismail Apr 21 '20 at 19:08
  • don't put quotes around --label and it's parameter. that will come across like it's one parameter and not separate ones. But look at what @oguzismail says - if label can have spaces it may not work as you hope – Always Learning Apr 21 '20 at 19:15
  • @oguzismail huh, turns out evaling the line that calls the script does indeed solve the problem... should be safe since user input is scrubbed, although I don't love it as a solution. Not sure if I should delete this question - do you want to whip up a quick answer for rep? – Alex Apr 21 '20 at 19:17
  • No, thanks. Why don't you use arrays btw? Like `params=(param1 'param 2'); prog "${params[@]}"` – oguz ismail Apr 21 '20 at 19:28
  • 1
    @oguzismail super old script - refactoring isn't a priority as we shoehorn in new functionality. Fingers crossed in about six months we'll get to throw this whole thing away in favor of pipeline jobs! – Alex Apr 21 '20 at 19:31

0 Answers0