具有文件输入的 exec 系列

发布于 2024-12-08 09:16:09 字数 422 浏览 0 评论 0 原文

大家好,我正在尝试用 C++ 编写一个 shell,但在使用 exec 命令使用输入文件的功能方面遇到了问题。例如,Linux 中的 bc shell 能够执行“bc < text.txt”,它像时尚一样批量计算文本中的行。我正在尝试用我的外壳做同样的事情。大致如下:

char* input = “input.txt”;
execlp(input, bc, …..)    // I don’t really know how to call the execlp command and all the doc and search have been kind of cryptic for someone just starting out.

使用 exec 命令是否可以做到这一点?或者我是否必须逐行读取并在 for 循环中运行 exec 命令?

Hey guys I am trying to write a shell with C++ and I am having trouble with the function of using input file with the exec commands. For example, the bc shell in Linux is able to do “bc < text.txt” which calculate the lines in the text in a batch like fashion. I am trying to do likewise with my shell. Something along the lines of:

char* input = “input.txt”;
execlp(input, bc, …..)    // I don’t really know how to call the execlp command and all the doc and search have been kind of cryptic for someone just starting out.

Is this even possible with the exec commands? Or will I have to read in line by line and run the exec commands in a for loop??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身软味 2024-12-15 09:16:09

您可以打开文件,然后将文件描述符dup2()发送到标准输入,或者您可以关闭标准输入,然后打开文件(这是有效的,因为标准输入是描述符0并且open( ) 返回编号最小的可用描述符)。

 const char *input = "input.txt";
 int fd = open(input, O_RDONLY);
 if (fd < 0)
     throw "could not open file";
 if (dup2(fd, 0) != 0)  // Testing that the file descriptor is 0
     throw "could not dup2";
 close(fd);             // You don't want two copies of the file descriptor
 execvp(command[0], &command[0]);
 fprintf(stderr, "failed to execvp %s\n", command[0]);
 exit(1);

您可能需要比 throw 更聪明的错误处理,尤其是因为这是子进程,而需要知道的是父进程。但是抛出站点标记了处理错误的点。

请注意close()

You can open the file and then dup2() the file descriptor to standard input, or you can close standard input and then open the file (which works because standard input is descriptor 0 and open() returns the lowest numbered available descriptor).

 const char *input = "input.txt";
 int fd = open(input, O_RDONLY);
 if (fd < 0)
     throw "could not open file";
 if (dup2(fd, 0) != 0)  // Testing that the file descriptor is 0
     throw "could not dup2";
 close(fd);             // You don't want two copies of the file descriptor
 execvp(command[0], &command[0]);
 fprintf(stderr, "failed to execvp %s\n", command[0]);
 exit(1);

You would probably want cleverer error handling than the throw, not least because this is the child process and it is the parent that needs to know. But the throw sites mark points where errors are handled.

Note the close().

残疾 2024-12-15 09:16:09

重定向由 shell 执行——它不是 bc 的参数。您可以调用 bash(相当于 bash -c "bc ),

例如,您可以使用 execvp 以及文件参数 " bash" 和参数列表

"bash"
"-c"
"bc < text.txt"

the redirect is being performed by the shell -- it's not an argument to bc. You can invoke bash (the equivalent of bash -c "bc < text.txt")

For example, you can use execvp with a file argument of "bash" and argument list

"bash"
"-c"
"bc < text.txt"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文