Find the answer to your Linux question:
Results 1 to 3 of 3
hello to all, im working with debian sarge 3.1, and need do 2 programs, one is a C program and the other is Shell program. My problem is the next: ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Posts
    10

    How to give parameters from C to shell ??

    hello to all, im working with debian sarge 3.1, and need do 2 programs, one is a C program and the other is Shell program. My problem is the next: i need to guive parameters from C program to shell program, for example:

    i have this program shell:

    #!/bin/bash

    num=$1

    echo "The number is $1"

    exit 0

    with the shell i need get parameters strings or integers.

    and the C program is:

    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {

    FILE *f=null;

    int a=23;
    char script[100];

    sprintf(script,"/home/user/script.sh %d",a);
    f=popen(script,"r");
    system("exec /home/user/script.sh");
    if (f==null)
    {
    perror("it cant find program script.sh");
    exit(-1);
    }
    pclose(f);
    return 0;
    }

    but, when the shell is executed only print "the number is", it cant get the parameter, and the program C create other new file with name "script.sh 23".

    i need that my shell print "The number is 23".

    well i hope that can help me and thanks =)

  2. #2
    Just Joined!
    Join Date
    Aug 2007
    Posts
    37
    Your shell script is okay, but I think the C program should be more like:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int a=23;
        char script[100];
    
        sprintf(script,"/home/sand/script.sh %d",a);
        system(script);
    
        return 0;
    }

  3. #3
    Just Joined!
    Join Date
    Sep 2007
    Posts
    10
    OMG!! thats really true

    i can get parameters with my shell *0* ty a lot joziba this problem is solve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...