简体   繁体   English

我正在尝试编写一个 shell 程序来一次执行多个命令

[英]I am trying to write a shell program to execute more than one command at a time

My Code我的代码

#include<stdio.h> 
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main() 
{ 

char * arg_list[3];
arg_list[0] = "ls";
arg_list[1] = "-l";
arg_list[2] = 0;

char *arg_list2[3];
arg_list2[0] = " ps";
arg_list2[1] = "-ef";
arg_list2[2] = 0;

    for(int i=0;i<5;i++){ // loop will run n times (n=5) 

       if(fork() == 0) { 
    if (i == 0){
    execvp("ls", arg_list);
}else if(i==1){
execvp("ps" , arg_list2);
}else if(i>1){
           printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid()); 
        exit(0); 
}
      } 
    } 
    for(int i=0;i<5;i++) // loop will run n times (n=5) 
           wait(NULL); 

} 

ME trying to modify it我试图修改它

#include<stdio.h> 
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main() 
{ 
    for(int i=0;i<5;i++){ // loop will run n times (n=5) 

       if(fork() == 0) { 
           printf("[son] pid %d from [parent] pid %d\n",getpid(),getppid()); 
        execlp(argv[i],argv[i],argv[i+1],(char*)NULL);
        exit(0); 
      } 
    } 
    for(int i=0;i<5;i++) // loop will run n times (n=5) 
           wait(NULL); 

} 

-- NEED GUIDANCE AND UNDERSTANDING -- 需要指导和理解

I am trying to make my own tiny little shell program.我正在尝试制作自己的小 shell 程序。 When I run my first code works fine, runs all commands on the command line.当我运行我的第一个代码时工作正常,在命令行上运行所有命令。 But I cannot know and define all commands the user might enter.但我无法知道和定义用户可能输入的所有命令。 So i am trying to get a base code which could run any commands single or multiple entered by user.所以我试图获得一个基本代码,它可以运行用户输入的任何单个或多个命令。 I tried using execlp where it does not compile saying argv is not defined which is true as i don't want to specifically define it.我尝试使用execlp在它不编译的地方说argv未定义,这是真的,因为我不想专门定义它。

I am trying to make my own tiny little shell program.我正在尝试制作自己的小 shell 程序。 When I run my first code works fine, runs all commands on the command line.当我运行我的第一个代码时工作正常,在命令行上运行所有命令。 But I cannot know and define all commands the user might enter.但我无法知道和定义用户可能输入的所有命令。

For sure.... A shell program purpose is basically:当然....一个shell程序的目的基本上是:

  1. Read user input读取用户输入
  2. Execute user input执行用户输入
  3. Return result of execution.返回执行结果。

There's nothing in your code that read user input....您的代码中没有任何内容可以读取用户输入....

So i am trying to get a base code which could run any commands single or multiple entered by user.所以我试图获得一个基本代码,它可以运行用户输入的任何单个或多个命令。

So read user input;-)所以阅读用户输入;-)

I tried using execlp where it does not compile saying argv is not defined which is true as i don't want to specifically define it.我尝试使用 execlp 在它不编译的地方说 argv 未定义,这是真的,因为我不想专门定义它。

For sure... but how would GCC guessed that `argv[]̀ must be automaticallty filled with user input?当然......但是 GCC 怎么会猜到 `argv[]̀ 必须自动填充用户输入? There's nothing automatic when coding in C language.用 C 语言编码时,没有什么是自动的。 You have to manage this manually.您必须手动管理它。

Also, note that argc , argv et envp are usually reserved for main() function:另外,请注意argcargvenvp通常保留给main() function:

main(int argc, char **argv, char **envp)

So you may use something else to build your command array.所以你可以使用其他东西来构建你的命令数组。

In pseudo code, what you must implement is:在伪代码中,您必须实现的是:

quit=0
while (quit = 0) {
    command_to_run = read_user_input();
    if (command_to_run == "exit") {
       quit = 1;
    } else {
       execute(command_to_run);
    }
}

Some advices:一些建议:

  • Try to use more functions.尝试使用更多功能。 For example, implement a fork_and_run(char **cmd) function to fork and then execute command provided by the user.例如,实现一个fork_and_run(char **cmd) function fork,然后执行用户提供的命令。 Il will make your code more readable and easy to maintain. Il 将使您的代码更具可读性和易于维护。

  • Read carefully manpages: everything you should know (like, for example, the fact that array provided to execvp() must be NULL-terminated) is written in it.仔细阅读手册页:您应该知道的所有内容(例如,提供给execvp()的数组必须以 NULL 结尾的事实)都写在其中。

  • Your debugging messages should be printed to stderr .您的调试消息应打印到stderr The result of the command run must be printed to stdin , so use fprintf() instead of printf() to write to the correct stream.命令运行的结果必须打印到stdin ,所以使用fprintf()而不是printf()来写入正确的 stream。

  • I would use a #define debug(x) fprintf(stderr, x) or something similar for debugging output so that you can easily disable later;-)我会使用#define debug(x) fprintf(stderr, x)或类似的东西来调试 output 以便您以后可以轻松禁用;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一次不能执行多个shell命令 - Can't execute more than one shell command at a time 在shell(bash)中如何在管道中执行多个命令? - In a shell (bash) How can I execute more than one command in a pipeline? Nodejs Shell脚本在Linux上运行良好,但在Windows中运行不正常。 为什么不执行多个命令 - Nodejs shell script works fine in linux but not in Windows. Why won't it execute more than one command Bash脚本执行多个命令 - Bash Script execute more than one command 我正在尝试将 shell 脚本中的日志文件内容写入 email 而不是 go 到日志文件并打开它以查看结果 - i am trying to write the contents of logfile in shell script in an email rather than go to logfile and open it to see the result 我如何在linux的exec命令中执行多个命令 - How can I execute more than one command in exec command of linux expect 如何在Gradle文件中的任务中执行多个Shell脚本 - How to execute more than one shell scripts in a task in gradle file 如何编写Shell脚本以设置执行确切时间的命令? - How to write shell script to set to execute command for some exact time? 如何同时在多个选项卡/ shell中运行相同的linux命令? - How do I run the same linux command in more than one tab/shell simultaneously? Bash Shell脚本循环未处理多个命令 - Bash Shell Script Loop not processing more than one command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM