Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 05-04-2004, 07:09 AM   #1
LQ Newbie
Registered: May 2004
Posts: 23

Rep: Reputation: 15
Question how to execute shell script with c code?

i have built c code (in a file coba.c) just like this :
int main()
char *apa="test";
int i;
return 0;

and i have built shell script in the same directory with coba.c like this (name is cobi):
echo $apa

i hope, the output is "test" in terminal by execute coba like ./coba. but there are no output. any body help me, please?
Old 05-04-2004, 10:14 AM   #2
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 36
#include <stdlib.h>
int main()
  char *apa="test";
  char tmp[256]={'\0'};
  sprintf(tmp,"./cobi '%s'",apa);
  return 0;
echo "in cobi"
echo $1
exit 0
Old 05-04-2004, 03:41 PM   #3
Registered: Jan 2003
Posts: 56

Rep: Reputation: 15
Really off topic, but this answered my question, partially. Wanted to know how to do it in c++, but guess this will work too. Another question though, once you run it, is there a way you can get its process ID and put that in a file? Also, jim don't you also goto vbforums?

Last edited by dsheller; 05-04-2004 at 03:42 PM.
Old 05-04-2004, 03:54 PM   #4
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Yes, I post at VBF.

You can call fork() and execlp(), for example, to run a script and get it's return status. This invokes the POSIX shell in a child process. Getting return values are not straightforward. You need to read the man page for execlp. fork() returns a pid
pid_t pid;
if (pid == 0 ){
 /* you are in the child process*/
     execlp(something or other);
/* you are still in the parent process */
    printf("child pid=%d\n",pid);
system() provides a wrapper for this. It does return the exit status of the command interpreter it invokes, but the status is returned the way wait() returns a status.


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 10:30 PM
cannot execute script file from shell shahrahulb Linux - General 4 03-04-2008 08:02 AM
auto execute shell script ykirankumar Linux - Software 3 09-22-2004 08:23 AM
shell script won't execute brandnewbie Linux - Newbie 7 08-10-2004 02:30 PM
shell script how to execute x2000koh Programming 6 07-29-2003 05:20 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:38 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration