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?

Graham
  • 43
  • 1
  • 1
  • 3
  • 4
    Is `go` in the $PATH ? – jedifans Aug 28 '16 at 00: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 – pnovotnak Aug 28 '16 at 00: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? – Graham Aug 28 '16 at 00: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. – pnovotnak Aug 28 '16 at 00:44
  • Yes, both print bin/bash – Graham Aug 28 '16 at 00:57

1 Answers1

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
Community
  • 1
  • 1
pnovotnak
  • 4,341
  • 2
  • 27
  • 38
  • 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. – Graham Aug 28 '16 at 01:56
  • Try doing the printenv again but this time without the grep. – jedifans Aug 28 '16 at 11:22
  • 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` – Graham Aug 29 '16 at 00:07