LoginSignup
0
0

More than 5 years have passed since last update.

[WIP] Shell Command Language - trap 日本語訳

Last updated at Posted at 2019-04-02

原文: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap

例によって自分用のメモ

trap n [condition...]
trap [action condition...]

If the first operand is an unsigned decimal integer, the shell shall treat all operands as conditions, and shall reset each condition to the default value. Otherwise, if there are operands, the first is treated as an action and the remaining as conditions.

最初のオペランドが符号なし10進整数の場合、シェルはすべてのオペランドを条件として扱い、各条件をデフォルト値にリセットします。それ以外の場合は、最初のオペランドがアクションとして扱われ、残りが条件として扱われます。

If action is '-', the shell shall reset each condition to the default value. If action is null ( "" ), the shell shall ignore each specified condition if it arises. Otherwise, the argument action shall be read and executed by the shell when one of the corresponding conditions arises. The action of trap shall override a previous action (either default action or one explicitly set). The value of "$?" after the trap action completes shall be the value it had before trap was invoked.

actionが ' - 'の場合、シェルは各条件をデフォルト値にリセットします。actionがnull("")の場合、シェルは指定された各条件が発生しても無視します。そうでなければ、対応する条件の1つが発生したときに、引数のアクションがシェルによって読み取られて実行されます。trapの動作は前の動作(デフォルトの動作または明示的に設定された動作)を上書きします。トラップアクションが完了した後、"$?"の値は、トラップが呼び出される前の値になります。

The condition can be EXIT, 0 (equivalent to EXIT), or a signal specified using a symbolic name, without the SIG prefix, as listed in the tables of signal names in the header defined in XBD Headers; for example, HUP, INT, QUIT, TERM. Implementations may permit names with the SIG prefix or ignore case in signal names as an extension. Setting a trap for SIGKILL or SIGSTOP produces undefined results.

条件は、EXIT、0(EXITと同等)、またはXBDヘッダーで定義されている<signal.h>ヘッダー内のシグナル名の表にリストされているように、SIG接頭部なしの記号名を使用して指定されたシグナルです。たとえば、HUP、INT、QUIT、TERMなどです。SIG接頭辞を持つ名前や、シグナル名の大文字小文字の区別を無視する拡張を行う実装も許可されています。SIGKILLまたはSIGSTOPにトラップを設定すると、未定義の結果が生じます。

The environment in which the shell executes a trap on EXIT shall be identical to the environment immediately after the last command executed before the trap on EXIT was taken.

シェルがEXITでトラップを実行する環境は、EXITでトラップが実行される前に最後に実行されたコマンドの直後の環境と同じでなければなりません。

Each time trap is invoked, the action argument shall be processed in a manner equivalent to:

trapが呼び出されるたびに、action引数は以下と同等の方法で処理されます:

eval action

Signals that were ignored on entry to a non-interactive shell cannot be trapped or reset, although no error need be reported when attempting to do so. An interactive shell may reset or catch signals ignored on entry. Traps shall remain in place for a given shell until explicitly changed with another trap command.

非対話型シェルへの入り口で無視されたシグナルは、トラップまたはリセットすることはできませんが、そうしようとしてもエラーを報告する必要はありません。対話型シェルは、入り口で無視されたシグナルをリセットまたはキャッチすることがあります。トラップは、別のtrapコマンドで明示的に変更されるまで、特定のシェルに対してその場に残ります。

When a subshell is entered, traps that are not being ignored shall be set to the default actions, except in the case of a command substitution containing only a single trap command, when the traps need not be altered. Implementations may check for this case using only lexical analysis; for example, if trap and $( trap -- ) do not alter the traps in the subshell, cases such as assigning var=trap and then using $($var) may still alter them. This does not imply that the trap command cannot be used within the subshell to set new traps.

サブシェルに入ると、トラップを変更する必要がない場合に、単一のtrapコマンドのみを含むコマンド置換の場合を除いて、無視されていないトラップはデフォルトのアクションに設定されます。実装は字句解析だけを使用してこのケースをチェックするかもしれません。 たとえば、`trap`$(trap -- )がサブシェル内のトラップを変更しない場合、var=trapを割り当ててから$($var)を使用するなどの場合でも、それらは変更される可能性があります。 これは、trapコマンドをサブシェル内で使用して新しいトラップを設定できないことを意味するのではありません。

The trap command with no operands shall write to standard output a list of commands associated with each condition. If the command is executed in a subshell, the implementation does not perform the optional check described above for a command substitution containing only a single trap command, and no trap commands with operands have been executed since entry to the subshell, the list shall contain the commands that were associated with each condition immediately before the subshell environment was entered. Otherwise, the list shall contain the commands currently associated with each condition.

オペランドなしのtrapコマンドは、各条件に関連したコマンドのリストを標準出力に書き出します。
コマンドがサブシェルで実行される場合、実装は、単一のtrapコマンドのみを含むコマンド置換に対して上記のオプションのチェックを実行しません。そして、サブシェルに入ってからオペランド付きのtrapコマンドが実行されていない場合、リストにはサブシェル環境に入る直前に各条件に関連付けられていたコマンドが含まれていなければなりません。
それ以外の場合、リストには現在各条件に関連付けられているコマンドが含まれます。

The format shall be:

そのフォーマットは以下のようになります:

"trap -- %s %s ...\n", <action>, <condition> ...

The shell shall format the output, including the proper use of quoting, so that it is suitable for reinput to the shell as commands that achieve the same trapping results.

シェルは、同じトラッピング結果を得られるコマンドとしてシェルで再度実行するのに適しているように、引用符の適切な使用を含めて出力をフォーマットします。

For example:

例として、

save_traps=$(trap)
...
eval "$save_traps"
0
0
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
0
0