gpt4 book ai didi

c - c/popen 中的 bash 找不到我的脚本

转载 作者:行者123 更新时间:2023-11-30 16:58:20 24 4
gpt4 key购买 nike

我尝试使用 popen 从 C 程序执行我的 bash 脚本 (tes02.sh)。但是当我运行程序时,我收到消息:./tes02.sh:找不到这是程序:

 #include <stdio.h>

#define LINE_BUFSIZE 128

int main(int argc, char *argv[])
{
char line[LINE_BUFSIZE];
int linenr;
FILE *pipe;

/* Get a pipe where the output from the scripts comes in */
pipe = popen("./tes02.sh", "r");
if (pipe == NULL) { /* check for errors */
perror(argv[0]); /* report error message */
return 1; /* return with exit code indicating error */
}

/* Read script output from the pipe line by line */
linenr = 1;
while (fgets(line, LINE_BUFSIZE, pipe) != NULL) {
printf("Script output line %d: %s", linenr, line);
++linenr;
}

/* Once here, out of the loop, the script has ended. */
pclose(pipe); /* Close the pipe */
return 0; /* return with exit code indicating success. */
}

我的脚本的目录是:/home/pi我应该进入目录吗?如果是的话,请问我该怎么做...谢谢

最佳答案

我会首先检查该文件是否存在于您的当前目录中:

if ( 0 != access( "./tes02.sh", F_OK ) )
{
fprintf ( stderr, "File tes02.sh does NOT exist in curdir\n" );
return -1; // return code - normally I return negative codes for errors
}
/* Get a pipe where the output from the scripts comes in */
pipe = popen("./tes02.sh", "r");
if (pipe == NULL) { /* check for errors */
perror(argv[0]); /* report error message */
return 1; /* return with exit code indicating error */
}

(...程序的其余部分...)

关于c - c/popen 中的 bash 找不到我的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38952729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com