为什么下面的 shell 命令在命令行中直接执行时有效,但在使用 popen/system 通过 C 程序执行时却无效?

Why is the following shell command working when executed directly in command line, but not working when executed through C program using popen/system?

命令是:ps -c -p | tr -s " " | cut -d " " -f 2,6-10,13 | grep 'R'

我正在 运行 通过 adb shell 设置它。基本上,我想要一个当前在 运行 队列中的进程列表(和某些参数)。如果我直接通过 shell.

运行 它工作正常

但是,如果我将它放入 C 程序中并在 Android 上将其交叉编译为 运行,则无法正常工作。只有 ps -c -p 在工作(我已经检查过了)。但是在运行宁这个ps -c -p | tr -s " " | cut -d " " -f 2,6-10,13 | grep 'R',我得到输出:

usage: tr [-cds] SET1 [SET2]

Translate, squeeze, or delete characters from stdin, writing to stdout

-c/-C  Take complement of SET1
-d     Delete input characters coded SET1
-s     Squeeze multiple output characters of SET2 into one character

tr: Needs 1 argument
usage: cut OPTION... [FILE]...

Print selected parts of lines from each FILE to standard output.

-b LIST select only these bytes from LIST.
-c LIST select only these characters from LIST.
-f LIST select only these fields.
-d DELIM    use DELIM instead of TAB for field delimiter.
-s  do not print lines not containing delimiters.
-n  don't split multibyte characters (Ignored).

cut: Needs -fcb

我认为 ps -c -p 的输出没有传送到 trtr 也没有传送到 cut。 能否请您提出问题所在?

这是我使用的代码:

    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <string.h>

    #define BUFSIZE 128

    int main(int argc,char **argv)
    {
        char *cmd4 = "ps -c -p | tr -s " " | cut -d " " -f 2,6-10,13 | grep 'R'";    
        system(cmd4);

        FILE *fp;


        char buf[BUFSIZE];
     // Another method
    if ((fp = popen(cmd4, "r")) == NULL) {
        printf("Error opening pipe4!\n");
        return -1;
    }

    while (fgets(buf, BUFSIZE, fp) != NULL) {
        // Do whatever you want here...
        printf("cmd 4 running!");
        printf("OUTPUT: %s", buf);
    }

    if(pclose(fp))  {
        printf("Command not found or exited with error status4\n");
        return -1;
    }

    return 0;
}

在 shell 中,您正在使用以下命令:

ps -c -p | tr -s " " | cut -d " " -f 2,6-10,13 | grep 'R'

在 C 中,您将以下内容传递给 system(然后传递给 popen):

ps -c -p | tr -s  | cut -d  -f 2,6-10,13 | grep 'R'

看出区别了吗?引号需要在 C 源代码中转义。另外,当你遇到这样的麻烦时,一定要输出相关数据,这样你就可以看到实际发生了什么,而不是你计划的。一个简单的 puts(cmd4) 会立即揭示这一点。