Bash 脚本无法执行 Go 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39186854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:05:31  来源:igfitidea点击:

Bash script can't execute Go command

bashgo

提问by Graham

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

我正在尝试编写一个 bash 脚本来自动在不同目录中运行 go get/install。相关部分在这里:

( 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
  • 光盘../网络
  • go get
    ./staging.sh: line 43: go: command not found
  • 光盘../网络
  • go install
    ./staging.sh: line 44: go: command not found
  • 光盘 ../服务
  • go get
    ./staging.sh: line 45: go: command not found
  • 光盘 ../服务
  • 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?

如果我只是手动转到目录并运行命令,它们就可以正常工作。为什么从脚本运行时它们不执行?

回答by pnovotnak

I'm guessing you followed the installation instructions on the go installation page that tell you to add some lines to your ~/.profilefile. 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.

我猜您是按照 go 安装页面上的安装说明操作的,该说明告诉您在~/.profile文件中添加一些行。该文件不会为非交互式会话(例如;您的脚本)加载因此,您要么需要将其添加到 shell 的 rcfile 中,要么通过脚本中的完整路径引用 go 二进制文件。

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

您可以go通过在 shell 中运行来找出 的完整路径:

$ which go
/path/to/go

Then, in your script:

然后,在您的脚本中:

GO=/path/to/go
$GO command

Or, you can extend your PATHinside the script:

或者,您可以PATH在脚本内部扩展:

PATH=$PATH:/path/to