LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 05-12-2004, 05:44 PM   #1
khucinx
LQ Newbie
 
Registered: May 2004
Posts: 23

Rep: Reputation: 15
Question c code vs shell script?


i have c code ( test.c ) like :

#include<stdio.h>
#include<unistd.h>
int main()
{
execl ("/bin/csh", "csh", "-c", "./coba", (char *)0);
printf("succes\n");
return 0;
}

in the same directory, i have shell script (coba) too :

#!/bin/sh
ls

when i run c code ( by type ./test ), the output are file and directory :
ahmad/
test.c
test
coba
but "succes" is not printed in terminal. how can i print "succes" from code above?
 
Old 05-12-2004, 06:06 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
execl() replaces the current process (which is your program) with the program passed to execl(). In your case /bin/csh.

So after execl() your program does not run anymore, because it was replaced by /bin/csh. So the printf() line is never reached.

To solve this, use system() instead, with start a new process, then waits for it to exit, return to your program. Using system(), the program is even simpler:
Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
	 system("/bin/csh -c ./coba");
	 printf("succes\n");
	 return 0;
}
Note that:
#include <unistd.h> is replaced by
#include <stdlib.h>

Last edited by Hko; 05-12-2004 at 06:09 PM.
 
  


Reply



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
Can we hide the code of a Shell Script? kushalkoolwal Programming 35 03-30-2011 04:55 PM
new shell window by script or code r2d2 Programming 2 12-14-2005 01:59 PM
return value from shell script to c code? khucinx Programming 1 05-13-2004 03:43 PM
c code vs shell script again? khucinx Programming 1 05-12-2004 09:44 PM
how to execute shell script with c code? khucinx Programming 3 05-04-2004 02:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:53 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration