linux - "find a command in a path"是什么意思?

标签 linux shell path environment-variables

这是我操作系统类(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/

相关文章:

linux - echo 在/bin/sh 和/bin/bash 之间打印 "\n"的方式不同

html - 沿路径发出动画 SVG

python - 操作系统错误 : The CUDA lib64 path could not be located in/usr/lib64

java - 路径中的 Ant 大写与小写字符

linux - 解析 ASCII 十六进制转储以正确转换为二进制

linux - ftp 在 Linux 上显示文件及其所有者

linux - 为什么像 cat 这样的命令在后台运行时会立即退出?

linux - 在 Linux 中为 MongoDB 创建副本集

linux - shell 脚本将文件复制到新文件。新文件名应作为输入文件名并带有扩展名

正则表达式在文本中查找目录