简体   繁体   English

Bash脚本无法执行Go命令

[英]Bash script can't execute Go command

I'm trying to write a bash script to automatically run a go get/install in different directories. 我正在尝试编写一个bash脚本来自动在其他目录中运行go get / install。 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 cd ../web
  • go get 去弄
    ./staging.sh: line 43: go: command not found ./staging.sh:第43行:go:找不到命令
  • cd ../web cd ../web
  • go install 去安装
    ./staging.sh: line 44: go: command not found ./staging.sh:第44行:go:找不到命令
  • cd ../services cd ../服务
  • go get 去弄
    ./staging.sh: line 45: go: command not found ./staging.sh:第45行:go:找不到命令
  • cd ../services cd ../服务
  • go install 去安装
    ./staging.sh: line 46: go: command not found ./staging.sh:第46行:go:找不到命令

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? 为什么从脚本运行时它们不执行?

I'm guessing you followed the installation instructions on the go installation page that tell you to add some lines to your ~/.profile file. 我猜您遵循了go安装页面上的安装说明,该说明告诉您在~/.profile文件中添加一些行。 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. 该文件不会为非交互式会话(例如您的脚本)加载因此,您需要将其添加到Shell的rcfile中,或者通过脚本中的完整路径引用go二进制文件。

You can find out the full path of go by running in your shell: 您可以通过在Shell中运行来找到go的完整路径:

$ 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:/path/to

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM