git如何在终端上作为命令在当前目录中运行C脚本? [英] How is git able to run C scripts in the current directory as a command on terminal?

查看:172
本文介绍了git如何在终端上作为命令在当前目录中运行C脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在研究bash和shell,并试图弄清楚git如何使终端命令能够在当前目录中运行C脚本,例如git init,git push/pull等.

I have been looking into bash and shell recently and been trying to work out how git is able to make a terminal command that runs C scripts in the current directory e.g git init, git push/pull etc.

我一直在尝试通过在我的主目录中创建一个可执行的python脚本在python中进行仿真,

I have been trying to simulate it in python, by making an executable python script in my home directory,

#!/usr/bin/env python
import os
print("current_dir: ",os.getcwd()) #prints /Users/usr/folder/script.py

然后我创建一个.command文件,该文件调用python脚本

Then I create a .command file that calls the python script

cd
cd FOLDER/
python3 SCRIPT.py

并编辑bash配置文件以导出变量以运行.command文件.

and editing the bash profile to export a variable to run the .command file.

export mycommand=/Users/urn/folder/command.command

尽管这与git实现其命令行的方式还差得很远.例如,当我运行我的脚本实际上不是终端命令时,它只是一个环境变量,因此是$.

Although this is not even nearly close to the way git achieves its command line. For example, when I run my script is not actually a terminal command it is just an environment variable, hence the $.

$mycommand

第二,这将转到python文件的目录并从该目录运行脚本,因此输出将始终相同

Secondly, this goes to the directory of the python file and runs the script from with that directory, therefore the output will always be the same

/Users/usr/folder/script.py

尽管在git中,它会在当前目录中运行文件.因此,打印语句将根据终端目录而更改.

Although in git it runs the file in the current directory. Therefore the print statement would change depending on the terminal directory.

我如何创建自己的终端命令"(例如git init)以在我所在的任何目录中运行python脚本.PS,我在Mac上.

How am I able to create my own 'terminal command' such as git init to run my python script in whatever directory I'm in. Ps, I'm on mac.

任何帮助将不胜感激:)

Any help would be greatly appreciated :)

推荐答案

听起来您至少缺少两个基本概念:

It sounds like you are missing at least two basic concepts:

  • 搜索路径:当您发出的命令不是shell函数或内置命令且具有不合格名称(一个不包含/字符的命令)时,shell在零个或多个目录的列表中搜索匹配的可执行文件.此目录列表是路径",它存储在PATH环境变量中.您可以根据需要进行更改.当您不指定git程序的路径时,这就是外壳程序查找它的方式.

  • The search path: when you issue a command that is not a shell function or built-in and that has an unqualified name (one with no / characters), the shell searches a list of zero or more directories for a matching executable file. This list of directories is the "path", and it is stored in the PATH environment variable. You can change it if you wish. This is how the shell finds the git program when you do not specify a path to it.

可执行脚本:Python,shell,Perl,.通过将适当的shebang行作为第一行并分配可执行模式,可以使必须通过解释器运行的程序仅通过名称即可执行.实际上,您在示例Python程序中包含了一个适当的shebang行,但是您似乎不明白其含义,因为您可以通过python3命令显式启动该脚本. shebang行只是对Python的另一条评论,但这对系统是有意义的.

Executable scripts: Python, shell, Perl, etc. programs that must be run via an interpreter can be made executable by name alone by including an appropriate shebang line as the very first line and assigning an executable mode. You include an appropriate shebang line in your example Python program, in fact, but you seem not to understand its significance, because you explicitly launch the script via the python3 command. The shebang line is just another comment to Python, but it is meaningful to the system.

似乎您可能还缺少其他一些概念,例如您的脚本不需要位于当前工作目录中就可以通过python3启动器运行它,尽管路径仍然存在.只需指定其完整路径名即可.另外,Python在路径PYTHONPATH上有其自己的变体,它可以通过它定位模块和包.但是,这些是替代方法-前两点足以实现您的明显目标.具体来说,

It seems like you probably also are missing some other concepts, like the fact that your script doesn't need to be in the current working directory for you to run it via the python3 launcher, path notwithstanding. Just specify its full pathname. Alternatively, Python has its own variation on a path, PYTHONPATH, by which it can locate modules and packages. These are alternatives, however -- the first two points are enough to achieve your apparent objective. Specifically,

  1. 将shebang行保留在脚本中,尽管默认的python可能是v2.7,因此,如果您真的想通过python3专门运行它,请修改shebang行,使之如此:

  1. Keep the shebang line in your script, though your default python is probably v2.7, so if you really want to run it specifically via python3 then modify the shebang line to say so:

#!/usr/bin/env python3

  • 确保文件具有可执行模式.例如,

  • Make sure the file has executable mode. For example,

    chmod 0755 /Users/urn/bin/SCRIPT.py
    

    然后,您应该可以通过脚本的完整路径名在任何地方执行脚本.

    Then you should be able to execute the script from anywhere via its full pathname.

    要通过其简单名称从任何位置访问它,请确保包含它的目录在您的路径中.为此,明智的选择一个适当的目录,例如/usr/local/bin/Users/urn/bin(您可能需要先创建目录).无论选择哪个,请确保该目录位于您的PATH中.例如,编辑/Users/urn/.bash_profile,必要时创建它,并确保它包含(例如)命令

    To access it from anywhere via its simple name, ensure that the directory containing it is in your path. For this purpose, it would be wise to choose an appropriate directory, such as /usr/local/bin or /Users/urn/bin (you may need to create the directory first). Whichever you choose, ensure that that directory is in your PATH. For example, edit /Users/urn/.bash_profile, creating it if necessary, and ensure that it contains (say) the commands

    PATH=$PATH:/Users/urn/bin
    export PATH
    

    这将在您随后打开的 new 终端窗口中生效,但不会在已经打开的任何终端窗口中自动生效.在这些窗口中,您将可以通过脚本的简单名称从任何地方运行该脚本.

    That will take effect in new Terminal windows you open afterward, but not automatically in any that are already open. In those windows, you will be able to run the script, from anywhere, via its simple name.

    这篇关于git如何在终端上作为命令在当前目录中运行C脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆