.. 11-shell .. _label11: 11. シェルコマンド ================================== .. Shell commands (commands to be executed by the operating system) can be freely mixed with other code. シェルコマンド(OSによって実行されるコマンド)は自由に他のコードとミックスすることができます。 .. NOTE: the syntax and shell usage is identical on all platforms, including Win32. To avoid portability problems on Win32, it is recommended that you avoid the use of the native shell interpreter cmd. .. note:: 構文とシェルの使い方はすべてのプラットフォーム(Win32を含む)において同一です。Win32上に移植した場合の問題を避けるために、ネイティブのインタープリター ``cmd`` を使わないことをおすすめします。 :: LIB = $(dir lib) println(The contents of the $(LIB) directory is:) ls $(LIB) .. _label11.1: 11.1 簡単なコマンド ---------------------------------- .. The syntax of shell commands is similar to the syntax used by the Unix shell bash. In general, a command is a pipeline. A basic command is part of a pipeline. It is specified with the name of an executable and some arguments. Here are some examples. シェルコマンドの構文はUnixシェル ``bash`` を使うときの構文と似ています。 通常、コマンドは *パイプライン* となっています。通常のコマンドはパイプラインの一部です。コマンドを実行するためには、実行可能なコマンド名といくつかの引数を指定する必要があります。以下はいくつかの例です。 :: ls ls -AF . echo Hello world .. The command is found using the current search path in the variable PATH[], which should define an array of directories containing executables. コマンドは実行可能コマンドが格納されているディレクトリの配列 ``PATH[]`` 変数の値を用いて探します。 .. A command may also be prefixed by environment variable definitions. コマンドは環境変数の定義によって修正されることがあります。 :: # "Hello world" と出力 env X="Hello world" Y=2 printenv X # Visual C++ のインクルードパスを検索 env include="c:\Program Files\Microsoft SDK\include" cl foo.cpp .. _label11.2: 11.2 検索 ---------------------------------- .. Commands may contain wildcard patterns. A pattern specifies a set of files through a limited kind of regular expression. Patterns are expanded before the function is executed. コマンドにはワイルドカードパターンを含めることができます。パターンには制限された正規表現を用いたファイルの集合を指定します。パターンは関数が実行される前に展開されます。 :: # .c 拡張子のファイルをすべてリスト ls *.c # 1文字の接頭辞と.cの拡張子を持ったすべてのファイルをリスト ls ?.c # hello.mlファイルをfoo.mlにリネーム mv {hello,foo}.ml .. A comprehensive description of OMake glob patterns is given in Section 10.4. OMakeのglobパターンのさらなる説明は ":ref:`label10.4`" で与えられます。 .. _label11.3: 11.3 バックグラウンドでのジョブ ---------------------------------- .. The command may also be placed in the background by placing an ampersand after the command. Control returns to the shell without waiting for the job to complete. The job continues to run in the background. コマンドはまたアンパサンド ``&`` をコマンドの後に付与することで、バックグラウンド上で実行されます。ユーザへの制御は、ジョブが完了するまで待つことなく返されます。ジョブはバックグラウンド上で走り続けます。 :: gcc -o hugeprogram *.c & .. index:: single: ファイルのリダイレクション .. _label11.4: 11.4 ファイルのリダイレクション ---------------------------------- .. Input and output can be redirected to files by using the <, >, and >& directives after the command. 入力と出力は ``<`` , ``>`` , ``>&`` をコマンドの後に付与することによって、ファイルにリダイレクトすることができます。 :: # "foo" ファイルに書き込み echo Hello world > foo # fooファイルからの入力をリダイレクト cat < foo # 標準出力、標準エラーをfooファイルにリダイレクト gcc -o boo *.c >& foo .. index:: single: パイプライン .. _label11.5: 11.5 パイプライン ---------------------------------- .. Pipelines are sequences of commands, where the output from each command is sent to the next. Pipelines are defined with the | and |& syntax. With | the output is redirected, but errors are not. With |& both output and errors are redirected. パイプラインはコマンドのシーケンスで、各々のコマンドの出力は次のコマンドへ送られます。パイプは ``|`` と ``|&`` で定義されます。 ``|`` は出力はリダイレクトされますが、エラーはされません。 ``|&`` は出力とエラーの両方がリダイレクトされます。 :: # lsコマンドの出力をプリンターに送る ls *.c | lpr # 出力とエラーをEメールを使ってjyhに送る gcc -o hugefile *.c |& mail jyh .. index:: single: 実行の条件分岐 .. _label11.6: 11.6 条件分岐の実行 ---------------------------------- .. Commands may also be composed though conditional evaluation using the || and && syntax. Every command has an integer exit code, which may be zero or some other integer. A command is said to succeed if its exit code is zero. The expression command1 && command2 executes command2 only if command1 succeeds. The expression command1 || command2 executes command2 only if command1 fails. コマンドは ``||`` と ``&&`` の条件分岐を使うことで複雑に組み合わせることができます。すべてのコマンドは0か他の整数の終了コードを返します。コマンドは終了コードが0であった場合、成功したと宣言します。式 ``command1 && command2`` は、 ``command1`` が成功した場合のみ ``command2`` が実行されます。式 ``command1 || command2`` は、 ``command1`` が失敗した場合のみ ``command2`` が実行されます。 :: # 可能な場合のみx/yファイルを表示する cd x && cat y # foo.exeを実行するか、エラーメッセージを表示する (test -x foo.exe && foo.exe) || echo "foo.exe is not executable" .. index:: single: グループ化 .. _label11.7: 11.7 グループ化 ---------------------------------- .. Parenthesis are used for grouping in a pipeline or conditional command. In the following expression, the test function is used to test whether the foo.exe file is executable. If it is, the foo.exe file is executed. If the file is not executable (or if the foo.exe command fails), the message "foo.exe is not executable" is printed. パイプラインをグループ化したり、条件分岐をさせる場合には括弧を使います。以下の式では、 ``test`` 関数は ``foo.exe`` ファイルが実行可能であるかどうか試し、もしそうであったのなら、 ``foo.exe`` ファイルが実行されます。もし実行可能でなかったのなら(あるいは ``foo.exe`` コマンドが失敗したのなら)、メッセージ ``"foo.exe is not executable"`` が表示されます。 :: # foo.exeを実行するか、エラーメッセージを表示する (test -x foo.exe && foo.exe) || echo "foo.exe is not executable" .. _label11.8: 11.8 シェルコマンドとは何か? ---------------------------------- .. Syntactially, shell commands are any line that is not one of the following 構文的には、シェルコマンドは以下のうち一つも該当していない任意の行を指します。 .. A variable definition of the form VAR=string A function call f(...) or method call o.f(...) A rule definition containing a colon string: ... A special command, including the following: * ``VAR=string`` の形の変数定義 * 関数の呼び出し ``f(...)`` かメソッドの呼び出し ``o.f(...)`` * コロン:を含んだルールの定義 ``string: ...`` * 以下のリストを含む特殊コマンド * ``if ...`` * ``switch ...`` * ``match ...`` * ``section ...`` * ``return ...`` .. Commands may also be builtin (aliases). See the documentation for the Shell object for more information. コマンドはまたビルドイン(エイリアス)でもあります。さらなる情報は ":ref:`label12.1.22`" を参照してください。 .. _label11.9: 11.9 基本的なビルドイン関数 ---------------------------------- .. index:: single: echo() .. _label11.9.1: 11.9.1 echo ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. The echo function prints a string. ``echo`` 関数は文字列を表示します。 :: $(echo ) echo .. index:: single: cd() .. _label11.9.2: 11.9.2 cd ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. The cd function changes the current directory. ``cd`` 関数はカレントディレクトリを変更します。 :: cd(dir) dir : Dir .. The cd function also supports a 2-argument form: ``cd`` 関数は2つの引数を取ることもできます。 :: $(cd dir, e) dir : Dir e : expression .. In the two-argument form, expression e is evaluated in the directory dir. The current directory is not changed otherwise. 2つ引数を取る形では、式 ``e`` がディレクトリ ``dir`` 上で評価されます。カレントディレクトリは変更されません。 .. The behavior of the cd function can be changed with the CDPATH variable, which specifies a search path for directories. This is normally useful only in the osh command interpreter. ``cd`` 関数のふるまいはディレクトリの検索パスを指定している ``CDPATH`` 変数を用いて変更できます。これは通常、oshコマンドインタープリタ上でのみ有効です。 :: CDPATH : Dir Sequence .. For example, the following will change directory to the first directory ./foo, ~/dir1/foo, ~/dir2/foo. 例えば、以下の式ではディレクトリを最初のディレクトリ ``./foo`` , ``~/dir1/foo`` , ``~/dir2/foo`` に変更します。 :: CDPATH[] = . $(HOME)/dir1 $(HOME)/dir2 cd foo .. _label11.10: 11.10 ジョブを制御するビルドイン関数 --------------------------------------- .. index:: single: jobs() .. _label11.10.1: 11.10.1 jobs ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. The jobs function prints a list of jobs. ``jobs`` 関数はジョブのリストを表示します。 :: jobs .. index:: single: bg() .. _label11.10.2: 11.10.2 bg ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. The bg function places a job in the background. ``bg`` 関数はバックグラウンド上にジョブを配置します。 :: bg .. index:: single: fg() .. _label11.10.3: 11.10.3 fg ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. The fg function brings a job to the foreground. ``fg`` 関数はジョブをフォアグラウンドに持っていきます。 :: fg .. index:: single: stop() .. _label11.10.4: 11.10.4 stop ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. The stop function suspends a job. ``stop`` 関数はジョブを停止(suspends)します。 :: stop .. index:: single: wait() .. _label11.10.5: 11.10.5 wait ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. The wait function waits for a job to finish. If no process identifiers are given, the shell waits for all jobs to complete. ``wait`` 関数はジョブが完了するまで待機します。プロセス固有の値が指定されなかった場合、シェルはすべてのジョブが完了するまで待機します。 :: wait .. index:: single: kill() .. _label11.10.6: 11.10.6 kill ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. The kill function signals a job. ``kill`` 関数はジョブにシグナルを送ります。 :: kill [signal] .. _label11.11: 11.11 コマンド履歴 ---------------------------------- .. index:: single: history() .. _label11.11.1: 11.11.1 history ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ :: $(history-index) : Int $(history) : String Sequence history-file : File history-length : Int .. The history variables manage the command-line history in osh. They have no effect in omake. ヒストリ変数はosh上のコマンドライン履歴を管理します。これらの変数はomakeに影響を及ぼしません。 .. The history-index variable is the current index into the command-line history. The history variable is the current command-line history. ``history-index`` 変数はコマンドライン履歴における、現在のインデックスを表します。 ``history`` 変数は現在のコマンドライン履歴を表します。 .. The history-file variable can be redefined if you want the command-line history to be saved. The default value is ~/.omake/osh_history. コマンドライン履歴を保存したいと思った場合、あなたは ``history-file`` 変数を再定義することができます。デフォルトの値は ``~/.omake/osh_history`` です。 .. The history-length variable can be redefined to specify the maximum number of lines in the history that you want saved. The default value is 100. コマンドライン履歴の行数の最大値を指定したい場合、あなたは ``history-length`` 変数を再定義することができます。デフォルトの値は ``100`` です。