Calling a shell script from a C program


 
Thread Tools Search this Thread
Top Forums Programming Calling a shell script from a C program
# 1  
Old 07-09-2010
Calling a shell script from a C program

Hi,

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.

Please let me know how this can be done.

Thanks
# 2  
Old 07-09-2010
Start it using popen, and read from the returned file descriptor as you would from any other.
# 3  
Old 07-09-2010
Hi pludi,

can you show me how it's done with a sample snippet please?
# 4  
Old 07-09-2010
hello

You need system() function to call scripts or other commands. you can use "wc" command to count the number of records returned by it.

Regards,
Gaurav.
# 5  
Old 07-09-2010
Quick example, will copy the contents of the files given on the command line to stdout, prefixed by the filename and the line number (yes, it's UUOC):
Code:
#include <stdio.h>

int main(int argc, char **argv)
{
    FILE *pipe = NULL;
    int i;
    char buffer[1024];

    if (argc == 1) {
        return 1;
    }
    for (i = 1; i < argc; i++) {
        sprintf(buffer, "cat -n %s", argv[i]);
        if ((pipe = popen(buffer, "r")) == NULL) {
            perror("popen");
            return 1;
        }
        while (fgets(buffer, 1024, pipe) != NULL)
            printf("%s: %s", argv[i], buffer);
        if (pclose(pipe) == -1) {
            perror("pclose");
            return 1;
        }
    }
    return 0;
}

# 6  
Old 07-12-2010
Hi Gaurav,

below is the system call that I have used to call my shell script named "test"

Code:
#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.

Can you help?
# 7  
Old 07-12-2010
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.
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 pass the environment name while calling java program from unix script?

Hi, I'm trying to test one unix shell script in dev environment. But I'm not sure how to pass the environment in my java program calling code. I'm trying to use -DconsumerEnv="DEV" but unfortunately I get 'null' while trying to print the value from java class. System.out.println("Environment: "+... (4 Replies)
Discussion started by: Pramit
4 Replies

2. Homework & Coursework Questions

Calling compiled C program with Perl program

Long story short: I'm working inside of a Unix SSH under a bash shell. I have to code a C program that generates a random number. Then I have to call the compiled C program with a Perl program to run the C program 20 times and put all the generated random #s into a text file, then print that text... (1 Reply)
Discussion started by: jdkirby
1 Replies

3. Shell Programming and Scripting

Calling perl script in shell program

How to call a perl script in shell program / shell scripting. PLS HELP ME (2 Replies)
Discussion started by: hravisankar
2 Replies

4. Shell Programming and Scripting

Calling a shell script from a C program

Hi, 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. Please let me know how this can be done. Thanks ... (0 Replies)
Discussion started by: swasid
0 Replies

5. UNIX for Dummies Questions & Answers

Calling a c program using perl script

On bash I run precompiled c Program as follows: ./create_cust 1 10000 US S > us_cust.csv create_cust is a c program and requires 4 parameters. I am redirecting the output of this program to csv file I need to run this same program in perl I am aware of exec command though not... (7 Replies)
Discussion started by: gkbond
7 Replies

6. Shell Programming and Scripting

Run shell script from C program by calling fork and execl

I need to write a c program that uses the fork and excel system calls to run the shell script mode invoked like this: "./mode 644 ls -l" (that is the argumetns will always be 644 ls -l) here's the mode script: #!/bin/sh octal="$1" shift find . -maxdepth 1 -perm $octal -exec $@ {} \; ... (3 Replies)
Discussion started by: computethis
3 Replies

7. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

8. Shell Programming and Scripting

calling a program (w/ params) from within shell

Hi all, I need to call some script (s1) from within my shell script (s2). s1 accepts parameters and I want to feed it with values of params from my script. I tried many things but none work (I am so much of a beginner), please help one of my attempts : . . . param1="hehe" param2="haha" ... (12 Replies)
Discussion started by: Lorna
12 Replies

9. Shell Programming and Scripting

Calling Functions of Other K Shell Program

Hi, I have a K shell a.ksh function abc { // Some logic } In b.ksh i have included the a.ksh ./a.ksh I want to call the abc function from this b.ksh script. Thanks Vijay (2 Replies)
Discussion started by: vijaykrc
2 Replies

10. Shell Programming and Scripting

Calling SHELL script from C program

Hi, I just tried to call a simple script from a pretty simple C program. I could not succeed :-( a message was thrown saying "sh: line 1: "Script name with path": Permission denied" The C program and shell script are below, both are in the same directory and shell script is given... (7 Replies)
Discussion started by: Chanakya.m
7 Replies
Login or Register to Ask a Question