如何使用 bash 脚本从不同目录运行代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11189637/
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
How do I run code from a different directory with a bash script
提问by JBWhitmore
I have been putting my code on github, but I've run into an implementation snag. I run the same code on many computers (including a computer that I do not have root access on).
我一直把我的代码放在 github 上,但我遇到了一个实现障碍。我在多台计算机(包括我没有 root 访问权限的计算机)上运行相同的代码。
One piece of code (a bash script) calls some python code like:
一段代码(一个 bash 脚本)调用了一些 python 代码,例如:
python somecode.py
The shell will run the correct version of python, but it won't find somecode.py.
shell 将运行正确版本的 python,但它不会找到 somecode.py。
What I've tried:
我试过的:
Fail #1: I tried to add both the directory which contains somecode.py and the full path to the file to the PATH; to no avail. [Errno 2] No such file or directory
失败 #1:我尝试将包含 somecode.py 的目录和文件的完整路径添加到 PATH;无济于事。[Errno 2] 没有那个文件或目录
Fail #2: I can make it work for one computer ONLY if I add the full path to the correct version of python in the top line:
失败 #2:只有当我在顶行添加正确版本的 python 的完整路径时,我才能让它在一台计算机上工作:
#!/usr/local/cool/python/version/location
However this breaks it running on any other computer.
但是,这会破坏它在任何其他计算机上的运行。
Fail #3: I can also make it work if I make the bash script say:
失败 #3:如果我让 bash 脚本说:
python /full/path/to/github/place/somecode.py
but again, this only works for ONE computer because the paths are different for different computers.
但同样,这仅适用于一台计算机,因为不同计算机的路径不同。
What I really want to do: I want to be able to use the same code (both bash script and somecode.py) on multiple computers.
我真正想做的是:我希望能够在多台计算机上使用相同的代码(bash 脚本和 somecode.py)。
Any suggestions about how to do this properly is welcome. Thanks!
欢迎任何有关如何正确执行此操作的建议。谢谢!
Solution
解决方案
Added:
添加:
#!/usr/bin/env python
To the top of my somecode.py code;
到我的 somecode.py 代码的顶部;
mv somecode.py somecode
chmod +x somecode
Make sure PATH has /full/path/to/directory/with/somecode.
确保 PATH 有 /full/path/to/directory/with/somecode。
Bash script now says only:
Bash 脚本现在只说:
somecode
and it works.
它有效。
回答by Levon
For problem #2 try
对于问题#2 尝试
#!/usr/bin/env python
though it may find different versions of Python on different machines, but as long as that's not a problem this should fix that particular problem
尽管它可能会在不同的机器上找到不同版本的 Python,但只要这不是问题,这应该可以解决该特定问题
See this SO question Python deployment and /usr/bin/env portabilitytoo. And this post by Alex Martellire use of this.
请参阅此 SO 问题Python 部署和 /usr/bin/env 可移植性。Alex Martelli 的这篇文章重新使用了这一点。
回答by cdarke
If you say python somefile.pythen it will take the location of somefile.pyas the current directory, not from $PATH. It will take the location of pythonfrom $PATH.
如果你说,python somefile.py那么它将把 的位置somefile.py作为当前目录,而不是来自$PATH. 它将采用pythonfrom的位置$PATH。
If you say somefile.pythen it will take the location of somefile.pyfrom $PATH, and the location of pythonfrom the #!line of your python script, which can use the PATH if you follow @Levon's suggestion.
如果你说,somefile.py那么它将采用somefile.pyfrom$PATH的位置,以及python来自#!你的python脚本行的位置,如果你遵循@Levon的建议,它可以使用 PATH 。

