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
Bash script can't execute Go command
提问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 ~/.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.
我猜您是按照 go 安装页面上的安装说明操作的,该说明告诉您在~/.profile
文件中添加一些行。该文件不会为非交互式会话(例如;您的脚本)加载。因此,您要么需要将其添加到 shell 的 rcfile 中,要么通过脚本中的完整路径引用 go 二进制文件。
You can find out the full path of go
by 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 PATH
inside the script:
或者,您可以PATH
在脚本内部扩展:
PATH=$PATH:/path/to