LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to run a shell command containing awk, and grep within a C program (https://www.linuxquestions.org/questions/programming-9/how-to-run-a-shell-command-containing-awk-and-grep-within-a-c-program-63905/)

Linh 06-05-2003 06:38 PM

How to run a shell command containing awk, and grep within a C program
 
1) #include <stdio.h>
2) #include <stdlib.h>
3) int system(const char *string1);

4) int main()
5) {
6) char c;
7) char string1[] = "ifconfig";
8) system(string1);

9) char linux_command[] = "ifconfig eth0 | grep eth0 | awk '{print $5}'";
10) system(linux_command);

10) execlp ("bin/bash", "bash", "-c", linux_command, (char *) 0);
11) return(0);
12) }
========================================

1) Line 7 and 8 worked. The ouput did get printed on the screen.
2) Line 9 and 10 do not worked, because awk, and grep are involved. How do I get it to work ?

3) On line 10, what is execlp ? How do I use it ? Is the syntax on line 10 is correct ?
I got line 10 example from a book, but it does not explain very clearly.

fancypiper 06-05-2003 06:52 PM

:tisk: Did you read the rules you agreed to? :study:

Linh 06-05-2003 07:00 PM

There is no vulgarity in my question. It is all C programming.

fancypiper 06-05-2003 07:05 PM

Quote:

# Do not post the same discussion in more than one forum. Duplicate discussions can be frustrating for other members. Try and pick the most relevant forum for your post. If you are unsure put it in Linux - General.


All times are GMT -5. The time now is 06:23 AM.