英文:
Command runs from terminal without error but fails from bash script
问题
以下命令在终端中运行时没有任何错误,但在bash脚本中失败。
/usr/local/go/bin/go build -o bin/adapter -ldflags="-X main.level=info" cmd/adapter/*
我的bash脚本如下所示:
#!/bin/bash
export GO=/usr/local/go/bin/go
export ldflags="-ldflags=\"-X main.level=info\""
CMD="$GO build -o bin/adapter $ldflags cmd/adapter/*"
echo $CMD
${CMD}
使用"bash -x"
进行Bash调试会产生以下输出:
+ export GO=/usr/local/go/bin/go
+ GO=/usr/local/go/bin/go
+ export 'ldflags=-ldflags="-X main.level=info"'
+ ldflags='-ldflags="-X main.level=info"'
+ CMD='/usr/local/go/bin/go build -o bin/adapter -ldflags="-X main.level=info" cmd/adapter/*'
+ echo /usr/local/go/bin/go build -o bin/adapter '-ldflags="-X' 'main.level=info"' 'cmd/adapter/*'
/usr/local/go/bin/go build -o bin/adapter -ldflags="-X main.level=info" cmd/adapter/*
+ /usr/local/go/bin/go build -o bin/adapter '-ldflags="-X' 'main.level=info"' 'cmd/adapter/*'
invalid value "\"-X" for flag -ldflags: missing =<value> in <pattern>=<value>
usage: go build [-o output] [build flags] [packages]
Run 'go help build' for details.
我做错了什么吗?
到目前为止,我尝试了不同的双引号(")转义技术,还尝试了$ldflags
和"$ldflags"
,但得到了相同的输出。
更新1:
尝试了下面的选项,但仍然不起作用:
ldflags=(-ldflags="-X main.level=info")
${ldflags[@]}
上述试验的调试输出如下所示:
+ mkdir -p bin
+ export GO=/usr/local/go/bin/go
+ GO=/usr/local/go/bin/go
+ ldflags=(-ldflags="-X main.level=info")
+ export ldflags
+ CMD='/usr/local/go/bin/go build -o bin/adapter -ldflags=-X main.level=info cmd/adapter/*'
+ echo /usr/local/go/bin/go build -o bin/adapter -ldflags=-X main.level=info 'cmd/adapter/*'
/usr/local/go/bin/go build -o bin/adapter -ldflags=-X main.level=info cmd/adapter/*
+ /usr/local/go/bin/go build -o bin/adapter -ldflags=-X main.level=info 'cmd/adapter/*'
malformed import path "main.level=info": invalid char '='
malformed import path "cmd/adapter/*": invalid char '*'
英文:
Below command runs without any error from terminal but fails from bash script.
/usr/local/go/bin/go build -o bin/adapter -ldflags="-X main.level=info" cmd/adapter/*
My bash script is as shown below :-
#!/bin/bash
export GO=/usr/local/go/bin/go
export ldflags="-ldflags=\"-X main.level=info\""
CMD="$GO build -o bin/adapter $ldflags cmd/adapter/*"
echo $CMD
${CMD}
Bash debug using "bash -x"
produces following output :-
+ export GO=/usr/local/go/bin/go
+ GO=/usr/local/go/bin/go
+ export 'ldflags=-ldflags="-X main.level=info"'
+ ldflags='-ldflags="-X main.level=info"'
+ CMD='/usr/local/go/bin/go build -o bin/adapter -ldflags="-X main.level=info" cmd/adapter/*'
+ echo /usr/local/go/bin/go build -o bin/adapter '-ldflags="-X' 'main.level=info"' 'cmd/adapter/*'
/usr/local/go/bin/go build -o bin/adapter -ldflags="-X main.level=info" cmd/adapter/*
+ /usr/local/go/bin/go build -o bin/adapter '-ldflags="-X' 'main.level=info"' 'cmd/adapter/*'
invalid value "\"-X" for flag -ldflags: missing =<value> in <pattern>=<value>
usage: go build [-o output] [build flags] [packages]
Run 'go help build' for details.
Am I doing something wrong?
Until now I tried using different double quote(") escaping techniques also tried $ldflags
and "$ldflags"
but gets the same output.
Update 1 :-
Tried below option but still not working :-
ldflags=(-ldflags="-X main.level=info")
${ldflags[@]}
Debug output for above trial is as follow :-
+ mkdir -p bin
+ export GO=/usr/local/go/bin/go
+ GO=/usr/local/go/bin/go
+ ldflags=(-ldflags="-X main.level=info")
+ export ldflags
+ CMD='/usr/local/go/bin/go build -o bin/adapter -ldflags=-X main.level=info cmd/adapter/*'
+ echo /usr/local/go/bin/go build -o bin/adapter -ldflags=-X main.level=info 'cmd/adapter/*'
/usr/local/go/bin/go build -o bin/adapter -ldflags=-X main.level=info cmd/adapter/*
+ /usr/local/go/bin/go build -o bin/adapter -ldflags=-X main.level=info 'cmd/adapter/*'
malformed import path "main.level=info": invalid char '='
malformed import path "cmd/adapter/*": invalid char '*'
答案1
得分: 1
但是从bash脚本中失败了....
你的命令不完全相同。在第一个版本中(你声称可以工作),你写的是:
/usr/local/go/bin/go build -o bin/adapter -ldflags="-X main.level=info" cmd/adapter/*
这意味着go
的第四个参数(在C术语中为argv[4])变成了(在shell处理引号后):
> -ldflags=-X main.level=info
在你的脚本中,我们可以从跟踪中看到,这个参数被传递为:
> -ldflags="-X
这意味着我们有一个不需要的双引号,并且_main.level=info_丢失了(这个部分进入了下一个参数)。
在你的情况下,我会将参数定义为一个数组,以go
希望看到的方式,即:
pars=(build -o bin/adapter '-ldflags=-X main.level=info' cmd/adapter/*)
然后像这样运行它们:
echo Running $GO "${pars[@]}" ...
"$GO" "${pars[@]}"
英文:
but fails from bash script ....
Your commands are not identical. In the first version (which you claim to work), you write
/usr/local/go/bin/go build -o bin/adapter -ldflags="-X main.level=info" cmd/adapter/*
which means that the 4th parameter to go
(argv[4] in C-terminology) becomes (after the shell deals with the quotes)
> -ldflags=-X main.level=info
In your script, we can see from the trace that this parameter is pased as
> -ldflags="-X
which means that we have an undesired double quote, and main.level=info is missing (this goes into the next parameter).
I would in your case define the parameters in an array
the way go
wants to see them, i.e.
pars=(build -o bin/adapter '-ldflags=-X main.level=info' cmd/adapter/*)
and run them like this:
echo Running $GO "${pars[@]}" ...
"$GO" "${pars[@]}"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论