executing shell commands from c?

Discussion in 'C Programming' started by Kentor, Mar 19, 2007.

  1. Kentor

    Kentor Guest

    I know I can use the system function to execute commands, but I need
    to pass parameters to it and it doesn't want to accept them...
    is there a way to do system("mv %s temp123", myvar) ?
    or maybe there's another function that accepts parameters?
    thanks for your help.

    Simon
     
    Kentor, Mar 19, 2007
    #1
    1. Advertising

  2. Kentor

    Ben Pfaff Guest

    "Kentor" <> writes:

    > I know I can use the system function to execute commands, but I need
    > to pass parameters to it and it doesn't want to accept them...
    > is there a way to do system("mv %s temp123", myvar) ?


    Use a function such as sprintf to construct the command in a
    temporary string, then pass that string to system().
    --
    "When in doubt, treat ``feature'' as a pejorative.
    (Think of a hundred-bladed Swiss army knife.)"
    --Kernighan and Plauger, _Software Tools_
     
    Ben Pfaff, Mar 19, 2007
    #2
    1. Advertising

  3. Kentor

    Ian Collins Guest

    Kentor wrote:
    > I know I can use the system function to execute commands, but I need
    > to pass parameters to it and it doesn't want to accept them...
    > is there a way to do system("mv %s temp123", myvar) ?
    > or maybe there's another function that accepts parameters?


    Build the string you want with sprintf() and pass it to system().

    --
    Ian Collins.
     
    Ian Collins, Mar 19, 2007
    #3
  4. >I know I can use the system function to execute commands, but I need
    >to pass parameters to it and it doesn't want to accept them...


    Generate a string that contains the command you wish to execute,
    including all the parameters. It does not have to be a string
    literal. Then pass it to system().

    >is there a way to do system("mv %s temp123", myvar) ?


    No, but take a look at the sprintf() function, and consider what
    happens if you pass the resulting string to system(). There are
    lots of ways to construct strings in C other than sprintf(), but
    sprintf() is often convenient for this purpose. Beware that it's
    YOUR problem to allocate a sufficiently long buffer to hold the
    command, either with a character array, or with malloc().
     
    Gordon Burditt, Mar 19, 2007
    #4
  5. Kentor

    SM Ryan Guest

    "Kentor" <> wrote:
    # I know I can use the system function to execute commands, but I need
    # to pass parameters to it and it doesn't want to accept them...
    # is there a way to do system("mv %s temp123", myvar) ?
    # or maybe there's another function that accepts parameters?
    # thanks for your help.

    More specifically, you can do things like

    static char F[] = "mv %s temp123";
    char f[sizeof F+strlen(myvar)+1];
    sprintf(f,F,myvar);
    int rc = system(f);

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    I have no idea what you just said.
    I get that alot.
     
    SM Ryan, Mar 19, 2007
    #5
  6. Kentor

    Ben Pfaff Guest

    SM Ryan <> writes:

    > static char F[] = "mv %s temp123";
    > char f[sizeof F+strlen(myvar)+1];
    > sprintf(f,F,myvar);


    sizeof F includes F's null terminator in its value, so you don't
    have to add an additional 1 to the size of f. (You could, in
    fact, subtract 2, because f will not include %s.) I can see why
    you'd want to be conservative, though.

    Also, this is not valid C89, because the size of f is not a
    constant expression.
    --
    "You call this a *C* question? What the hell are you smoking?" --Kaz
     
    Ben Pfaff, Mar 19, 2007
    #6
  7. Kentor wrote:
    > I know I can use the system function to execute commands, but I need
    > to pass parameters to it and it doesn't want to accept them...
    > is there a way to do system("mv %s temp123", myvar) ?
    > or maybe there's another function that accepts parameters?
    > thanks for your help.


    Build the command string with sprintf (obviously other ways are available),
    Use the built command string as the argument for system
     
    Martin Ambuhl, Mar 19, 2007
    #7
  8. Kentor

    Kentor Guest

    On Mar 18, 11:08 pm, SM Ryan <wyrm...@tango-sierra-oscar-foxtrot-
    tango.fake.org> wrote:
    > "Kentor" <> wrote:
    >
    > # I know I can use the system function to execute commands, but I need
    > # to pass parameters to it and it doesn't want to accept them...
    > # is there a way to do system("mv %s temp123", myvar) ?
    > # or maybe there's another function that accepts parameters?
    > # thanks for your help.
    >
    > More specifically, you can do things like
    >
    > static char F[] = "mv %s temp123";
    > char f[sizeof F+strlen(myvar)+1];
    > sprintf(f,F,myvar);
    > int rc = system(f);
    >
    > --
    > SM Ryanhttp://www.rawbw.com/~wyrmwif/
    > I have no idea what you just said.
    > I get that alot.


    Great thanks everyone, and thanks even more for the example Ryan.
     
    Kentor, Mar 19, 2007
    #8
  9. Kentor

    CBFalconer Guest

    Ben Pfaff wrote:
    > SM Ryan <> writes:
    >
    >> static char F[] = "mv %s temp123";
    >> char f[sizeof F+strlen(myvar)+1];
    >> sprintf(f,F,myvar);

    >
    > sizeof F includes F's null terminator in its value, so you don't
    > have to add an additional 1 to the size of f. (You could, in
    > fact, subtract 2, because f will not include %s.) I can see why
    > you'd want to be conservative, though.
    >
    > Also, this is not valid C89, because the size of f is not a
    > constant expression.


    ITYM strlen(myvar) is not a constant expression.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>

    "A man who is right every time is not likely to do very much."
    -- Francis Crick, co-discover of DNA
    "There is nothing more amazing than stupidity in action."
    -- Thomas Matthews


    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Mar 19, 2007
    #9
  10. Kentor

    Guest

    On Mar 18, 10:49 pm, "Kentor" <> wrote:
    > I know I can use the system function to execute commands, but I need
    > to pass parameters to it and it doesn't want to accept them...
    > is there a way to do system("mv %s temp123", myvar) ?
    > or maybe there's another function that accepts parameters?


    In addition to creating that string make sure myvar makes
    sense, learn shell quoting rules, and use '--' argument for
    mv.

    Best regards,
    Yevgen
     
    , Mar 19, 2007
    #10
  11. Kentor

    Ben Pfaff Guest

    CBFalconer <> writes:

    > Ben Pfaff wrote:
    >> SM Ryan <> writes:
    >>
    >>> static char F[] = "mv %s temp123";
    >>> char f[sizeof F+strlen(myvar)+1];

    >>
    >> Also, this is not valid C89, because the size of f is not a
    >> constant expression.

    >
    > ITYM strlen(myvar) is not a constant expression.


    That's what I said: the expression inside the brackets is the
    size of f.
    --
    "...what folly I commit, I dedicate to you."
    --William Shakespeare, _Troilus and Cressida_
     
    Ben Pfaff, Mar 20, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    3,999
    Thomas Stegen
    Jun 28, 2003
  2. mlay

    executing shell commands in asp

    mlay, Nov 9, 2003, in forum: ASP General
    Replies:
    2
    Views:
    193
    Aaron Bertrand [MVP]
    Nov 9, 2003
  3. Kevin Skrenes
    Replies:
    6
    Views:
    233
    Robert Klemme
    Mar 17, 2007
  4. Guilherme Viteri

    Ruby executing shell commands

    Guilherme Viteri, Sep 8, 2010, in forum: Ruby
    Replies:
    2
    Views:
    232
    Roger Pack
    Sep 8, 2010
  5. Murugesh
    Replies:
    5
    Views:
    193
Loading...

Share This Page