Read Value from shell command Linux

2020-05-06 08:28发布

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 ?

标签: c++ c linux shell
1条回答
Anthone
2楼-- · 2020-05-06 09:11

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.

查看更多
登录 后发表回答