I have a shell script which connects to a database and fetches the count of the records from a table. I want to embed this whole script in a C program. Also the count fetched should be available in the C program for further usage.
below is the system call that I have used to call my shell script named "test"
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
i=system("./test 1234");
printf("i=%d\n",i);
}
Here "test" is the shell script that is fetching the count from a table for a particular column("1234") that I am passing as the parameter. When the "test" script alone is run, it is returning the correct count but when trying to check through the C program it's returning some different value. How can I get the exact value from the script in my program above? wc is returning 1 as it's a single line output but I want the actual count value.
Because system() does not return the output of a script, but only the exit status. Eg. if instead of "./test" you'd run /bin/true (or /usr/bin/true), you'd always get 0, while with /bin/false you'd always get 1. If you want to get the output of a command, either use a temporary file, or popen as in the example I've given.
thanks for the clarification pludi. so, does popen only take a command or can an entire script be passed in. can you show me how my test script fits in your example please?
Also, please make it a habit of reading the man pages first when available, experiment a bit for yourself, and ask specific questions. We're here to help you, not to do your work for you.