[英]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程序的目的基本上是:
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:另外,请注意
argc
、 argv
和envp
通常保留给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.