终端命令的结果作为c ++文件的命令行参数

时间:2018-04-24 11:54:31

标签: linux shell ubuntu terminal

例如,如果我有这样的终端命令
$ hostname -I这将因此将您的IP打印为字符串 到终端。
我想使用此命令的结果作为我的c ++可执行文件的argv [1]参数。有什么方法可以做到吗

1 个答案:

答案 0 :(得分:2)

这个问题不够具体。然而...

1)如果要直接从C ++代码调用特定命令,请按照Holt建议的那样跟随How to execute a command and get output of command within C++ using POSIX?

2)如果要使用hostname -I的输出调用程序,请尝试:

./my_program $(hostname -I)

./my_program `hostname -I`
相关问题