docker bash:语法错误:意外

发布于 2025-02-08 20:17:08 字数 818 浏览 0 评论 0原文

我尝试在Docker中运行Bash脚本,Buit我一直遇到此错误:

./ scripts/proto-generator.sh:第13行:语法错误:出乎意料的“(“(期望”然后)

这是我的proto-generator.sh文件:

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}"

我如何解决语法错误?

谢谢您的帮助!!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深居我梦 2025-02-15 20:17:08

我本来想把这个问题作为评论,但我还没有允许。无论如何,经过快速测试后,我认为您的Docker Image是基于Alpine Linux的。标准高山Linux Docker映像不会像您期望的那样带有bash,而是 ash 和ash没有[[ - 构建在命令中,即您应该坚持使用标准[ aka test command>命令或获取没有它。而且,可悲的是,测试没有能力处理正则表达式。

也就是说,再次假设您不想用完整的bash膨胀高山图像,grep进行了救援。您的情况的解决方案将是(第13行,ff。)

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

说明

如果直接从grep使用您的模式直接测试返回代码。 GREP如果没有匹配,则返回非零,否则为0。 -Q抑制匹配的输出,-e根据|的需要,切换到扩展正则表达式。

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)
quot; ; 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 |.

趴在窗边数星星i 2025-02-15 20:17:08

只需将一个放在

#!/bin/bash

脚本的顶部,

我认为您正在用 /bin /sh执行它,
SH不支持Bash Regex。
或者,您正在使用最近介绍的Bash的过时版本,在Bash中发出了正则。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文