LinuxQuestions.org
Review your favorite Linux distribution.
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 06-01-2004, 08:36 PM   #1
khucinx
LQ Newbie
 
Registered: May 2004
Posts: 23

Rep: Reputation: 15
execute shell command inside of c code?


i have c code like this :
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *str;
printf("myshell-> ");
scanf("%s",str);
system("/bin/sh -c str");
return 0;
}
when i execute it, there is something error :
myshell-> ls
/bin/sh: line 1: str: command not found
Segmentation fault

so, what should i do?

thank's
 
Old 06-01-2004, 09:37 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Changes are in red:

#include<stdio.h>
#include<stdlib.h>
int main()
{
char str[256], buf[256];
printf("myshell-> ");
scanf("%s",str);
sprintf(buf, "/bin/sh -c %s", str);
system(buf);
return 0;
}
 
1 members found this post helpful.
Old 06-01-2004, 09:43 PM   #3
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Rep: Reputation: 15
I think on most linux systems,
the execve() or execvp() will work (try 'man execve' or 'man execvp').

I believe that you have to fork a child process to call these functions in, though.

also don't forget to allocate memory to str (which may be the
cause of segmentation fault).
 
Old 02-02-2006, 06:37 AM   #4
leamassiot
Member
 
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Rep: Reputation: 17
Post

o If your OS is:
- *nix => you should use "fork" + one of the "exec"'s familly functions,
fork: http://www.opengroup.org/onlinepubs/009695399/
exec*: http://www.opengroup.org/onlinepubs/009695399/
- win* => you should use "CreateProcess"
CreateProcess: http://msdn.microsoft.com/library/de...ateprocess.asp

o To build the command line you can also try (I do not know if the code works):

Code:
#include <string.h>
...

char * maf()
{
  char s_begin[20]="/bin/sh -c ";
  char s_end[256];

  scanf("%s", str);
  return strcat(s_begin, s_end);
};
...
 
Old 02-02-2006, 04:34 PM   #5
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
If you need to communicate with the script - read output or send commands to it, then consider popen() in stdio which is a simple pipe interface
 
Old 10-26-2010, 12:46 PM   #6
ibrahimhab
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Rep: Reputation: 0
Unhappy execute shell command with arguments

execute shell command with arguments
with c languge (using strtok)
can help me pleze??????????

Last edited by ibrahimhab; 10-26-2010 at 12:48 PM.
 
Old 10-26-2010, 09:30 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by ibrahimhab View Post
execute shell command with arguments
with c languge (using strtok)
can help me pleze??????????
Yes, RTFM first:

man 3 strtok
.

If you have specific questions, ask them.
 
1 members found this post helpful.
  


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
very new to C, how to execute command inside C? neurotic85 Programming 5 09-05-2005 08:19 AM
execute shell command inside of tcl file khucinx Programming 1 07-05-2004 01:29 PM
run shell command inside of c code? khucinx Programming 2 05-17-2004 10:04 AM
how to execute shell script with c code? khucinx Programming 3 05-04-2004 02:54 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 02:26 PM.

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