/usr/bin/bash: Difference between revisions

Line 1,234: Line 1,234:


== JOB CONTROL ==
== JOB CONTROL ==
      Job  control  refers  to the ability to selectively stop (suspend) the execution of processes and
ジョブ制御とは、プロセスの実行を選択的に停止(サスペンド)し、後の時点で実行を継続(レジューム)する機能のことである。 ユーザーは通常、オペレーティング・システム・カーネルのターミナル・ドライバとbashが共同で提供する対話型インターフェースを介してこの機能を使用する。
      continue (resume) their execution at a later point.  A user typically employs this  facility  via
      an  interactive  interface  supplied jointly by the operating system kernel's terminal driver and
      bash.


      The shell associates a job with each pipeline.  It keeps a table  of  currently  executing  jobs,
シェルは各パイプラインにジョブを関連付ける。 シェルは現在実行中のジョブのテーブルを保持し、jobsコマンドで一覧できる。 bashがジョブを非同期(バックグラウンド)で開始すると、次のような行が表示される:
      which  may  be listed with the jobs command.  When bash starts a job asynchronously (in the back‐
      ground), it prints a line that looks like:


              [1] 25647
:<nowiki>[1] 25647</nowiki>


      indicating that this job is job number 1 and that the process ID of the last process in the pipe‐
このジョブがジョブ番号 1 であり、このジョブに関連付けられたパイプラインの最後のプロセスのプロセス ID 25647 であることを示す。 1つのパイプライン内のすべてのプロセスは同じジョブのメンバーである。 Bashはジョブ制御の基礎としてジョブ抽象化を使用する。
      line associated with this job is 25647.  All of the processes in a single pipeline are members of
      the same job.  Bash uses the job abstraction as the basis for job control.


      To facilitate the implementation of the user interface to job control, the operating system main‐
ジョブ制御に対するユーザーインターフェースの実装を容易にするために、オペレーティングシステムは現在の端末プロセスグループIDという概念を保持している。 このプロセスグループのメンバー(プロセスグループIDが現在の端末プロセスグループIDと等しいプロセス)は、'''SIGINT'''のようなキーボードが生成するシグナルを受け取る。 これらのプロセスはフォアグラウンドにあると言われる。 バックグランドプロセスとは、プロセスグループIDが端末のものと異なるプロセスのことで、このようなプロセスはキーボードが生成するシグナルを受けない。 フォアグラウンド・プロセスだけが、端末からの読み取りや、 stty tostopでユーザーが指定した場合の端末への書き込みを許可される。 ターミナルからの読み込み(stty tostopが有効な場合はターミナルへの書き込み)を試みるバックグラウンド・プロセスは、カーネルのターミナル・ドライバによって'''SIGTTIN'''('''SIGTTOU''')シグナルが送られ、キャッチされない限り、プロセスは一時停止される。
      tains  the  notion  of  a current terminal process group ID.  Members of this process group (pro‐
      cesses whose process group ID is equal to the current terminal process  group  ID)  receive  key‐
      board-generated signals such as SIGINT.  These processes are said to be in the foreground.  Back‐
      ground processes are those whose process group ID differs from the terminal's; such processes are
      immune  to keyboard-generated signals.  Only foreground processes are allowed to read from or, if
      the user so specifies with stty tostop, write to the terminal.  Background  processes  which  at‐
      tempt  to  read  from  (write  to  when stty tostop is in effect) the terminal are sent a SIGTTIN
      (SIGTTOU) signal by the kernel's terminal driver, which, unless caught, suspends the process.


      If the operating system on which bash is running supports job control, bash contains  facilities
bashが動作しているオペレーティング・システムがジョブ制御をサポートしている場合、bashにはジョブ制御を使用するための機能が含まれている。 プロセスの実行中にサスペンド文字(通常は^Z、Control-Z)を入力すると、そのプロセスが停止し、制御がbashに戻る。 遅延サスペンド文字(通常は^Y、Control-Y)を入力すると、ターミナルから入力を読み込もうとしたときにプロセスが停止し、制御がbashに戻る。 その後、ユーザーはこのジョブの状態を操作することができ、 bgコマンドを使ってバックグラウンドで継続させたり、fgコマンドを使ってフォアグラウンドで継続させたり、 killコマンドを使って終了させたりすることができる。 Zは即座に有効になり、保留中の出力とtypeaheadが破棄されるという副次的な効果もある。
      to  use  it.  Typing  the suspend character (typically ^Z, Control-Z) while a process is running
      causes that process to be stopped and returns control to bash.  Typing the delayed suspend  char‐
      acter  (typically  ^Y, Control-Y) causes the process to be stopped when it attempts to read input
      from the terminal, and control to be returned to bash.  The user may then manipulate the state of
      this job, using the bg command to continue it in the background, the fg command to continue it in
      the foreground, or the kill command to kill it.  A ^Z takes effect immediately, and has the addi‐
      tional side effect of causing pending output and typeahead to be discarded.


      There  are  a  number  of  ways to refer to a job in the shell.  The character % introduces a job
シェルでジョブを参照する方法はいくつかある。 文字 % は、ジョブ仕様 (jobspec) を示す。 ジョブ番号nは%nと呼ばれる。 また、ジョブの開始時に使用される名前の接頭辞を使用したり、コマンドラインに現れる部分文字列を使用してジョブを参照することもできる。 例えば、%ce は、コマンド名が ce で始まる停止したジョブを指す。 接頭辞が複数のジョブにマッチした場合、bashはエラーを報告する。 一方、%?ceを使うと、コマンドラインにceという文字列を含むジョブを指す。 この部分文字列が複数のジョブにマッチすると、bashはエラーを報告する。 記号%%%+は、シェルが考える現在のジョブ(フォアグラウンドまたはバックグラウンドで起動中に停止した最後のジョブ)を指す。 前のジョブは %- を使って参照できる。 ジョブが1つしかない場合、%+%-の両方がそのジョブを指すのに使われる。 ジョブに関する出力(例えばjobsコマンドの出力)では、現在のジョブには常に+、前のジョブには-が付けられる。
      specification (jobspec).  Job number n may be referred to as %n.  A job may also be  referred  to
      using  a  prefix  of  the name used to start it, or using a substring that appears in its command
      line.  For example, %ce refers to a stopped job whose command name begins with ce.  If  a  prefix
      matches  more  than one job, bash reports an error.  Using %?ce, on the other hand, refers to any
      job containing the string ce in its command line.  If the substring matches more  than  one job,
      bash  reports  an  error.  The symbols %% and %+ refer to the shell's notion of the current job,
      which is the last job stopped while it was in the foreground or started in the background.  The
      previous  job  may  be referenced using %-.  If there is only a single job, %+ and %- can both be
      used to refer to that job.  In output pertaining to jobs (e.g., the output of the jobs  command),
      the  current  job is always flagged with a +, and the previous job with a -.  A single % (with no
      accompanying job specification) also refers to the current job.


      Simply naming a job can be used to bring it into the foreground: %1 is a synonym for  ``fg %1'',
1 ``fg %1'' の同義語で、バックグラウンドにあるジョブ 1 をフォアグラウンドにする。 同様に、%1 &''はジョブ1をバックグラウンドで再開し、`bg %1''と等価である。
      bringing job 1 from the background into the foreground.  Similarly, ``%1 &'' resumes job 1 in the
      background, equivalent to ``bg %1''.


      The shell learns immediately whenever a job changes state.  Normally,  bash  waits  until  it  is
シェルはジョブの状態が変わるとすぐにそれを知る。 通常、bashは他の出力を中断しないように、ジョブのステータスの変更を報告する前にプロンプトを表示するまで待つ。 set組み込みコマンドの-bオプションを有効にすると、bashはそのような変更を直ちに報告する。 '''SIGCHLD'''に対するトラップは、終了する子プロセスごとに実行される。
      about  to  print  a  prompt before reporting changes in a job's status so as to not interrupt any
      other output.  If the -b option to the set builtin command is enabled, bash reports such  changes
      immediately.  Any trap on SIGCHLD is executed for each child that exits.


      If  an attempt to exit bash is made while jobs are stopped (or, if the checkjobs shell option has
ジョブが停止中(または shopt 組み込みコマンドを使用して checkjobs シェル・オプションが有効になっている場合は実行中)に bash を終了しようとすると、シェルは警告メッセージを表示し、checkjobs オプションが有効になっている場合は、ジョブとそのステータスを一覧表示する。 その後、jobsコマンドを使用してステータスを調べることができる。 コマンドを介さずに2回目の終了が試みられた場合、シェルは警告を再度表示せず、停止しているジョブはすべて終了される。
      been enabled using the shopt builtin, running), the shell prints a warning message, and,  if  the
      checkjobs option  is  enabled,  lists the jobs and their statuses.  The jobs command may then be
      used to inspect their status.  If a second attempt to exit is made without  an  intervening  com‐
      mand, the shell does not print another warning, and any stopped jobs are terminated.


      When  the  shell  is  waiting for a job or process using the wait builtin, and job control is en‐
シェルがwait組み込み関数を使用してジョブまたはプロセスを待機しており、ジョブ制御が有効になっている場合、waitはジョブの状態が変化したときに戻る。fオプションを付けると、waitはジョブまたはプロセスが終了するまで待ってから戻るようになる。
      abled, wait will return when the job changes state. The -f option causes wait to wait  until  the
      job or process terminates before returning.


== PROMPTING ==
== PROMPTING ==