The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 11:00 AM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-19-2007 09:52 PM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 12:15 AM
"./cofigure" and "make" turmoil gnerd UNIX for Dummies Questions & Answers 16 02-23-2004 12:53 PM
how to request a "read" or "delivered" receipt for mails plelie2 Shell Programming and Scripting 1 08-06-2002 12:26 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-26-2006
Registered User
 

Join Date: Apr 2006
Posts: 2
Stumble this Post!
Unhappy how could i make a program mixed with many "|", "<" and ">"

I have written following code to do: ls -l | wc -w, it works:
but when there are not only a single "|", if there are more such as:
ls -l | sort -r | sort | sort -r, This program does not work, i want to know how could i deal with it when there are more "|", another situation is that, if it mixes "|" and "<" or ">", such as: cat < apa | wc | wc >h***, this is also complex, how could i deal with these? thank you very much

(The following is just for a single "|")
void pipeProcess(char *args1[], char *args2[]) {
int thepipe[2], pid;

if(pipe(thepipe) == -1)
oops("Can not get a pipe", 1);
/* Create a new process */
if((pid = fork()) == -1)
oops("Can not fork", 2);

if(pid >0) {/* This is parent process */
close(thepipe[1]); /* parent does not write to pipe */
if(dup2(thepipe[0], 0) == -1) /* let parent read interface attaches to stdin */
oops("Could not redirect stdin", 3);
close(thepipe[0]); /* close thepipe[0] interface because it has attached to stdin */
execvp(args2[0], args2);
oops(*args2, 4);
}

/* Child process executes av[1] and writesinto pipe */
close(thepipe[0]); /* Child does not read from pipe */
if(dup2(thepipe[1], 1) == -1)
oops("Could not redirect stdout", 4);
close(thepipe[1]);
execvp(args1[0], args1);
oops(*args1, 5);
}
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-28-2006
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,812
Stumble this Post!
Let me guess - you have to write your own shell for homework or a final project....
Reply With Quote
  #3 (permalink)  
Old 04-29-2006
Registered User
 

Join Date: Apr 2006
Posts: 2
Stumble this Post!
yes, but here i met this problem and i am thinking of a better method to deal with it
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0