这是我操作系统类(class)作业说明的摘录。粗体部分是我无法理解的部分,我现在也无法联系到教授,我真的很想今晚开始。我知道什么是环境变量,我想……只是在 shell 中声明的一个变量,对吧?但是,粗线具体是什么意思?
Write a C program to implement an interactive shell in which users execute commands.
Call this program myshell (so use gcc -o myshell -Wall etc. to compile).
Create an infinite loop that repeatedly prompts the user to enter a command (see example output and input below).
Before executing the command entered by the user, the command must be found using the path specified by environment variable THEPATH (do not use PATH!). By default, the THEPATH variable is not set, so for testing, you'll want to set (and unset) this variable manually (see details below). If THEPATH is found, your program must execute the command in a child process via fork() and one of the exec() system calls.
To obtain and parse THEPATH, consider using the getenv() function and the strtok() or strsep() functions.
最佳答案
要让 shell 运行程序,它必须知道该程序在哪里。例如,您希望能够键入 ls
在提示符下,但实际二进制文件为 ls
可以在 /bin/ls
找到.那就是PATH
(或您的情况, THEPATH
)进来。当您键入 ls
时, shell 关闭并查看每个 PATH
具有匹配名称的程序的目录。当它找到一个时,它就会运行它。让我们使用 ls
作为例子,一个 PATH
设置为:
/usr/local/bin:/usr/bin:/bin
假设ls
是/bin/ls
, 然后 shell 首先查找 ls
在/usr/local/bin
, 没有找到,然后在 /usr/bin
中查找, 然后终于在 /bin
中找到了并执行它。
实际上做这个操作就是你作业中关于getenv
的提示, strtok
, 和 strsep
进来了。
关于linux - "find a command in a path"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12658237/