Register
It is currently Thu Sep 18, 2014 3:44 pm

stop stdin to script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Thu Jun 05, 2008 5:09 pm   

Joined: Thu Jun 05, 2008 5:04 pm
Posts: 4
I have a script which I pipe standard input into to filter.

Code:
#!/bin/bash
ctr=0
while read line ; do
    let ctr=$ctr+1
    echo $line
    if [ $ctr == 10 ] ; then
        break
    fi
done


when I run:

Quote:
cat something_large | myscript.sh


I get broken pipe. How can I gracefully stop using stdin?


Top
 Profile  
 PostPosted: Thu Jun 05, 2008 11:53 pm   
User avatar

Joined: Sun Apr 13, 2008 4:05 am
Posts: 37
Location: /dev/random
read can do it with various ways, choose a parameter for it.

-n nchars
read returns after reading nchars characters rather than waiting for a complete
line of input.
-t timeout
Cause read to time out and return failure if a complete line of input is not read
within timeout seconds. This option has no effect if read is not reading input
from the terminal or a pipe.
-d delim
The first character of delim is used to terminate the input line, rather than new-
line.


Top
 Profile  
 PostPosted: Fri Jun 06, 2008 10:33 am   

Joined: Thu Jun 05, 2008 5:04 pm
Posts: 4
I don't think I have a problem with read. I believe read uses readline when input is stdin. My problem is after Ive read enough lines, the input is still piping plenty more and I exit so it has nowhere to write.


Top
 Profile  
 PostPosted: Fri Jun 06, 2008 10:58 am   

Joined: Tue Apr 01, 2008 10:19 am
Posts: 49
Perhaps pipe your something_large through head -n 10?


Top
 Profile  
 PostPosted: Fri Jun 06, 2008 2:40 pm   

Joined: Thu Jun 05, 2008 5:04 pm
Posts: 4
I kind of lied. It was not a cat. It was `svn log | filtersvn.sh`. When I run `svn log | head -10` I also get a pipe broken error. So it is probably a situation with svn log.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP