By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

executing shell commands from c?

P: n/a
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

Mar 19 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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.
Mar 19 '07 #2

P: n/a
"Kentor" <ke****@gmail.comwrites:
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_
Mar 19 '07 #3

P: n/a
>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().

Mar 19 '07 #4

P: n/a
"Kentor" <ke****@gmail.comwrote:
# 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.
Mar 19 '07 #5

P: n/a
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrites:
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
Mar 19 '07 #6

P: n/a
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
Mar 19 '07 #7

P: n/a
On Mar 18, 11:08 pm, SM Ryan <wyrm...@tango-sierra-oscar-foxtrot-
tango.fake.orgwrote:
"Kentor" <ken...@gmail.comwrote:

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

Mar 19 '07 #8

P: n/a
On Mar 18, 10:49 pm, "Kentor" <ken...@gmail.comwrote:
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 '07 #9

P: n/a
Ben Pfaff wrote:
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrites:
> 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

Mar 20 '07 #10

P: n/a
CBFalconer <cb********@yahoo.comwrites:
Ben Pfaff wrote:
>SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrites:
>> 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_
Mar 20 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.