0

Is there a easier way to read the output of a Linux Shell Command like

ifconfig | grep "inet Adresse" | grep -v 127.0.0.1 | awk '{print $2}' | awk -F":" '{print $2}'

than using popen()

fp = popen(command, "r");

while(fgets(line, PATH_MAX, fp)!=NULL)
{
      //someoperations
}

pclose(fp);

I need to get my info twice before and after some operations.

As my output is just one Line ?

alk
  • 69,737
  • 10
  • 105
  • 255
r4d1um
  • 528
  • 1
  • 9
  • 32
  • you can write a small wrapper function.. – Karoly Horvath Nov 22 '12 at 12:56
  • 4
    `popen` is simple, or Glib's `g_spawn_sync()` http://stackoverflow.com/questions/219138/get-command-output-in-pipe-c-for-linux , http://stackoverflow.com/questions/43116/how-can-i-run-an-external-program-from-c-and-parse-its-output – pce Nov 22 '12 at 12:59
  • 1
    This sounds like an [X-Y Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Is the question you meant to ask more like _"How can I get the external IP addresses of my computer using C++?"_ – Rook Nov 22 '12 at 13:10
  • No this command was just a sample command. – r4d1um Nov 22 '12 at 13:13
  • Fair enough. I'll just +1 what pce wrote, then! – Rook Nov 22 '12 at 13:15

1 Answers1

0

I have used popen() now, was the easiest way Thank you @pce

fp = popen(fullCommand, "r");
while(fgets(line, PATH_MAX, fp) != NULL);
pclose(fp);

line was type of

char line[PATH_MAX];

Thank all of you.

r4d1um
  • 528
  • 1
  • 9
  • 32