LinuxQuestions.org
Help answer threads with 0 replies.
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-10-2004, 08:50 PM   #1
khucinx
LQ Newbie
 
Registered: May 2004
Posts: 23

Rep: Reputation: 15
Question runing shell script


i have c code that execute shell script ( coba ) like
this :

.
.
execl("/bin/csh","csh","-c","./coba",(char *)0);
printf("he");
.
.

i want to print "he", but i can't because execl.
everyone, how to execute printf("he") after execute
execl, please?
 
Old 05-11-2004, 11:45 AM   #2
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Here, this seams to work:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

pid_t NewProcess(char *file, char **args)
{
	pid_t ForkPid;
	ForkPid=fork();
	if (ForkPid==0)
	{
		fprintf(stderr, "Ya baby!\n");
		//execv(file, args);
		system("cat test.cpp > test.txt");
		fprintf(stderr, "Done!\n");
		exit(0);
	} 
	else if (ForkPid<0)
		return -1;
	else 
		return ForkPid;
return ForkPid;
}

int main(int argc, char **argv)
{
	pid_t Pid;

	if (argc<2)
	{
		fprintf(stderr, "I need at least one argument!\n");
		return 1;
	}
	fprintf(stderr, "Starting New Process: %s\n", argv[1]);
	Pid=NewProcess(argv[1], &argv[2]);
	fprintf(stderr, "Process Created: %d!\n", Pid);
return 0;
}
The output is as follows:
Code:
[nerd@The_Nerd programming]$ g++ -O3 -o test test.cpp
[nerd@The_Nerd programming]$ ./test dsf
Starting New Process: dsf
Process Created: 1885!
[nerd@The_Nerd programming]$ Ya baby!
Done!

[nerd@The_Nerd programming]$
The line:
[nerd@The_Nerd programming]$ ./test dsf

Is where I start the program, the dsf is because origionally in my testing I was using the first arg as a program name, however this changed and I didn't take out the if statement to test for the arg, so I just typed some random junk so the program would run.

Output:

Starting New Process: dsf <---- This is where it is starting a new proccess
Process Created: 1885! <---- Finished (this is the same as printf("he"); )
<---- Program terminates
[nerd@The_Nerd programming]$ Ya baby! <----- This is our new proccess (cat test.cpp > test.txt)
Done! <---- Proccess tirminates

Hope that helps!
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
runing login script with root privilleges djgerbavore Linux - Networking 8 06-16-2005 02:36 PM
runing perl unix script in windows 2000 anirudh Programming 3 04-15-2005 10:07 AM

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

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