# Script runs ok alone, broken pipe when piped to head?

**URL:** <https://www.ruby-forum.com/t/script-runs-ok-alone-broken-pipe-when-piped-to-head/131867>\
**Category:** Ruby\
**Created:** [March 13, 2008, 11:50pm UTC](https://www.ruby-forum.com/t/script-runs-ok-alone-broken-pipe-when-piped-to-head/131867 "2008-03-13T23:50:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [March 13, 2008, 11:50pm UTC](https://www.ruby-forum.com/t/script-runs-ok-alone-broken-pipe-when-piped-to-head/131867/1 "2008-03-13T23:50:25Z")

</div>

Maybe I need to close the file or something?

This invokation runs fine, but generates _so_ much output:

ruby -rubygems excelparse.rb stuff.xls

so I piped it to head and it bombs:

ruby -rubygems excelparse.rb stuff.xls|head  
Row: 0 Cell: 0\> Dispapp  
Row: 0 Cell: 1\> Iprc  
Row: 0 Cell: 2\> UH tag  
Row: 0 Cell: 3\> Weight  
Row: 0 Cell: 4\> Tags removed  
Row: 0 Cell: 5\> Disk removed or wiped  
Row: 1 Cell: 0\> 80118  
Row: 1 Cell: 1\> 1173  
Row: 1 Cell: 2\> 961SU  
Row: 2 Cell: 0\> 80118  
excelparse.rb:19:in `write': Broken pipe (Errno::EPIPE) from excelparse.rb:19:in`puts’  
from excelparse.rb:19  
from excelparse.rb:15:in `each' from excelparse.rb:15 from /usr/lib/ruby/gems/1.8/gems/parseexcel-0.5.2/lib/parseexcel/worksheet.rb:143:in`each’  
from  
/usr/lib/ruby/gems/1.8/gems/parseexcel-0.5.2/lib/parseexcel/worksheet.rb:143:in  
`each’  
from excelparse.rb:11

Any hints? The script itself is a minor mod of the example given at  
the parseexcel site (see below).

Mahalo,  
Dave

cat excelparse.rb  
require ‘parseexcel’

#Open the excel file passed in from the commandline  
workbook = Spreadsheet::ParseExcel.parse(ARGV[0])

#Get the first worksheet  
worksheet = workbook.worksheet(0)

j=0  
#cycle over every row  
worksheet.each { |row|  
i=0  
if row != nil  
#cycle over each cell in this row if it’s not an empty row  
row.each { |cell|  
if cell != nil  
#Get the contents of the cell as a string  
contents = cell.to\_s(‘latin1’)  
puts “Row: #{j} Cell: #{i}\> #{contents}”  
end  
i = i+1  
}  
end  
j=j+1  
}

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [March 14, 2008, 12:01am UTC](https://www.ruby-forum.com/t/script-runs-ok-alone-broken-pipe-when-piped-to-head/131867/2 "2008-03-14T00:01:34Z")

</div>

Hi,

On Fri, Mar 14, 2008 at 9:34 AM, Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> `each' from /usr/lib/ruby/gems/1.8/gems/parseexcel-0.5.2 /lib/parseexcel/worksheet.rb:143:in `each’  
> from excelparse.rb:11
> 
> Any hints? The script itself is a minor mod of the example given at  
> the parseexcel site (see below).

It’s a very random guess, but is the EPIPE possibly happening because  
`head’  
closed the connection after reading 10 lines? What happens if you run it  
like this…?  
ruby -rubygems excelparse.rb stuff.xls 2\>&1 | less

Arlen

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [March 14, 2008, 12:04am UTC](https://www.ruby-forum.com/t/script-runs-ok-alone-broken-pipe-when-piped-to-head/131867/3 "2008-03-14T00:04:54Z")

</div>

Dave B. wrote:

> so I piped it to head and it bombs:
> 
> ruby -rubygems excelparse.rb stuff.xls|head  
> Row: 0 Cell: 0\> Dispapp  
> Row: 0 Cell: 1\> Iprc  
> Row: 0 Cell: 2\> UH tag  
> Row: 0 Cell: 3\> Weight  
> Row: 0 Cell: 4\> Tags removed  
> Row: 0 Cell: 5\> Disk removed or wiped  
> Row: 1 Cell: 0\> 80118  
> Row: 1 Cell: 1\> 1173  
> Row: 1 Cell: 2\> 961SU  
> Row: 2 Cell: 0\> 80118  
> excelparse.rb:19:in `write’: Broken pipe (Errno::EPIPE)

The “head” command reads and prints the first 10 lines of input (by  
default), then closes its input and exits.

Your script is thus having it’s output stream closed which it isn’t  
expecting. The broken pipe is to be expected.

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [March 14, 2008, 1:37am UTC](https://www.ruby-forum.com/t/script-runs-ok-alone-broken-pipe-when-piped-to-head/131867/4 "2008-03-14T01:37:43Z")

</div>

On Mar 13, 1:04 pm, Mark B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Row: 1 Cell: 0\> 80118

So to get my script to act like a responsible command line citizen,  
I’d need to insert some error handling and return normally when the  
pipe breaks, right?

Thanks for the info.  
Dave

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [March 14, 2008, 9:51am UTC](https://www.ruby-forum.com/t/script-runs-ok-alone-broken-pipe-when-piped-to-head/131867/5 "2008-03-14T09:51:13Z")

</div>

Dave B. wrote:

> So to get my script to act like a responsible command line citizen,  
> I’d need to insert some error handling and return normally when the  
> pipe breaks, right?

Since your script is, basically, in the business of doing IO, then yes,  
it is probably worth catching IO exceptions and exiting cleanly.

---

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [March 16, 2008, 10:46pm UTC](https://www.ruby-forum.com/t/script-runs-ok-alone-broken-pipe-when-piped-to-head/131867/6 "2008-03-16T22:46:39Z")

</div>

On 14.03.2008 01:30, Dave B. wrote:

> > > Row: 1 Cell: 0\> 80118  
> > > So to get my script to act like a responsible command line citizen,  
> > > I’d need to insert some error handling and return normally when the  
> > > pipe breaks, right?

Well, it depends. If you consider it an error when the script is not  
able to deliver all output the default behavior is ok. I usually do not  
bother to deal with this situation because I use head and tail from the  
command line only and I know what to expect there.

Kind regards

robert
