Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker bash: syntax error: unexpected "(" (expecting "then")

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$1\e[0;m\n"
}

function printRed() {
    printf "\e[0;31m$1\e[0;m\n"
}

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!!

like image 524
retr0327 Avatar asked Oct 24 '25 16:10

retr0327


2 Answers

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 [ aka test 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.)

if ! echo $service | grep -qE "^(file|user)$" ; then
    printRed "Incorrect service: $service"
    exit 1
fi

explanation

Make the if directly test the return code from grep 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 |.

like image 93
Timor Avatar answered Oct 26 '25 07:10

Timor


Just put a

#!/bin/bash

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.

like image 21
Daniele Rugginenti Avatar answered Oct 26 '25 06:10

Daniele Rugginenti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!