0

Other posts here on SO about that error message are all about a missing whitespace or ; but it seems not to be the case for my code. Unfortunately I can't even tell on which line it is because Bitbucket gives me a too high line number. Shellcheck seems to pass for this code, so I am lost here.

Context: $Foo is a pipeline variable with a default, so should always have at least some value. The middle part is just joining of a shell string array: https://stackoverflow.com/a/2317171/4125622

    script:
      - >
        declare -a appsArr
        if [ $Foo == 'deploy' ]; then appsArr+=("foo"); fi
        if [ ${#appsArr[@]} ]; then
          AppsJoined=$(printf ", %s" "${appsArr[@]}")
          AppsFinal=${AppsJoined:1}
        fi
        if [ -z "$AppsFinal" ]; then
          echo "No apps selected, exiting..."
          exit 1
        fi
        echo "Will build and deploy following apps:"
        mkdir tmp && echo "${AppsFinal}" |& tee tmp/bar.txt
Phil
  • 7,065
  • 8
  • 49
  • 91
  • 3
    I'm not familiar with bitbucket and how it preprocesses scripts; what does this code look like when it gets handed to bash for execution? One problem I do see (which shouldn't cause a syntax error) is that `if [ ${#appsArr[@]} ]` will *always* evaluate to true, because the number of elements in the array will never be a null string, even if it's just "0". Also, in `if [ $Foo == 'deploy' ]` you should double-quote `$Foo` and use `=` instead of the nonstandard `==` (or since you're using bash, switch from `[ ]` to `[[ ]]`). – Gordon Davisson Jun 25 '23 at 23:03
  • Are you sure bitbucket uses `bash` to execute commands? I'd expect it to use `sh`, unless it provides a way for you to customize this. – Barmar Jun 25 '23 at 23:38
  • 2
    Copy/paste your code with a first line `#!/bin/bash` into https://shellcheck.net and fix any warnings/errors flagged. Update your question here with the corrected code. Good luck. – shellter Jun 25 '23 at 23:43
  • 2
    Also make sure you don't have CRLF line endings: the shell may be complaining about `then\r` – glenn jackman Jun 26 '23 at 02:44
  • Do a `echo using bash $BASH_VERSINFO` at the start of the program, so that you can verify the bash version you are using. – user1934428 Jun 26 '23 at 06:22
  • run your script in debug mode (bash -x script), that will help to find out a problematic part – yvs2014 Jun 26 '23 at 07:55
  • Lots of bashisms in here which are expected to cause problems if this code is run with `sh` (and the above notes that `sh` is the usual default are spot-on). As it is, though, we really need a [mre] for any bash-tag denizen to solve this -- you can't expect people to install bitbucket for the sake of answering one question. – Charles Duffy Jul 09 '23 at 13:31

0 Answers0