英文:
Bash script can't execute Go command
问题
我正在尝试编写一个bash脚本,在不同的目录中自动运行go get/install命令。相关的部分如下所示:
(cd ../web; go get)
(cd ../web; go install)
(cd ../services; go get)
(cd ../services; go install)
当我执行脚本时,却出现了以下错误:
- 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
如果我手动进入这些目录并运行命令,它们可以正常工作。为什么在脚本中运行时它们没有执行?
英文:
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?
答案1
得分: 6
我猜你是按照Go安装页面上的安装说明进行操作,该页面告诉你要在~/.profile
文件中添加一些内容。然而,这个文件在非交互式会话(例如脚本)中不会加载。(参考链接:https://askubuntu.com/questions/247738/why-is-etc-profile-not-invoked-for-non-login-shells)所以你需要将它添加到你的shell的rc文件中,或者在脚本中使用go二进制文件的完整路径。
你可以在shell中运行以下命令来找到go
的完整路径:
$ which go
/path/to/go
然后,在你的脚本中:
GO=/path/to/go
$GO command
或者,你可以在脚本中扩展PATH
:
PATH=$PATH:/path/to
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论