在 Windows 上运行带有 C++ 的 bash 脚本
Run a bash script with C++ on Windows
我正在尝试用C++编写一个程序,该程序可以在Windows上执行bash脚本,然后读取bash脚本的输出并将其存储到字符串或类似的东西中。这甚至可以在不 Windows 上安装任何额外软件的情况下实现吗?如果是这样,如何?
另外,如果我使用 Posix 库在 Linux 上编写程序,然后在 Linux 中交叉编译 Windows 的C++程序,然后将其移动到需要执行 bash 脚本的 Windows 上,它会起作用吗?
您可以使用 popen 函数。
FILE *fp;
fp = popen("bash script.sh", "r");
现在,您可以像读取文件一样读取输出。例:
char output[100];
fgets(output, sizeof(output), fp);
相关文章:
- 将bash参数传递给c++
- bash脚本在使用popen()时不返回代码以调用C ++程序
- 终端如何在 shell 提示符下以编程方式阻止退格,以便您无法在其上退格,例如 $ 表示 bash 或 C:\blah\>
- Bash 自动完成一个选项,而不运行应用程序
- 在 Windows 上运行带有 C++ 的 bash 脚本
- 使用 bash 脚本和测试处理进行代码编辑
- 主机箱/bash 未执行 docker 映像
- 如何在 bash 中使用管道在 c++ 中使用 getenv() 访问文件?
- 终端(bash)如何知道引发了什么异常
- -bash:/a.out:无法执行二进制文件:Exec格式错误
- 在 Bash 脚本中处理来自子进程的信号
- 将字符串变量从 bash 脚本传递到根宏
- 有没有办法在 C 或 C++ 中嵌入 Sh/Bash 会话?
- 如何将C++输出捕获到 Bash 变量?
- Bash run 命令在子壳的后台运行
- 从C++代码中以字符串形式运行多行 bash 脚本
- 在字符串流的 SSH 命令中转义 bash 脚本中的引号
- 如何使用 std::in 和 getline() 检测后台进程中从 bash 注入的输入
- Bash 脚本和命令行参数
- 从源代码安装 gcc: bash: gcc -v /usr/bin/gcc: 是一个目录