- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
a.log"这应该将结果带到输出,它没有响应并出错! int lsh_launch(c-6ren">
我的代码中的 execvp 有一些问题,我想写一个简单的终端,将命令的结果保存在日志文件中,问题是当我使用 "> a.log"
这应该将结果带到输出,它没有响应并出错!
int lsh_launch(char **args)
{
pid_t pid;
int status;
int i = 0;
while (args[i] != NULL)
{
printf("%s\n", args[i]);
i++;
}
args[i] = ">";
args[i + 1] = "a.log";
pid = fork();
if (pid == 0)
{
printf("child proc\n");
// Child process
if (execvp(args[0], args) == -1)
{
perror("lsh");
}
if (execvp(args[0], args) == -1)
{
perror("lsh");
}
exit(EXIT_FAILURE);
}
else if (pid < 0)
{
// Error forking
perror("lsh");
}
else
{
// Parent process
do {
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
当我将 args 更改为其他值(例如 -v
以查看版本)时,它起作用了,而且问题似乎出在导出到输出上!当我对程序执行 ls
和 > a.log
时的结果是:
- ls: cannot access >: No such file or directory
- ls: cannot access a.log: No such file or directory
最佳答案
重定向不是通过这种方式获得的。 >
(或类似的)是在 shell 中进行重定向的语法。 shell 解释命令行并在执行
命令之前进行重定向,这样:
pid = fork();
switch(pid) {
case 0:
d = open("myfile",O_WRONLY);
dup2(d,STDOUT_FILENO); // redirect *stdout* to open file d by duplicating it
close(d); // now unused d (d is a duplicate of *stdout*
exec**(...); // now mutate to a new code which inherits open file descriptors
exit(1);
break;
case -1: // error case of fork
break;
default:
wait(NULL); // or whatever you want, don't wait for *background style*
break;
}
关于c - execvp() 不处理写入输出 >filename.log,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33709533/
POSIX 系统公开了一系列 exec 函数,这些函数允许将可能不同的东西加载到当前进程中,保持打开的文件描述符、进程标识符等。 这可以出于多种原因完成,在我的例子中,这是自举——我想更改我自己进程的
我有一个用 c 编写的程序,它使用 execvpe(3) 函数,并且我设置了一行以包含必需的头文件: #include 我使用以下命令编译此文件... gcc foo.c -o foo ...仅收到
我在 C 中有一个函数,它创建一个子进程并使其运行 execvp。 int Execute(char **arg) { pid_t pid; int status; if ((
我正在使用 C 编写一个非常基本的 UNIX shell。在这个项目中,我试图使用 fork() 和 execvp() 来执行实际的 shell命令。我遇到了一个问题,它似乎可以很好地处理具有单个参数
我无法获得使用 execvp 调用的程序的完整输出。在这里,我基本上是在尝试用一个可以过滤输入和输出的程序来包装任何程序: #define BUF_SIZ 8192 int main(int argc
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在尝试用 c 语言创建一个 shell,当前正在尝试 ls 命令,但子进程总是返回 execvp 无法工作。这是函数。首先,我使用 exec 获取我所在的路径和从 stdin 获取的命令,这就是为
我正在开发这个程序,使用系统调用 execvp() 和 fork() 来运行通过命令行参数给出的 shell 命令。这里 arglist 是一个二维数组,其中包含命令名称及其参数列表。我将命令名称作为
我尝试使用 execvp 创建一个作为参数给出的存储库。但我遇到了一些无法在以下代码中解决的段错误(核心转储): #include //mkdir #include #include //p
我正在尝试使用 execvp 对文件进行排序,这是我的代码。 char *argv1[]={ "sh", "-c", "sort input.txt > output.txt", NULL }; 问题
我正在c中测试execvpe(),我尝试了下面的代码,这导致错误“函数‘execvpe’的隐式声明在C99 [-Wimplicit-function-declaration]中无效”。 #includ
我有以下代码: int main(int argc, char *argv[]) { int i, a=9; int length = 0; c
我正在使用 execvp() 运行一些系统调用。程序对于有效命令非常有效,对于任何不存在的命令则失败,这是完美的。该程序是,当我在需要额外参数(如 cat)的命令上使用 execvp() 并且我不提供
用户将读取一行,我将保留第一个单词作为 execvp 的命令。 假设他将输入“cat file.txt” ...命令将为 cat 。但我不知道如何使用这个execvp(),我读了一些教程但仍然没有明白
我正在用 C 编程编写一个正在运行的 shell 脚本。我读过有关 exec 函数的内容,虽然不太了解,但我读过一个像这样使用 execvp 的示例 execvp(*argv, argv) ; /*
这是我的输入函数: int i,j,n,len,c; char *buffer = 0; size_t bufsize = 0; ssize_t characters;
我正在尝试编写一个程序,该程序将 fork ,然后打开一个文件并执行它。它应该执行的文件称为子文件,并且它已经被编译。当我输入 ./child 时,它就会运行。但是,当我运行该程序时,它不会执行子程序
所以我有一个 C 程序,它接受命令输入,但偶尔我似乎会遇到某种内存错误。 我如何读取输入: static void parseLine(char* commandLine, Sequence* seq
我尝试在终端中传递命令行参数“ls -l/user/myuser”并在主目录中执行 execvp。 但不知何故,当我调试代码时,它给了我这个错误。 int main(int argc, char *a
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是一名优秀的程序员,十分优秀!