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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
06-01-2004, 08:36 PM
|
#1
|
|
LQ Newbie
Registered: May 2004
Posts: 23
Rep:
|
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
|
|
|
|
06-01-2004, 09:37 PM
|
#2
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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.
|
06-01-2004, 09:43 PM
|
#3
|
|
Member
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110
Rep:
|
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).
|
|
|
|
02-02-2006, 04:34 PM
|
#5
|
|
Member
Registered: May 2002
Posts: 964
Rep:
|
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
|
|
|
|
10-26-2010, 12:46 PM
|
#6
|
|
LQ Newbie
Registered: Oct 2010
Posts: 2
Rep:
|
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.
|
|
|
|
10-26-2010, 09:30 PM
|
#7
|
|
Senior Member
Registered: May 2005
Posts: 4,392
|
Quote:
Originally Posted by ibrahimhab
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 |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:49 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|