LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
LinkBack Search this Thread
Old 05-04-2004, 06:09 AM   #1
khucinx
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 :
#include<unistd.h>
int main()
{
char *apa="test";
int i;
i=execv("cobi",apa);
return 0;
}

and i have built shell script in the same directory with coba.c like this (name is cobi):
#!/bin/sh
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, 09:14 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
try:
Code:
#include <stdlib.h>
int main()
{
  char *apa="test";
  char tmp[256]={'\0'};
  sprintf(tmp,"./cobi '%s'",apa);
  system(tmp);  
  return 0;
}
Code:
#!/bin/sh  
echo "in cobi"
echo $1
exit 0
 
Old 05-04-2004, 02:41 PM   #3
dsheller
Member
 
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 02:42 PM.
 
Old 05-04-2004, 02:54 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
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
Code:
pid_t pid;
pid=fork();
if (pid == 0 ){
 /* you are in the child process*/
     execlp(something or other);
}else{
/* 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.
 
  


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


All times are GMT -5. The time now is 11:32 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