嗨,我想获得有关从我应该从 excl 调用中执行的脚本获取输出的建议:
void getCurrentFSLSMode(char* const in_fsls_directory, enum Mode* out_mode) {
int link[2];
pid_t pid;
char buffer[MAX_PATH_STR_LENGTH];
char command_buffer[MAX_COMMAND_STR_LENGTH];
char script_name[] = "/scpt.sh\0";
if (pipe(link)==-1)
printf("pipe");
if ((pid = fork()) == -1)
printf("fork");
if(pid == 0) {
dup2 (link[1], STDOUT_FILENO);
close(link[0]);
close(link[1]);
command_buffer[0] = '\0';
strcat(command_buffer, in_fsls_directory);
strcat(command_buffer, script_name);
printf("%s\n", command_buffer);
execl("/bin/sh", "sh", command_buffer, (char *)0);
} else {
close(link[1]);
int nbytes = read(link[0], buffer, sizeof(buffer));
buffer[nbytes - 1] = '\0';
// LAZY COMPARISON
// not algorithm name, it's just what im doing.
//
printf("DBG : %s %d\n", buffer, nbytes);
if (buffer[0] == 'G') {
*out_mode = K;
} else if (buffer[0] == 'K') {
*out_mode = G;
} else {
*out_mode = CLEARED;
}
wait(NULL);
}
}
如果我在运行二进制文件时使用相同的代码,我可以读出它的输出,但是,当我尝试执行上述脚本时,我只能读取发出的命令。
例子:
excl("binary command to run");
read(output)
output == "correct output"
但如果
excl("shell script to run");
read(output)
output == "shell script to run"
为什么是这样?