473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

executing shell commands from c?

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
10 1955
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
"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
>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
"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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Falk Schneider | last post by:
I wrote a PHP shell script under Linux which puts all existing PS-Files within a directory into a list and should then start a single Ghostview window for each file. Sounds simple, but it's not: ...
6
by: twsnnva | last post by:
Could anyone give me an example (code) of a simple program with a button that when clicked executes a linux shell or windows dos command like "ifconfig" or "ipconfig" and prints the output...
8
by: Siemel Naran | last post by:
Hi. I'm writing a command shell that reads commands from standard input. At this point I have the command in a std::string. Now I want to execute this command in the shell. From the Borland...
0
by: Bruceneedshelp | last post by:
My application starts logging and executes shell commands on startup. The application runs fine, except when it executes at boot time. In other words when I make it a "startup" application on my...
2
by: Mark | last post by:
hi, i've written a c++ program that converts one file type to another via two arguments (the input and output filenames). i want to execute this on my server, using something like ...
3
by: Ara Kooser | last post by:
Hello all, I hope I am posting this correctly. I am running Python 2.4.2 on Slackware 11.0 using IDLE. I am in the process of learning python so I am writing a text adventure game using...
1
by: Riccardo Maria Bianchi | last post by:
Hello! :) I'm trying to run shell commands both with os.system() and subprocess.Popen() class. But I can't run aliases or function defined in my .bashrc file, like in the login interactive...
1
by: raocheng | last post by:
Please see the following code. Suppose I have many shell commands to be executed. And I don't want to fork a sub shell for each command(eg: status,output = commands.getstatusoutput(cmd)) because...
5
by: inetquestion | last post by:
I am looking for a web interface for shell commands or shell scripts. Does anyone know of any exexisting php scripts which would solve this requirement? PHP form accepts input from a user, then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.