They handle executable elfs, scripts and symbolic links from PATH, however what the algorithm of this doing?
I'm afraid of
I cannot find a source code of this part of a shell.
它们处理来自PATH的可执行elf,脚本和符号链接,但是这样做的算法是什么?我害怕我找不到这部分shell的源代码。
UDP: Oh, I'm stupid. It looks for EACH executable file in PATH, either directory or ordinary file.
UDP:哦,我很蠢。它在PATH中查找EACH可执行文件,无论是目录还是普通文件。
3 个解决方案
#1
4
Well, the actual search is performed by find_user_command_in_path()
in findcmd.c:553.
嗯,实际搜索由findcmd.c:553中的find_user_command_in_path()执行。
The algorithm to search for a command ${foo}
is basically:
搜索命令$ {foo}的算法基本上是:
- check if
${foo}
is absolute: if it is return this path and stop searching - iterate over all elements in PATH:
for p in ${PATH}
- construct a path
${p}/${foo}
and see if it exists- if it exists and is executable return this path and stop searching
如果它存在且可执行则返回此路径并停止搜索
构造路径$ {p} / $ {foo}并查看它是否存在,如果它存在且可执行返回此路径并停止搜索
- construct a path
检查$ {foo}是绝对的:如果返回此路径并停止搜索
遍历PATH中的所有元素:对于$ {PATH}中的p构造路径$ {p} / $ {foo}并查看它是否存在(如果存在且可执行)返回此路径并停止搜索
#2
0
I'm no expert in this area, but I'm almost perfectly sure that on Linux the executable bit in file permissions is all that matters. No sophisticated algorithm needed.
我不是这个领域的专家,但我几乎完全相信在Linux上,文件权限中的可执行位是最重要的。不需要复杂的算法。
#3
0
Let's say that we have a file called hello in the current directory, and that the file contains just one line: echo "hello"
假设我们在当前目录中有一个名为hello的文件,该文件只包含一行:echo“hello”
-
If you ran chmod 755 on the file and and you subsequently execute the file, then the bash shell will look through every path that you have listed in the PATH variable of say .bashrc, starting with the first path, until it locates the first path that contains your hello executable. Think of PATH as a linked list and think of the bash shell as going through the linked list of paths, path by path. If the bash shell is not running the hello executable that you want it to run, you have one option: put your hello executable in any one of the preceeding paths.
如果你在文件上运行了chmod 755并且随后执行了该文件,那么bash shell将查看你在.bashrc的PATH变量中列出的每个路径,从第一个路径开始,直到它找到第一个路径包含你好的可执行文件。将PATH视为链接列表,并将bash shell视为路径的链接列表,路径路径。如果bash shell没有运行您希望它运行的hello可执行文件,那么您有一个选择:将hello可执行文件放在任何一个前面的路径中。
-
I am lazy. I don't bother to turn hello into an executable i.e. I am not running the chmod command and I just run
我很懒。我不打算把你好打成一个可执行文件,即我没有运行chmod命令,我只是运行
bash hello
where the bash shell is going to look for the hello file in the current directory, fork a bash process and the forked bash process is going to run the hello file before the forked bash process dies.
bash shell将在当前目录中查找hello文件,fork一个bash进程,forked bash进程将在forked bash进程终止之前运行hello文件。
I am using the bash shell as an example but any other shell will behave the same way.
我使用bash shell作为示例,但任何其他shell将以相同的方式运行。
#1
4
Well, the actual search is performed by find_user_command_in_path()
in findcmd.c:553.
嗯,实际搜索由findcmd.c:553中的find_user_command_in_path()执行。
The algorithm to search for a command ${foo}
is basically:
搜索命令$ {foo}的算法基本上是:
- check if
${foo}
is absolute: if it is return this path and stop searching - iterate over all elements in PATH:
for p in ${PATH}
- construct a path
${p}/${foo}
and see if it exists- if it exists and is executable return this path and stop searching
如果它存在且可执行则返回此路径并停止搜索
构造路径$ {p} / $ {foo}并查看它是否存在,如果它存在且可执行返回此路径并停止搜索
- construct a path
检查$ {foo}是绝对的:如果返回此路径并停止搜索
遍历PATH中的所有元素:对于$ {PATH}中的p构造路径$ {p} / $ {foo}并查看它是否存在(如果存在且可执行)返回此路径并停止搜索
#2
0
I'm no expert in this area, but I'm almost perfectly sure that on Linux the executable bit in file permissions is all that matters. No sophisticated algorithm needed.
我不是这个领域的专家,但我几乎完全相信在Linux上,文件权限中的可执行位是最重要的。不需要复杂的算法。
#3
0
Let's say that we have a file called hello in the current directory, and that the file contains just one line: echo "hello"
假设我们在当前目录中有一个名为hello的文件,该文件只包含一行:echo“hello”
-
If you ran chmod 755 on the file and and you subsequently execute the file, then the bash shell will look through every path that you have listed in the PATH variable of say .bashrc, starting with the first path, until it locates the first path that contains your hello executable. Think of PATH as a linked list and think of the bash shell as going through the linked list of paths, path by path. If the bash shell is not running the hello executable that you want it to run, you have one option: put your hello executable in any one of the preceeding paths.
如果你在文件上运行了chmod 755并且随后执行了该文件,那么bash shell将查看你在.bashrc的PATH变量中列出的每个路径,从第一个路径开始,直到它找到第一个路径包含你好的可执行文件。将PATH视为链接列表,并将bash shell视为路径的链接列表,路径路径。如果bash shell没有运行您希望它运行的hello可执行文件,那么您有一个选择:将hello可执行文件放在任何一个前面的路径中。
-
I am lazy. I don't bother to turn hello into an executable i.e. I am not running the chmod command and I just run
我很懒。我不打算把你好打成一个可执行文件,即我没有运行chmod命令,我只是运行
bash hello
where the bash shell is going to look for the hello file in the current directory, fork a bash process and the forked bash process is going to run the hello file before the forked bash process dies.
bash shell将在当前目录中查找hello文件,fork一个bash进程,forked bash进程将在forked bash进程终止之前运行hello文件。
I am using the bash shell as an example but any other shell will behave the same way.
我使用bash shell作为示例,但任何其他shell将以相同的方式运行。