The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help needed to sort multiple columns in one file ahjiefreak UNIX for Dummies Questions & Answers 1 12-07-2007 02:50 AM
Help needed in removing intermediate segments from a pipe delimited segment file naren_0101bits Shell Programming and Scripting 3 12-03-2007 08:54 AM
Help needed to sort file inditopgun Shell Programming and Scripting 2 09-27-2007 03:40 PM
Sort - Help Needed Andysundar UNIX for Dummies Questions & Answers 4 01-22-2006 11:55 PM
popen and tar, please HELP! stef83 High Level Programming 4 05-25-2005 07:41 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 05-17-2007
Registered User
 

Join Date: Apr 2007
Posts: 75
help needed with sort. pipe and popen()

Suppose I want to find the number of uniq lines in a file. I use the following command:

sort file1 | uniq -c | wc -l

But if for some reason sort fails, the above command returns 0 as the answer.

Why would sort fail ? sort makes use of directories /tmp or /var/tmp to store temporary files. So what I did is I renamed the temp dirs. And thats how sort failed..

After renaming the temp dirs:

Quote:
%> sort file1 | uniq -c | wc -l
sort: can't open /var/tmp/stmAAAEVayZ4.00000001: No such file or directory
0

So sort fails, but notice the 0 which is returned as the result.

Now, I have a program and I use popen() to execute the sort | uniq |wc command. What I want is, if sort fails, I should be able to catch it. But my program is not able to do so, because of the 0 returned as the result.

I have:

Code:
FILE *fptr;

fptr = popen("sort file1 | uniq -c | wc -l" , "r");

if (fptr != NULL) {
     fscanf(fptr, "%lu", &uniqLines);
} else {
    //ERROR
}
So even if sort fails, popen() writes a 0 to the stream pointed by fptr and uniqLines is always 0.

Please help ! !! !!!
Reply With Quote
Forum Sponsor
  #2  
Old 05-17-2007
Registered User
 

Join Date: Apr 2007
Posts: 75
Sorry, I think this is more of a C programming question. Can you please move it there ?
Reply With Quote
  #3  
Old 05-17-2007
Registered User
 

Join Date: Apr 2007
Posts: 75
I guess what I am looking for is: When commands are piped, if any one command fails, the commands that follow should not execute...
Reply With Quote
  #4  
Old 05-17-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by the_learner
I guess what I am looking for is: When commands are piped, if any one command fails, the commands that follow should not execute...
No can do. Commands that are piped are all launched at the same time, so all those to the right have already been started before sort decides it's having a bad day.

You would need to execute the commands one after the other with intermediate files.
Reply With Quote
  #5  
Old 05-18-2007
Registered User
 

Join Date: Apr 2007
Posts: 75
Ok it seems that the status of each command in the pipe is available in the array PIPESTATUS.

So echo ${PIPESTATUS[0]} will give me the status of the first command.
echo ${PIPESTATUS[1]} will give me the status of the second command and so on.

BUT now the problem is that PIPESTATUS is only available in bash. Where as popen() opens an sh shell.

And so I cannot do this:

Quote:
fptr = popen("sort file1 | uniq -c | wc -l; echo ${PIPESTATUS[0] > some_file" , "r");
Reply With Quote
  #6  
Old 05-19-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
And what is the problem with doing

Code:
FILE *fp=popen("/usr/bin/bash -c \"..........\"","r");
?

(edit, bash not ksh)

Last edited by porter; 05-21-2007 at 04:25 PM.
Reply With Quote
  #7  
Old 05-21-2007
Registered User
 

Join Date: Apr 2007
Posts: 75
Hey, can you please tell me what does ksh -c do ? (or give some examples)

PIPESTATUS is only availablein bash and not in ksh.

I switched my shell to ksh and then executed:

/usr/bin/bash -c "echo ${PIPESTATUS[*]}" but that did not work.

Thanks porter !

(On 2nd thoughts it might not be a good thing to rely on a specific shell feature, hmm.. I guess I will have to write my own C program to implement PIPES (I am trying but its driving me crazyy, I shall post my queries soon :P )
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:09 AM.


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

Content Relevant URLs by vBSEO 3.2.0