Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-01-2004, 08:36 PM   #1
LQ Newbie
Registered: May 2004
Posts: 23

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

i have c code like this :
int main()
char *str;
printf("myshell-> ");
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?

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

Rep: Reputation: 59
Changes are in red:

int main()
char str[256], buf[256];
printf("myshell-> ");
sprintf(buf, "/bin/sh -c %s", str);
return 0;
1 members found this post helpful.
Old 06-01-2004, 09:43 PM   #3
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
Registered: Dec 2005
Location: France
Distribution: Debian
Posts: 123

Rep: Reputation: 17

o If your OS is:
- *nix => you should use "fork" + one of the "exec"'s familly functions,
- win* => you should use "CreateProcess"

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

#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
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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
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: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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.


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 > Forums > Non-*NIX Forums > Programming

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

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