Passing arguments to script.....

This is a discussion on Passing arguments to script..... within the C Programming forums, part of the General Programming Boards category; Hi everyone, Does anyone have any idea how to pass arguments to a script which is invoked from inside our ...

  1. #1
    suwie
    Join Date
    Aug 2004
    Posts
    6

    Passing arguments to script.....

    Hi everyone,

    Does anyone have any idea how to pass arguments to a script which is invoked from inside our C code....

    Example :

    I have a script ( e.g Tcl script )
    Usually i invoke the tcl script using tclsh file.tcl arg1 arg2...argN

    Now i am trying to call this script from a C program.
    I have tried :
    - system(""); but this doesn't have the flexibility of customized arguments
    - i also try to use execl, execlp and other variance of exec...it either called the script and terminated directly or doesn't call the script at all...

    Anybody know if there is other methods?

    Thank you.

    Happy Coding....

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    You want to run

    tclsh file.tcl arg1 arg2...argN

    from a C program?
    and pass arg1 .. argN from the argv[] main gets when you invoke a C program?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    suwie
    Join Date
    Aug 2004
    Posts
    6
    what i mean is,
    if i usually run the tcl script using tclsh hello.tcl arg1....argN

    Then i need to run the script using a C program as an intermediate....

    Usually what we done is by using execl, execlp...and all those variants...or maybe use system("tclsh hello.tcl");

    I have tried using all those above.....but seems like execlp is not a good choice...the better one is system(""), but i can't provide a customize arguments....

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    have you tried to use sprintf?
    Code:
    char szCmdLine[MAX_PATH];
    int iArg1 = 1, iArg2 = 2, iArg3 = 3;
    
    sprintf(szCmdLine, "tclsh hello.tcl %d %d %d", iArg1, iArg2, iArg3);
    system(szCmdLine);

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    Well if you want easy, then use a shell script - it's just one line
    Code:
    #!/bin/sh
    /usr/bin/tclsh hello.tcl $*
    But if you want a C program, try this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /*
     * Given the invocation
     * ./a.out a b c
     * argc is 4, and the number of elements in argv is 5 (argv[argc] is NULL)
     * we need to stretch this out by 1 parameter
     * /usr/bin/tclsh hello.tcl a b c
     */
    int main ( int argc, char *argv[] ) {
        char    **newargv;
        int     i, j;
    
        /* space for an expanded argv */
        newargv = malloc( (argc+2) * sizeof *newargv );
    
        /* create a new argv */
        newargv[0] = "/usr/bin/tclsh";
        newargv[1] = "hello.tcl";
    
        for ( i = 2, j = 1 ; j < argc ; i++, j++ ) {
            newargv[i] = argv[j];
        }
        newargv[i] = NULL;
    
        /* run the new program, with the existing args */
        execv(newargv[0], newargv);
    
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    suwie
    Join Date
    Aug 2004
    Posts
    6
    Hi everyone.....

    wow.....how come i never think of the idea you guys write down...
    especially the sprintf.....

    Thank you very much......

    Happy coding....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arguments
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2009, 12:14 PM
  2. Passing arguments.
    By omnificient in forum C Programming
    Replies: 3
    Last Post: 05-28-2008, 02:41 PM
  3. Passing arguments between functions
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-17-2006, 04:59 PM
  4. passing arguments
    By Baard in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2003, 07:10 AM
  5. Game structure, any thoughts?
    By Vorok in forum Game Programming
    Replies: 2
    Last Post: 06-07-2003, 01:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21