4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

シェルスクリプト&PowerShellAdvent Calendar 2024

Day 7

シェルスクリプトでread中にシグナルが発生したらどうなるか?

Posted at

はじめに

シェルスクリプトで read 中にシグナルが発生したらどうなるか?の実験です。

実験

こんなスクリプトで実験

#!/bin/sh

trap 'echo usr1' USR1
(sleep 2; kill -s USR1 $$) &
echo "read start"
read line
echo "read aborted: $?"

結果

dash と FreeBSD sh と NetBSD sh と ksh93 と ksh88 は read が中断される。

dash
$ dash read.sh
read start
usr1
read aborted: 1
FreeBSD sh
$ sh read.sh
read start
usr1
read aborted: 158  (128 + 30:USR1)
NetBSD sh
$ sh read.sh
read start
usr1
read aborted: 1
ksh93
$ ksh read.sh
read start
usr1
read aborted: 266  (128 * 2 + 10:USR1)
ksh88 on Solaris 10
$ ksh read.sh
read start
usr1
read aborted: 0

bash は read 中に割り込まれるが中断されない。

$ bash read.sh
read start
usr1
                    (enterを押す)
read aborted: 0

mksh と NetBSD ksh と OpenBSD sh/ksh と yash と zsh は read 中に割り込まれない。

$ mksh read.sh
read start
                    (enterを押す)
usr1
read aborted: 0
NetBSD ksh
$ sh read.sh
read start
                    (enterを押す)
usr1
read aborted: 0
OpenBSD sh/ksh
$ sh read.sh
read start
                    (enterを押す)
usr1
read aborted: 0
$ yash read.sh
read start
                    (enterを押す)
usr1
read aborted: 0
$ zsh read.sh
read start
                    (enterを押す)
usr1
read aborted: 0

結論

バーラバラ

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?