For example if i have a terminal command like this
$ hostname -I
this will as a result print your ip as a string
to the terminal.
I want to use the result of this command as the argv[1] argument for my c++ executable. Is there any way I can do that
Asked
Active
Viewed 51 times
0

shycha
- 440
- 5
- 13

Branko Simovic
- 1
- 2
-
@Holt, @Branko Simovic I think this question requires specifying before marking it as duplicated with: https://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-output-of-command-within-c-using-posix Do you want to know how to invoke command from the C++ code or how to redirect the output of `hostname -I` to your program as the first argument, like in: `./my_program $(hostname -I)`? – shycha Apr 24 '18 at 12:34
-
@shycha You're right, this is not at all a duplicate, I completely misread the end of the question... Seems clear to me now that what OP want is `./my_program $(hostname -I)`. – Holt Apr 24 '18 at 12:36
-
1Then it is off topic here and should be asked on [Ask Ubuntu](https://askubuntu.com/) or as it is generic on [Unix & Linux](https://unix.stackexchange.com/) – Serge Ballesta Apr 24 '18 at 12:40
-
Yes, your probably right. If so, I was too fast with answering. Can you/someone delete my answer? The tag is also wrong -- this is the reason I've read it. @Holt Besides I think I also noticed a small problem with commenting on possibly duplicated question. But I'll need to look for some duplicated question to confirm it. – shycha Apr 24 '18 at 12:58
1 Answers
2
The question is not specific enough. However...
1) If you want to invoke the particular command directly from the C++ code, then follow How to execute a command and get output of command within C++ using POSIX? as Holt suggested.
2) If you want to invoke your program with the output of hostname -I
, then try:
./my_program $(hostname -I)
or
./my_program `hostname -I`

shycha
- 440
- 5
- 13