docker bash:语法错误:意外
我尝试在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我本来想把这个问题作为评论,但我还没有允许。无论如何,经过快速测试后,我认为您的Docker Image是基于Alpine Linux的。标准高山Linux Docker映像不会像您期望的那样带有bash,而是 ash 和ash没有
[[
- 构建在命令中,即您应该坚持使用标准[
akatest
command>命令或获取没有它。而且,可悲的是,测试
没有能力处理正则表达式。也就是说,再次假设您不想用完整的bash膨胀高山图像,
grep
进行了救援。您的情况的解决方案将是(第13行,ff。)说明
如果直接从
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[
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|
.只需将一个放在
脚本的顶部,
我认为您正在用 /bin /sh执行它,
SH不支持Bash Regex。
或者,您正在使用最近介绍的Bash的过时版本,在Bash中发出了正则。
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.