0

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 '*'
  • Please paste your script at [shellcheck.net](http://www.shellcheck.net/) and try to implement the recommendations made there. – Cyrus Jun 06 '21 at 18:05
  • Don't export variables that do not need to be exported. – William Pursell Jun 06 '21 at 18:06
  • It might help to understand why your initial command is the same as: `/usr/local/go/bin/go build -o bin/adapter -ldflags=-X\ main.level=info` and is also the same as `/usr/local/go/bin/go build -o bin/adapter -ldflags=-"X ma"in.level=info`. – William Pursell Jun 06 '21 at 18:07

1 Answers1

1

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[@]}"
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • I want command to run exactly as it runs on terminal. In you response double quotes are missing. – Ashwini Taradale Jun 07 '21 at 08:04
  • In the way you run the command from the terminal, `go` also does not see any double quotes, and since you claim that this would work, of course I omitted them. BTW, did you actually tray my solution? If you did and it does not work, also include the trace (set -x) from my suggestion in your question. – user1934428 Jun 07 '21 at 08:16