命令在终端中运行时没有错误,但在bash脚本中失败。

huangapple go评论127阅读模式
英文:

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=&quot;-X main.level=info&quot; cmd/adapter/*

My bash script is as shown below :-

#!/bin/bash

export GO=/usr/local/go/bin/go
export ldflags=&quot;-ldflags=\&quot;-X main.level=info\&quot;&quot;

CMD=&quot;$GO build -o bin/adapter $ldflags cmd/adapter/*&quot;
echo $CMD
${CMD}

Bash debug using &quot;bash -x&quot; produces following output :-

+ export GO=/usr/local/go/bin/go
+ GO=/usr/local/go/bin/go
+ export &#39;ldflags=-ldflags=&quot;-X main.level=info&quot;&#39;
+ ldflags=&#39;-ldflags=&quot;-X main.level=info&quot;&#39;
+ CMD=&#39;/usr/local/go/bin/go build -o bin/adapter -ldflags=&quot;-X main.level=info&quot; cmd/adapter/*&#39;
+ echo /usr/local/go/bin/go build -o bin/adapter &#39;-ldflags=&quot;-X&#39; &#39;main.level=info&quot;&#39; &#39;cmd/adapter/*&#39;
/usr/local/go/bin/go build -o bin/adapter -ldflags=&quot;-X main.level=info&quot; cmd/adapter/*
+ /usr/local/go/bin/go build -o bin/adapter &#39;-ldflags=&quot;-X&#39; &#39;main.level=info&quot;&#39; &#39;cmd/adapter/*&#39;
invalid value &quot;\&quot;-X&quot; for flag -ldflags: missing =&lt;value&gt; in &lt;pattern&gt;=&lt;value&gt;
usage: go build [-o output] [build flags] [packages]
Run &#39;go help build&#39; for details.

Am I doing something wrong?

Until now I tried using different double quote(") escaping techniques also tried $ldflags and &quot;$ldflags&quot; but gets the same output.

Update 1 :-
Tried below option but still not working :-

ldflags=(-ldflags=&quot;-X main.level=info&quot;)
${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=&quot;-X main.level=info&quot;)
+ export ldflags
+ CMD=&#39;/usr/local/go/bin/go build -o bin/adapter -ldflags=-X main.level=info cmd/adapter/*&#39;
+ echo /usr/local/go/bin/go build -o bin/adapter -ldflags=-X main.level=info &#39;cmd/adapter/*&#39;
/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 &#39;cmd/adapter/*&#39;
malformed import path &quot;main.level=info&quot;: invalid char &#39;=&#39;
malformed import path &quot;cmd/adapter/*&quot;: invalid char &#39;*&#39;

答案1

得分: 1

但是从bash脚本中失败了....

你的命令不完全相同。在第一个版本中(你声称可以工作),你写的是:

/usr/local/go/bin/go build -o bin/adapter -ldflags=&quot;-X main.level=info&quot; cmd/adapter/*

这意味着go的第四个参数(在C术语中为argv[4])变成了(在shell处理引号后):

> -ldflags=-X main.level=info

在你的脚本中,我们可以从跟踪中看到,这个参数被传递为:

> -ldflags="-X

这意味着我们有一个不需要的双引号,并且_main.level=info_丢失了(这个部分进入了下一个参数)。

在你的情况下,我会将参数定义为一个数组,以go希望看到的方式,即:

pars=(build -o bin/adapter &#39;-ldflags=-X main.level=info&#39; cmd/adapter/*)

然后像这样运行它们:

echo Running $GO &quot;${pars[@]}&quot; ...
&quot;$GO&quot; &quot;${pars[@]}&quot;
英文:

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=&quot;-X main.level=info&quot; 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 &#39;-ldflags=-X main.level=info&#39; cmd/adapter/*)

and run them like this:

echo Running $GO &quot;${pars[@]}&quot; ...
&quot;$GO&quot; &quot;${pars[@]}&quot;

huangapple
  • 本文由 发表于 2021年6月7日 02:02:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/67862182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定