c++ - system() 返回错误语法错误 : "(" unexpected

标签 c++ linux bash shell system

我想在我的 C++ 代码中使用一个使用 system() API 的命令。

命令是:

comm -23 <(apt-mark showmanual | sort -u) <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u)

有关该命令的更多详细信息,请使用 this link .

当我在 Ubuntu 64 位机器上使用终端时,此命令完美运行,但是当我在 C++ 代码中的 system("above command") API 中使用时,它显示错误,如下所示:

sh: 1: Syntax error: "(" unexpected

我正在 Google 上搜索并找到一个解决方案 ( C system function causes error 'sh: Syntax error: “(” unexpected ' ),但已接受的解决方案以另一种方式执行任务。这不是一个解决方案,其他解决方案不起作用。

每个人都解释了如何在 shell 脚本(.sh 文件)中删除此错误,因为所有解决方案可能都适用于 .sh,但它们不适用于 system() API。我无法找到如何在 C++ 代码 system() API 中消除此错误。

最佳答案

如上所述,进程替换为 <(...)是一个 Bash 功能,在 vanilla sh 中不可用。因此,您需要显式调用 Bash shell:

bash -c "comm -23 <(apt-mark showmanual | sort -u) <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u)"

关于c++ - system() 返回错误语法错误 : "(" unexpected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48076835/

相关文章:

c++ - "cout << cout"- 输出代表什么?

python - 编写并保存 python 脚本到 arduino yun RAM

linux - shebang 无法在 Linux 中运行 bash 脚本

linux - Bash for 循环语法解释

c++ - 添加方法来创建 C++ 类

c++ - 为什么只执行最后一个线程?

c++ - std::cout 在屏幕上显示十六进制而不是文本

linux - libipq 性能问题

linux - 无法从 Linux 服务器以 headless (headless)模式运行 Robot Framework 测试 - WebDriverException

bash - bash 脚本中期望包含特殊字符 2 部分