char* cmd[] = { "awk", "{ printf "%-10s %10s %10s %2s %2s \t%s \n", $3,$5,$6,$7,$8,$9 }", NULL};
execvp(cmd[0], cmd);
perror("execvp of awk failed");

我正在为程序使用管道。我想使用execvp在C中执行此命令:
ls -l | awk '{ printf "%-10s %10s %10s %2s %2s \t%s \n", $3, $5,$6,$7,$8,$9 }'

问题是在这种情况下我找不到printf的正确语法。

谁能指出我的错误?

最佳答案

您需要在命令中转义所有反斜杠和双引号:

char* cmd[] = {
    "awk",
    "{ printf \"%-10s %10s %10s %2s %2s \\t%s \\n\", $3, $5, $6, $7, $8, $9 }",
    NULL
};

10-08 00:41