![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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:
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
}
Please help ! !! !!! |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Sorry, I think this is more of a C programming question. Can you please move it there ?
|
|
#3
|
|||
|
|||
|
I guess what I am looking for is: When commands are piped, if any one command fails, the commands that follow should not execute...
|
|
#4
|
|||
|
|||
|
Quote:
You would need to execute the commands one after the other with intermediate files. |
|
#5
|
|||
|
|||
|
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:
|
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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 ) |
|||
| Google The UNIX and Linux Forums |