3

I'm trying to write a bash script to automatically run a go get/install in different directories. The relevant part is here:

( cd ../web ; go get )
( cd ../web ; go install )
( cd ../services ; go get )
( cd ../services ; go install )

When I execute the script, I get this though:

  • cd ../web
  • go get
    ./staging.sh: line 43: go: command not found
  • cd ../web
  • go install
    ./staging.sh: line 44: go: command not found
  • cd ../services
  • go get
    ./staging.sh: line 45: go: command not found
  • cd ../services
  • go install
    ./staging.sh: line 46: go: command not found

If I just go to the directories manually and run the commands, they work fine. Why aren't they executing when running from the script?

5
  • 4
    Is go in the $PATH ? Commented Aug 28, 2016 at 0:04
  • Sounds like a path issue.. Try adding a printenv | grep PATH to your script and making sure the go binary is in one of the folders listed. If you just installed go, try starting a new terminal Commented Aug 28, 2016 at 0:05
  • I get the path as: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin Which is different than if I run grep the path when I go to the file. So it probably is a path issue. How can I make the script get the correct path? Commented Aug 28, 2016 at 0:19
  • Does echo $SHELL produce the same result when you run it in your interactive shell and your script? It might be that you are loading a different shell and that's not loading the .*rc file where your $PATH is getting properly set. Commented Aug 28, 2016 at 0:44
  • Yes, both print bin/bash Commented Aug 28, 2016 at 0:57

1 Answer 1

6

I'm guessing you followed the installation instructions on the go installation page that tell you to add some lines to your ~/.profile file. This file doesn't load for non-interactive sessions (eg; your script.) So you either need to add it to your shell's rcfile, or reference the go binary by it's full path in your script.

You can find out the full path of go by running in your shell:

$ which go
/path/to/go

Then, in your script:

GO=/path/to/go
$GO command

Or, you can extend your PATH inside the script:

PATH=$PATH:/path/to
Sign up to request clarification or add additional context in comments.

3 Comments

I've tried both of these. It recognizes the command now, but it won't download any of the third party libraries because GOPATH isn't set. I tried setting GOPATH in the script the same way it's set in the ~/.profile but it doesn't seem to work.
Try doing the printenv again but this time without the grep.
Finally figured it out. After putting the paths in etc/bash.bashrc, it wasn't sourcing from there until I added: #!/bin/bash chmod a+x /etc/bash.bashrc PS1='$ ' source /etc/bash.bashrc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.