How to capturs system() output


 
Thread Tools Search this Thread
Top Forums Programming How to capturs system() output
# 1  
Old 05-03-2009
How to capturs system() output

Hi,
In my c program, i call the system("pgrep -c thttpd");
the return value is
//-- 0 - One or more processes matched the criteria.
//-- 1 - No process matched the criteria
but the output on the screen is 2.
how can i capture this output?
Thanks in advance
Alex
# 2  
Old 05-03-2009
It's not so easy

Hi!
You called system("...") in your C program. First of all, the returned value has to do with the exit status of the program run under system, not the output of the command!

Second: Every process (process is a running program) has it's standard output to send the output and a standard error to send error messages. In your case the messages possibly are error messages of "pgrep" process and are sent to your terminal (the default standard error chanel). To check thsi fact run your program as:

Code:
your_program >panos1 2>panos2

where your_program is the name of the executable, panos1 is a file to use as standard output and panos2 is a file to use as standard error. After running the above command, you probably have the messages in panos2 as this file is used as standard error for your executable and inherited to child processes too (e.g. pgrep run under system as a chile process).

Third: To capture the standard output (or the the standard error) of a process run as child of another process you have to use the pipe system calls or functions; see the manual pages in popen, pipe etc. It's not easy and some experience in the C language is needed.

Bye...
# 3  
Old 05-04-2009
Just call popen() - it does the piping for you.
Code:
#include <stdio.h>
int main()
{
    FILE *cmd=popen(("pgrep -c thttpd", "r");
    char result[24]={0x0};
    while (fgets(result, sizeof(result), cmd) !=NULL)
           printf("%s\n", result);
    pclose(cmd);
    return 0;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to capture system() function output in variable

How to capture system() function output in awk variable and the print that awk variable..... (8 Replies)
Discussion started by: bharat1211
8 Replies

2. Shell Programming and Scripting

Redirect system output to null in perl

Hi Guys, Please help me.. it is urgent. I am writing a perl script to capture command output and redirect it to a logfile.At the same i want to check the return code of the command and log it if the command is not succesful in my logfile.. Here is my code, it is working but system command inside... (2 Replies)
Discussion started by: sriramperumalla
2 Replies

3. Shell Programming and Scripting

Bash script show Kill system output

Hi we are calling kill -9 $pid command from bash script it gives below output, but we need to hide the output. i tried /dev/null but ni luck. is there any alternate way to schive this. ../kill_scr.sh: line 42: 1891 Killed /tmp/anr_rest_mul_wc.sh Soalris 10. ... (2 Replies)
Discussion started by: sachinbutala
2 Replies

4. Shell Programming and Scripting

System Output in to an Array or variable

hey guys in only new to scripting as such, but i have a problem. i want to take the output of a search i do in the command line to then be in a variable but only a certain part of the output. this this what im doing: -bash-2.05b$ ldapsearch -x '(dn:=dc)' dc|grep dc= # base... (1 Reply)
Discussion started by: jmorey
1 Replies

5. Shell Programming and Scripting

Capturing awk's system(cmd) output

Hi everybody, I am working on a bigger awk script in which one part is comparing the size of two files. I want to evaluate which file is bigger and then just save the bigger one. I got it all working except for the part where I want to figure out which file is bigger; the one awk is currently... (2 Replies)
Discussion started by: iMeal
2 Replies

6. Programming

[C language] system function print output when not expected.

Hi, I am new to C and have a little problem. I am not planning to be a C expert, but this would be nice to understand. The problem is that a 'system' call prints it output to stdout, when I do not expect this. This is the program: trial.c #include <ctype.h> #include <unistd.h>... (5 Replies)
Discussion started by: ejdv
5 Replies

7. Shell Programming and Scripting

perl: looping through the output of a 'system' command

Hi there could anybody point me in the right direction when it comes to looping through the output of a system command in perl (i.e. df -k) doing a test against each line to see if it matches? for example if i have a df -k output like this and I wanted to grab the lines that matched "sda" or... (3 Replies)
Discussion started by: rethink
3 Replies

8. Programming

C system() how to send the output to an array??

Hi, im in need if wisdom here guys... How could I store the output of commands like ping integrally directly into a array??? I'll be using system() to run the commands but how can I send the output to an array??? cuz I need to store it so that I can later copy the array's content to a buffer... (5 Replies)
Discussion started by: Jess83
5 Replies

9. Shell Programming and Scripting

system () output into file in awk

hello, I want to print my output into a file inside of awk, but I don't know it could wokr with using system (piping the $1-4 to another shellskript): cat file.txt |awk '{ if ($5==2) {dataname=$1 "_" $2 "_" $3 "_" $4 "_typing.rad" befehl=".gen_test " $7 " " $8 " " $8 system(befehl) >... (5 Replies)
Discussion started by: ergy1983
5 Replies

10. Shell Programming and Scripting

parsing output of system()

Hi, From the above output of df command I need to parse only 43G (available space) and display it. root@localhost:> df -h /vol1/joy Filesystem Size Used Avail Capacity Mounted on /vol1/joy 180G 137G 43G 76% ... (3 Replies)
Discussion started by: cjjoy
3 Replies
Login or Register to Ask a Question