Eric Tanter | 27 Aug 19:55 2012
Picon
Picon

Simple shell scripts

Hi,

Here is a simple shell script (upper.hs):

import Data.Char
main = interact $ map toUpper

which composes fine with other scripts:

bash-3.2$ yes | head -n 3 | runghc upper.hs 
Y
Y
Y

but not always:

bash-3.2$ yes | runghc upper.hs | head -n 3
Y
Y
Y
<stdout>: hFlush: resource vanished (Broken pipe)

Any idea why this error occurs/how to avoid it?

(running just:
yes | runghc upper.hs
gives the expected infinite stream of Ys)

Thanks!

(Continue reading)

Iavor Diatchki | 27 Aug 20:08 2012
Picon

Re: Simple shell scripts

Hello,

this happens because "head" probably closes the file descriptor after 3 lines, and then the Haskell program tries to write to a closed handle (i.e., it's stdout is not there anymore).  The best thing to do depends on the program. One fairly simple option would be to handle the exception, and do something (perhaps ignore it).
-Iavor

On Mon, Aug 27, 2012 at 10:55 AM, Eric Tanter <etanter <at> dcc.uchile.cl> wrote:
Hi,

Here is a simple shell script (upper.hs):

import Data.Char
main = interact $ map toUpper

which composes fine with other scripts:

bash-3.2$ yes | head -n 3 | runghc upper.hs
Y
Y
Y

but not always:

bash-3.2$ yes | runghc upper.hs | head -n 3
Y
Y
Y
<stdout>: hFlush: resource vanished (Broken pipe)

Any idea why this error occurs/how to avoid it?

(running just:
yes | runghc upper.hs
gives the expected infinite stream of Ys)

Thanks!

-- Éric

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Brandon Allbery | 27 Aug 20:11 2012
Picon

Re: Simple shell scripts

On Mon, Aug 27, 2012 at 1:55 PM, Eric Tanter <etanter <at> dcc.uchile.cl> wrote:
Here is a simple shell script (upper.hs):

"shell script" means a script written in the shell's programming language.  This is probably best referred to as a Haskell script.
 
bash-3.2$ yes | runghc upper.hs | head -n 3
Y
Y
Y
<stdout>: hFlush: resource vanished (Broken pipe)

Any idea why this error occurs/how to avoid it?

It's normal, and shells normally ignore it when programs in the middle of a pipeline die with SIGPIPE.  Problem here is the Haskell runtime is itself intercepting the SIGPIPE and throwing a verbose Haskell exception.

Possibly the runtime should detect that stdout is a pipe and disable the usual SIGPIPE handler, or if it must run cleanup stuff then it should not print anything and it should after cleanup raise(SIGPIPE) on itself with the default SIGPIPE handler so the shell will react properly.

You might be able to do this yourself with (System.Posix.Signals.installHandler openEndedPipe Default Nothing).

--
brandon s allbery                                      allbery.b <at> gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane