I try to run bash scripts in Docker, buit I keep getting this error:
./scripts/proto-generator.sh: line 13: syntax error: unexpected "(" (expecting "then")
Here’s my proto-generator.sh file:
function printGreen() {
printf "e[0;32m$1e[0;mn"
}
function printRed() {
printf "e[0;31m$1e[0;mn"
}
service=$1
outDir=./src/services/$service/models
protoDir=./protos/"${service}Service"/*.proto
if ! [[ "$service" =~ ^(file|user)$ ]]; then
printRed "Incorrect service: $service"
exit 1
fi
./node_modules/.bin/proto-loader-gen-types
--longs=String
--enums=String
--defaults
--oneofs
--grpcLib=@grpc/grpc-js
--outDir=$outDir
$protoDir
printGreen "GRPC codes generated: ${outDir}"
How can I fix the syntax error?
Thanks for any help!!
2
Answers
Just put a
at the top of the script
I think you are executing it in /bin/sh,
and sh doesn’t support bash regex.
Or you are using an outdated version of bash, regex in bash where introduced recently.
I would have loved to ask that question as a comment, but I’m not allowed to, yet. Anyway, after having had a quick test, I assume your docker image is Alpine Linux-based. The standard Alpine Linux docker images do not come with a bash as you might be expecting but with an ash and ash does not have a
[[
-built in command, i.e. you should be sticking to the standard[
akatest
command or get goind completely without it. And, sadly enough,test
does not have the ability to handle regular expressions.That said and again assuming that you do not want to bloat the Alpine image with a complete bash,
grep
comes to the rescue. A solution for your case would be (lines 13, ff.)explanation
Make the
if
directly test the return code fromgrep
using your pattern.grep
returns non-zero if there was no match, 0 otherwise.-q
suppresses output of the match,-E
switches to extended regular expression, as needed for the|
.