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
:
ls -l | awk '{ printf "%-10s %10s %10s %2s %2s \t%s \n", $3, $5,$6,$7,$8,$9 }'
问题是在这种情况下我无法找到printf
的正确语法。
有谁可以指出我的错误是什么?
答案 0 :(得分:3)
您需要在命令中转义所有反斜杠和双引号:
char* cmd[] = {
"awk",
"{ printf \"%-10s %10s %10s %2s %2s \\t%s \\n\", $3, $5, $6, $7, $8, $9 }",
NULL
};