0

I'm trying to run Testim job in Gitlab CI with some hardcoded arguments and some arguments taken from Gitlab CI pipeline's UI. Testim runner requires multiple labels should be passed each in a separate argument: --label "Label1" --label "Label 2"....

TESTIM_DOCKER=testim/docker-cli
TESTIM_TOKEN=some_token
TESTIM_PROJECT=some_project
GRID_NAME=some_grid

# This goes from UI:
TESTIM_LABELS="Tryout,Integration,Edit Entity"

# A dedicated script parses those labels:
LABELS=$(./scripts/misc/parse_testim_labels.sh $TESTIM_LABELS)

# Here what inside LABELS variable
$ echo $LABELS
--label 'tryout' --label 'Integration' --label 'Edit Entity'

Then I'm trying to execute the command below, so the idea is instead echo $LABELS should appear --label 'tryout' --label 'Integration' --label 'Edit Entity' but this does not work:

docker run --rm -v "$(pwd)":/opt/testim-runner $TESTIM_DOCKER \
--token $TESTIM_TOKEN \
--project $TESTIM_PROJECT \
echo $LABELS \
--grid $GRID_NAME \
--base-url $TESTIM_BASE_URL \
--report-file /opt/testim-runner/testim-report.xml

What is a correct way to insert those arguments to that command line?

UPD1: I wrapped echo $LABELS into backticks and executing set -xv docker run... got this:

set -xv docker run --rm -v /Users/miric/code/main:/opt/testim-runner testim/docker-cli --token some_token --project some_project --label \''tryout'\' --label \''Integration'\' --label \''Edit' 'Entity'\' --grid some_grid --base-url --report-file /opt/testim-runner/testim-report.xml
miric
  • 53
  • 10
  • It's not at all ideal to store a _list_ of arguments in a _string_ in the first place. Better practice is to store lists of arguments in arrays (though that does have its own issues -- arrays can't be exported to the environment). – Charles Duffy Sep 23 '21 at 19:15
  • Note that none of your variables are _environment_ variables; they're all just regular shell variables. Calling them "env variables" is incorrect. – Charles Duffy Sep 23 '21 at 19:15
  • 2
    remove `echo` so that `echo $LABELS` becomes `$LABELS` ? if that doesn't work I'd suggest `set -xv` followed by your `docker` command, then add the results/output to the question; may need to create `LABELS` as an array but would want to see the `set -xv` output first – markp-fuso Sep 23 '21 at 19:16
  • 2
    More quotes are certainly called for. http://shellcheck.net/ will flag that automatically. – Charles Duffy Sep 23 '21 at 19:16
  • ...if `$LABELS` contains shell syntax -- quotes, etc -- then you need something to parse that syntax and generate an array of arguments. We have existing Q&A entries describing how to do this (typically with either xargs or the Python `shlex` module). If it _doesn't_ have shell syntax you may be able to get away with just an unquoted expansion (taking out the `echo`), but beware of unwanted glob expansion and other surprises. – Charles Duffy Sep 23 '21 at 19:17
  • ...see [BashFAQ #50](https://mywiki.wooledge.org/BashFAQ/050) describing some of those surprises. – Charles Duffy Sep 23 '21 at 19:18
  • (As another aside: All-caps names are used for variables that are special to the shell or other POSIX tools; your own names should have at least one lower-case character to avoid overwriting a special name by mistake. See the POSIX standard describing this convention at https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html -- read that link keeping in mind that since writing to a shell variable overwrites any like-named environment variable, one can't follow the convention in the presence of unknown environment variables without following it for shell variables). – Charles Duffy Sep 23 '21 at 19:21
  • (...re: the above, an easy way to think about it is to keep in mind that `for PATH in /dir/*/; do` will stop your shell from being able to run any other executables, but `for path in /dir/*/; do` won't break anything at all). – Charles Duffy Sep 23 '21 at 19:22
  • @CharlesDuffy you're right. My bad, no env variables. Just Shell ones. Fixed heading. – miric Sep 23 '21 at 19:25
  • If you took out the backticks you wouldn't need the echo, and your code would be more efficient with both of them removed. (Also slightly better correctness without the echo, though either way you have the bugs described in BashFAQ #50). – Charles Duffy Sep 23 '21 at 19:55
  • @CharlesDuffy Is I leave just $LABELS, I get this: `'--label '\''tryout'\'' --label '\''Integration'\'' --label '\''Edit Entity'\'` – miric Sep 23 '21 at 19:58
  • Probably I complicated my workflow and it might be more simple. Let's assume I have `TESTIM_LABELS` equal to `"tryout,Integration,Service Areas"`. What it better way to put those labels divided by comma (one of them is with a space and contains two words) as multiple `--label` arguments? – miric Sep 23 '21 at 20:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237436/discussion-between-miric-and-charles-duffy). – miric Sep 23 '21 at 20:10
  • @CharlesDuffy "If it doesn't have shell syntax you may be able to get away with just an unquoted expansion [...], but beware of unwanted glob expansion and other surprises" - well, a surprise in the provided example would already be in the form of the space and quote metacharacters, neither of which are interpreted by the executed subprocess. To interpret it as shell syntax, a new shell needs to be started, otherwise, only post-variable expansion syntax is interpreted, which doesn't include quoting. – Larry Sep 23 '21 at 21:05
  • @Larry, when that comment was added we didn't yet know the OP's string contained quotes. I _did_ say in an earlier comment that quotes would be a problem. – Charles Duffy Sep 23 '21 at 21:26
  • @miric, the _easy_ way to do this is to put your arguments in an array, not a string. See BashFAQ #50, as linked previously. – Charles Duffy Sep 23 '21 at 21:29
  • @Larry, and for that matter you quoted the qualifier yourself! "*if it doesn't have shell syntax*" -- obviously, quotes are syntax. – Charles Duffy Sep 23 '21 at 21:36

0 Answers0