c - 使用 execvp 在 Ubuntu 上用 C 语言构建 shell

标签 c linux

  1. 我打算在 Ubuntu 上用 C 编写一个简单的 shell。 我考虑过使用 exevcp() 函数。 我只能运行“ls”命令,其他命令似乎都不起作用。 谁能告诉我为什么会这样,或者给我一个构建 shell 的更好方法的想法?

  2. 我的目的是构建这个 shell;我不明白为什么我不能直接使用命令行并将其原样放入 execvp() 函数中。

    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    #include <string.h>
    #include <errno.h>
    
    int main(int argc,char **argv,char **envp)
    {
        char* args[] = {"history" , NULL};
    
        execvp(args[0],args);
    }
    

你能给我解释一下吗?

最佳答案

exec(3) 只能用于运行外部程序。 history 是大多数 shell 中的内置项,不能以这种方式运行。

关于c - 使用 execvp 在 Ubuntu 上用 C 语言构建 shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13520360/

相关文章:

c - 在用户空间使用 netlink 从内核接收数据时出错

c - 在内存中分配特定数量的字节

linux - 创建 Linux 别名以仅列出目录

python - 是否可以看到Python进程正在做什么?

c - 处理 awk 命令时如何将 'system()' 调用转换为 'fork() + execl()' ?

c - 如何解决2+2和2++2冲突

c - 通过 Malloc 增加分配给结构的内存大小

r - 在 linux 中为 R 安装 xgboost 时出错

c++ - 在 C++ 中将文件从一个目录复制到另一个目录

c - 如果客户端发送多个缓冲区而没有休眠,为什么 tcp 服务器会收到一个缓冲区?