help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] How to customize SIGPIPE trap in practice in bash?


From: Peng Yu
Subject: [Help-bash] How to customize SIGPIPE trap in practice in bash?
Date: Wed, 26 Dec 2018 12:53:55 -0600

Hi,

I am trying to understand how to deal with SIGPIPE on my own other
than relying on the default handler. But it is very different from
other signals. The following examples just dump too many trapped
messages. It is not of any real use.

Should users ever customize SIGPIPE on their own in a bash script? Can
anybody show a good example to demonstrate the use of SIGPIPE?

$ cat ./main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
./script.sh | head -n 1

$ cat ./script.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
trap 'echo in SIGPIPE>&2' SIGPIPE
for((i=0;i<=3000;++i)); do
    echo "$i"
done
$ ./main.sh
./script.sh | head -n 1
trap 'echo in SIGPIPE>&2' SIGPIPE
for((i=0;i<=3000;++i)); do
  echo "$i"
done
0
./script.sh: line 7: echo: write error: Broken pipe
echo in SIGPIPE>&2
in SIGPIPE
./script.sh: line 7: echo: write error: Broken pipe
echo in SIGPIPE>&2
in SIGPIPE
...

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]