Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > executing shell commands from c?

Reply
Thread Tools

executing shell commands from c?

 
 
Kentor
Guest
Posts: n/a
 
      03-19-2007
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

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      03-19-2007
"Kentor" <(E-Mail Removed)> 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_
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-19-2007
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.
 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      03-19-2007
>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().

 
Reply With Quote
 
SM Ryan
Guest
Posts: n/a
 
      03-19-2007
"Kentor" <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      03-19-2007
SM Ryan <(E-Mail Removed)> 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
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      03-19-2007
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
 
Reply With Quote
 
Kentor
Guest
Posts: n/a
 
      03-19-2007
On Mar 18, 11:08 pm, SM Ryan <wyrm...@tango-sierra-oscar-foxtrot-
tango.fake.org> wrote:
> "Kentor" <(E-Mail Removed)> 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.

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      03-19-2007
Ben Pfaff wrote:
> SM Ryan <(E-Mail Removed)> 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

 
Reply With Quote
 
muntyan@gmail.com
Guest
Posts: n/a
 
      03-19-2007
On Mar 18, 10:49 pm, "Kentor" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ruby executing shell commands Guilherme Viteri Ruby 2 09-08-2010 08:09 PM
Executing shell commands from Ruby... again. Kevin Skrenes Ruby 6 03-17-2007 09:00 AM
executing commands in the same shell in Windows using Perl Murugesh Perl Misc 5 12-07-2005 04:25 PM
executing shell commands in asp mlay ASP General 2 11-09-2003 03:25 PM
Re: man pages for C commands (GCC commands) Ben Pfaff C Programming 4 06-28-2003 06:21 PM



Advertisments