0

I am getting bad substitution error on running the following shell script. (Line numbers written just for reference):

Line 11> SCENARIO_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

Line 12> SCENARIO_PATH="${SCENARIO_DIR}/scenarios"

The error in TeamCity is | ./k6-run-all.sh: line 12: syntax error: bad substitution

Please note that on running this in local, I do not get this error and the scenario path is correctly extracted. But when I run this on TeamCity (which runs on Docker) it is giving me the above error.

Scenario path in my local is: /Users/sonaliagrawal/Documents/antman/src/scenarios/full-card-visa

Scenario path in TeamCity is extracting correctly despite the error which is: //scenarios/full-card-visa

Solution tried: Since in TeamCity, SCENARIO_DIR is itself just / hence I wrote an if then else to handle it, but it didn't help solve the substitution error, it just corrected the path to /scenarios/full-card-visa. The code I had added is as follows-

SCENARIO_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
echo "Scenario directory $SCENARIO_DIR"

SCENARIO_PATH=""
if [[ "$SCENARIO_DIR" = "/" ]]; then
   SCENARIO_PATH="/scenarios"
else
   SCENARIO_PATH="${SCENARIO_DIR}/scenarios"
fi

Reference: In case it helps, Dockerfile is as follows:

FROM loadimpact/k6:0.34.1
COPY ./src/lib /lib
COPY ./src/scenarios /scenarios
COPY ./src/k6-run-all.sh /k6-run-all.sh
WORKDIR /
ENTRYPOINT []
CMD ["sh", "-c", "./k6-run-all.sh"]
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Sonali Agrawal
  • 303
  • 4
  • 14

1 Answers1

-1

It's because your script isn't being executed as a bash script. Put the following on the top of the .sh file.

#!/bin/bash
Andrew Fenn
  • 107
  • 6
  • Why is this downvoted? It's literally the answer to the question. – Andrew Fenn Sep 19 '22 at 06:54
  • No idea who downvoted it. But I tried to add `#!/bin/bash` to the top of my bash file, now it is not running at all. It is giving error as `sh: ./k6-run-all.sh: not found`. Any idea how to fix it? – Sonali Agrawal Sep 20 '22 at 10:50
  • That problem is because you're running a command that doesn't exist in your current working directory. It's not related to the script. – Andrew Fenn Oct 03 '22 at 06:23
  • I fixed the issue by doing 3 steps: 1. installed bash in docker, for doing so after entrypoint, I added the following 2 lines in Dockerfile: USER root RUN apk add --no-cache bash 2. I added the shebang on top of bash file: #!/bin/bash 3. I changed Dockerfile's last line to run bash: CMD ["bash", "-c", "./k6-run-all.sh"] – Sonali Agrawal Oct 03 '22 at 09:31