我应该创建一个使用C.下面是我的代码Linux命令创建Linux外壳:帮助,用C
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define SHELL "/bin/sh"
#include "extern.h"
int mysystem (char *command)
{
int status;
pid_t pid;
pid = fork();
if (pid == 0)
{
execl (SHELL, SHELL, "-c", command, NULL);
_exit (EXIT_FAILURE);
}
else if (pid < 0)
status = -1;
else
if (waitpid (pid, &status, 0) != pid)
status = -1;
return status;
}
一切都是正确的,当我测试使用如“ls”不同的命令代码, “人”,等等。但是当我使用记事本创建一个包含testfile的以下内容:
echo "hello"
exit 2
返回代码出来是512
当它应该只是2
。 任何人都可以帮助我解决我的代码?
谁能帮我编辑我的问题。有些东西缺失。 thx – Brian 2010-11-24 21:52:50
当你在回声“你好”之后加上分号时它会工作吗? – 2010-11-24 21:55:28
@Amir阿富汗尼:不,它不起作用 – Brian 2010-11-24 21:57:25