help needed with sort. pipe and popen()


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers help needed with sort. pipe and popen()
# 1  
Old 05-17-2007
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. Smilie

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 ! !! !!!
# 2  
Old 05-17-2007
Sorry, I think this is more of a C programming question. Can you please move it there ?
# 3  
Old 05-17-2007
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  
Old 05-17-2007
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.
# 5  
Old 05-18-2007
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");
Smilie
# 6  
Old 05-19-2007
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 08:25 PM..
# 7  
Old 05-21-2007
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 )
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk pipe to sort

In the below awk to add a sort by smallest to largest should it be added after the END? Thank you :). BEGIN { FS="*" } # Read search terms from file1 into 's' FNR==NR { s next } { # Check if $5 matches one of the search terms for(i in s) { if($5 ~ i) { ... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Sort file using pipe

I am trying to create a file (epilepsy70_average.txt) and then pipe that file into a sort and save a new file. The new file is sort.txt but as of know it is blank. I can create the file in one command and then sort it in another. Is the pipe not correct? Thank you :). awk... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. SuSE

Is this iostat pipe to sort service request command correct?

Hi, my os is SLES 10 from sort iostat as my output does not include rkB/s and wkB/s I probably have to adjust the key position, so is the following iostat pipe to sort service request command correct? oracle@vmc_stallite:~> iostat -x | sort -nrk11 sda 0.96 ... (4 Replies)
Discussion started by: jediwannabe
4 Replies

4. Shell Programming and Scripting

sort command help needed

I have a file with three fields of data for example say we have 1420 $12.56 45 3456 $15.89 14 2341 $25.87 32 I need to use a command to show who is getting paid the highest amount to standard output. I am not sure if I should use a sort command or a copy and paste... (1 Reply)
Discussion started by: drew211
1 Replies

5. Shell Programming and Scripting

Help needed to sort data

Hello All, Today i have been asking lots of question, hope to become good in scripting soon with all the wonderful advices i get. The question is i want to sort data a get uniq string from it. The code i am using to generate the output is:- check_sun() { for i in $SUN_PLATFORM do $ECHO... (0 Replies)
Discussion started by: asirohi
0 Replies

6. Shell Programming and Scripting

Help needed with Sort and uniq data

Hi All, After Sorting directories and files i have got following output as below, now i only want the strings common in them, so the actual output should be as below in the bottom. How do i do that? Thanks -adsi File to be modified:- Common Components for ----> AA... (4 Replies)
Discussion started by: asirohi
4 Replies

7. Shell Programming and Scripting

sort command help needed.

Hello, suppose i have a student.dat file whose format is studentno studentname semester marks1 marks2 marks3 total then how can i sort first in semester wise and then total wise? I mean semester wise ranking wise record should be displayed. Semester should be displayed in ascending order... (6 Replies)
Discussion started by: salman4u
6 Replies

8. Shell Programming and Scripting

Help needed in removing intermediate segments from a pipe delimited segment file

Hi, I just stuckup in doing some regular expressions on a file. I have data which has multiple FHS and BTS segments like: FHS|12121|LOCAL|2323 MSH|10101|POTAMAS|2323 PID|121221|THOMAS|DAVID|23432 OBX|2342|H1211|3232 BTS|0000|MERSTO|LIABLE FHS|12121|LOCAL|2323 MSH|10101|POTAMAS|2323... (3 Replies)
Discussion started by: naren_0101bits
3 Replies

9. Shell Programming and Scripting

Help needed to sort file

I have a text file comprising of 3 columns 1. First column - numeric count/frequency of error codes 2. Second column - alphanumeric hyphenated error code 3. Third column - Error code description (alphanumeric) 184 ABCD-954 Errorcodedescription1 35645 DFMS-323 Errorcodedescription2 43534 ... (2 Replies)
Discussion started by: inditopgun
2 Replies

10. UNIX for Dummies Questions & Answers

Sort - Help Needed

Hi, I am facing a small problem while I am doing the sort. Problem Definition: I have a file which has the following data. >cat test.dat zxzxc asdasd qwaxcZa qwaxcA When I do a sort on this file. the result set is asdasd qwaxcA (4 Replies)
Discussion started by: Andysundar
4 Replies
Login or Register to Ask a Question