LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-17-2004, 02:04 AM   #1
khucinx
LQ Newbie
 
Registered: May 2004
Posts: 23

Rep: Reputation: 15
Question run shell command inside of c code?


in one directory, i have cpc.c and cource file

cpc.c is :
#include<stdio.h>
#include<stdlib.h>
main()
{
system("/bin/sh -c cp source dest");
}

i want copy source file to dest file, but there is no dest file.
when i execute cpc by type ./cpc, there is something error like "cp: missing file arguments".

how about that?

thank's
 
Old 05-17-2004, 02:24 AM   #2
pragti
LQ Newbie
 
Registered: Feb 2004
Posts: 16

Rep: Reputation: 0
Hi
The correct prog is following
cpc.c is :
#include<stdio.h>
#include<stdlib.h>
main()
{
system("/bin/sh \"-c cp source dest\"");
}

as -c option take the single input string as argument in shell.so multiple strings must be quoted
 
Old 05-17-2004, 10:04 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Why so difficult?
According to "man system":
Quote:
system() executes a command specified in string by calling /bin/sh -c string, and returns after the command has been completed.
So if system() already will start "sh -c" for you, why start another one?
Code:
#include <stdlib.h>

int main()
{
        system("cp source dest");
        return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
execute shell command inside of c code? khucinx Programming 6 10-26-2010 09:30 PM
Problem executing mv command inside shell script pablogosse Linux - General 7 12-16-2009 12:21 PM
execute shell command inside of tcl file khucinx Programming 1 07-05-2004 01:29 PM
how do I run a command from inside a c++ program? exodist Programming 1 04-06-2004 04:34 PM
how to invoke a linux command inside a java code ? kusum Linux - Software 2 11-23-2003 01:19 PM


All times are GMT -5. The time now is 03:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration