<!doctype linuxdoctr system>
<!--<!doctype linuxdoc system>-->

<!-- 
     An introduction to bash shell programming
 -->

<!-- Title information -->

<article>
<title>BASH Programming - Introduction HOW-TO</title>
<author>by Mike G <tt/mikkey at dynamo.com.ar/</author>
<date>Mon Jul 27 11:47:00 ART 2000 </date>
<trans>日本語訳 千旦裕司
<tdate>July 2000

<abstract> 
<!--This article intends to help you to start programming 
    basic-intermediate shell scripts. It does not intend to be an 
    advanced document (see the title). I am NOT an expert nor guru 
    shell programmer. I decided to write this because I'll learn a 
    lot and it might be useful to other people. Any feedback will be 
    apreciated, specially in the patch form :)-->
この文書は、初中級のシェルスクリプトのプログラムを始める人の手引書として書か
れています。(タイトルからも分かるように)これは高度な内容を解説した文書ではあり
ません。わたし自身、シェルプログラムのエキスパートではないですし、ましてや達人
でもありません。これを書こうと思い立ったのは、そこから自分が多くを学べるだろう
と思ったからであり、もしかしたら他人の役に立つかもしれないと考えたからです。
どんなフィードバックも歓迎します。特に、パッチ形式でいただけると嬉しく
思います :)
</abstract>


<!-- Table of contents -->
<toc>

<!-- Requisites -->
<!--<sect>Introduction-->
<sect>イントロダクション

<!--<sect1>Getting the latest version-->
<sect1>最新バージョンの入手場所
<p>
<htmlurl url="http://www.linuxdoc.org/HOWTO/Bash-Prog-Intro-HOWTO.html"
     name="http://www.linuxdoc.org/HOWTO/Bash-Prog-Intro-HOWTO.html">


     </sect1>

<!--<sect1>
          Requisites
          <P> Familiarity with GNU/Linux command lines, and familiarity 
          with basic programming concepts is helpful. While this is not
          a programming introduction, it explains (or at least tries) many
          basic concepts.
          <P></sect1>-->
<sect1>必要事項
<p>
GNU/Linux のコマンドラインに違和感がなく、プログラムについての基本的な
考え方が身についていると理解しやすいと思います。しかし、この冊子はプログラミ
ングの技術書ではないので、多くの基本概念について説明を加えて(あるいは少なくと
もその努力をして)います。


<!--<sect1>Uses of this document-->
<sect1>この文書の使い方

<!--<P> This document tries to be useful in the following situations-->
<p>この文書は、次のような場合に役立つことを念頭に置いて書かれています。

<itemize>
<!--<item> You have an idea about programming and you want to start coding 
some shell scripts.-->
<item>プログミングがどういうものか知っていて、これからシェルスクリプトの
コーディングを始めようとしている場合。

<!--<item> You have a vague idea about shell programming and want some sort of 
reference.-->
<item>シェルプログラミングについて漠然としか知らないので、参照できる文献が
欲しい場合。

<!--<item> You want to see some shell scripts and some comments to start 
writing your own.-->
<item>自分で書き始めるにあたって、実際のスクリプトとそのコメントをいくつか
見たい場合。

<!--<item> You are migrating from DOS/Windows (or already did) and want to 
make "batch" processes.-->
<item>DOS/Windows 環境から移行しようとしていて(あるいはしてしまって)、
「バッチ処理」を実現したい場合。

<!--<item> You are a complete nerd and read every how-to available-->
<item>いわゆる完全に「オタク(nerd)」と呼ばれるひとで、片っ端から HOW-TO 本を
読んでいる場合。

</itemize>
<!--</sect1>-->


<!-- Very simple Scripts --> 
<!--<sect>Very simple Scripts-->
<sect>非常にシンプルなスクリプト

<!--<P> This HOW-TO will try to give you some hints about shell script 
programming strongly based on examples.-->
<p>
この HOW-TO は、例題に重点を置いて、シェルスクリプトのプログラミングに関す
るヒントを、述べようとしています。

<!--<P> In this section you'll find some little scripts which will hopefully 
help you to understand some techniques.-->
<p>この章では、いくつかのテクニックを理解するのに役立つような簡単なスクリプト
を取り上げます。

<!--<sect1>Traditional hello world script-->
<sect1>伝統の Hello World スクリプト

        <!-- __TO-DO__
	someone suggested using this instead of the line numbering 
	but it doesn't work on verion 1.0.9
	<pre></pre> 
	-->
<P>
        <tscreen><verb>
        #!/bin/bash
        echo Hello World    
        </verb></tscreen>
<P> 
<!--<P> This script has only two lines. The first indicates the system which 
program to use to run the file. -->
<p>
このスクリプトは、二行しかありません。一行目は、ファイルを実行するのに
どのプログラムを使うかをシステムに指示しています。

<!--<P> The second line is the only action performed by this script, which 
prints 'Hello World' on the terminal.-->
<p>
二行目は、このプログラムが実行する唯一の命令が書かれていて、それによって
端末に "<tt>Hello World</tt>" と表示されます。

<!--<P> If you get something like <it>./hello.sh: Command not found.</it>
Probably the first line '#!/bin/bash' is wrong, issue whereis bash or see 
'finding bash' to see how sould you write this line. </sect1>-->
<p>
もしかすると、<tt>./hello.sh: Command not found.</tt> と表示されたかもしれ
ません。おそらく、最初の行の "<tt>#!/bin/bash</tt>" が正しくないためで
しょう。'<tt>whereis bash</tt>' と打つか、<ref id="finding_bash" 
name="「bash を探す(10.3)」">を見るかして、その行の書き方を確認してください。

<!--<sect1>A very simple backup script-->
<sect1>非常にシンプルなバックアップスクリプト

        <p>
        <tscreen><verb>
	#!/bin/bash          
	tar -cZf /var/my-backup.tgz /home/me/
	</verb></tscreen>

<!--<P> In this script, instead of printing a message on the terminal, 
we create a tar-ball of a user's home directory. This is NOT intended
to be used, a more useful backup script is presented later in this document.
</sect1></sect>-->
<p>
このスクリプトでは、ターミナルにメッセージを表示するのではなく、ユーザのホー
ムディレクトリの内容を圧縮ファイル(tarball)にまとめます。これは実際の使用を
意図したものではありません。もっと便利なバックアップスクリプトを、この文書で
後ほど紹介します。


<!--########################第三章　リダイレクションについて############-->

<!-- Redirection -->
<!--<sect>All about redirection-->
<sect>リダイレクトションについて(redirection)

<!--<sect1>Theory and quick reference-->
<sect1>理論と簡単なリファレンス

<!--<p>There are 3 file descriptors, stdin,stdout and stderr(std=standard).-->
<p><tt>bash</tt> には、ファイル記述子(file descriptor)として、標準入力
(<tt>stdin</tt>)、標準出力(<tt>stdout</tt>)、そして標準エラー(<tt>stderr</tt>)
の 3 つがあります。
( std は standard の意味です。)

<!--<p>Basically you can:
<enum>
<item> redirect stdout to a file
<item> redirect stderr to a file
<item> redirect stdout to a stderr 
<item> redirect stderr to a stdout 
<item> redirect stderr and stdout to a file 
<item> redirect stderr and stdout to stdout 
<item> redirect stderr and stdout to stderr
</enum>-->
<p>基本的に以下のことができます。
<enum>
<item><tt>stdout</tt> を <tt>file</tt> にリダイレクト
<item><tt>stderr</tt> を <tt>file</tt> にリダイレクト
<item><tt>stdout</tt> を <tt>stderr</tt> にリダイレクト
<item><tt>stderr</tt> を <tt>stdout</tt> にリダイレクト
<item><tt>stderr</tt> と <tt>stdout</tt> を <tt>file</tt> にリダイレクト
<item><tt>stderr</tt> と <tt>stdout</tt> を <tt>stdout</tt> にリダイレクト
<item><tt>stderr</tt> と <tt>stdout</tt> を <tt>stderr</tt> にリダイレクト
</enum>

<!--1 'represents' stdout and 2 stderr.-->
(リダイレクト構文では)数字の <tt>1</tt> は <tt>stdout</tt> を表し、
<tt>2</tt> は <tt>stderr</tt> を表します。

<!--<p> A little note for seeing this things: with the less command you can 
view both stdout (which will remain on the buffer) and the stderr that will 
be printed on the screen, but erased as you try to 'browse' the buffer. 
</sect1>-->
<p>
*上記の項目それぞれについての簡単なメモ* <tt>less</tt> コマンドを使うと、
<tt>stdout</tt> (これはバッファに残ります)と <tt>stderr</tt> (スクリーンには
表示されますが、そのバッファを見ようとすると消去されてしまいます)の両方を見る
ことができます。
	
<!--<sect1>Sample: stdout 2 file-->
<sect1>サンプル: stdout を file へ 
<!--<p> This will cause the ouput of a program to be written to a file.-->
<p>
次の場合、プログラムの出力がファイルに書き込まれます。

<!--        <tscreen><verb>
	ls -l > ls-l.txt
	</verb></tscreen>-->
        <tscreen><verb>
        $ ls -l > ls-l.txt
        </verb></tscreen>

<!--Here, a file called 'ls-l.txt' will be created and it will contain what 
you would see on the screen if you type the command 'ls -l' and execute it.
</sect1>-->
<p>
上記では <tt>ls-l.txt</tt> というファイルが作成されます。そして、'<tt>
ls -l</tt>' というコマンドを実行した場合にスクリーンに表示される
内容が、そのファイルに保存されます。 

<!--<sect1>Sample: stderr 2 file-->
<sect1>サンプル: stderr を file へ 

<!--<p> This will cause the stderr ouput of a program to be written to a 
file.-->
<p>
次の場合、プログラムの <tt>stderr</tt> の出力がファイルに書き込まれます。

<!--        <tscreen><verb>
	grep da * 2> grep-errors.txt
	</verb></tscreen>-->
        <tscreen><verb>
        $ grep da * 2> grep-errors.txt
        </verb></tscreen>

<!--Here, a file called 'grep-errors.txt' will be created and it will contain
 what you would see the stderr portion of the output of the 'grep da *' 
command.</sect1>-->
<p>
上記では、<tt>grep-errors.txt</tt> というファイルが作成されます。そして、
そのファイルには、'<tt>grep da *</tt>' の出力のうち、読者が見ようとしている
はずの <tt>stderr</tt> の部分が書き込まれています。

<!--<sect1>Sample: stdout 2 stderr-->
<sect1>サンプル: stdout を stderr へ

<!--<p> This will cause the stderr ouput of a program to be written to the 
same file descriptor than stdout.-->
<p>
次の場合、プログラムの <tt>stderr</tt> 出力が、<tt>stdout</tt> と同一の
ファイル記述子に書き込まれます。

<!--	<tscreen><verb>
	grep da * 1>&2 
	</verb></tscreen>-->
        <tscreen><verb>
        $ grep da * 1>&2
        </verb></tscreen>

<!--Here, the stdout portion of the command is sent to stderr, you may notice
that in differen ways.</sect1>-->
<p>
上記では、コマンドの <tt>stdout</tt> の部分が <tt>stderr</tt> に送られます。
別のやり方があることに読者は気付いているかもしれません。

<!--<sect1>Sample: stderr 2 stdout--> 
<sect1>サンプル: stderr から stdout へ

<!--<p> This will cause the stderr ouput of a program to be written to the 
same file descriptor than stdout.-->
<p>
次の場合、プログラムの <tt>stderr</tt> 出力が、<tt>stdout</tt> と同一のファイル
記述子に書き込まれます。

<!--	<tscreen><verb>
	grep * 2>&1
	</verb></tscreen>-->
        <tscreen><verb>
        $ grep * 2>&1
        </verb></tscreen>

<!--Here, the stderr portion of the command is sent to stdout, if you pipe to 
less, you'll see that lines that normally 'dissapear' (as they are written to 
stderr) are being kept now (because they're on stdout). </sect1>-->
<p>
上記では、コマンドの <tt>stderr</tt> の部分が <tt>stdout</tt> に送られます。
したがって、もしパイプを使って <tt>less</tt> に流せば、通常なら <tt>stderr
</tt> に書き込まれて消えてしまうはずの出力行が、 <tt>stdout</tt> に送られたこと
で保持されているのを確認できます。

<!--<sect1>Sample: stderr and stdout 2 file-->
<sect1>サンプル: stderr と stdout から file へ

<!--<p> This will place every output of a program to a file. This is suitable 
sometimes for cron entries, if you want a command to pass in absolute 
silence.-->
<p>次の場合、プログラムの全ての出力がファイルに入れられます。これは、完全に
黙ってコマンドを実行させたいような <tt>cron</tt> の実行項目を書く場合に適して
います。

<!--	<tscreen><verb>
	rm -f $(find / -name core) &> /dev/null 
	</verb></tscreen>-->
        <tscreen><verb>
        $ rm -f $(find / -name core) &> /dev/null
        </verb></tscreen>

<!--This (thinking on the cron entry) will delete every file called 'core' in 
any directory. Notice that you should be pretty sure of what a command is 
doing if you are going to wipe it's output. </sect1>-->
<p><tt>cron</tt> プログラムの実行項目を思い浮かべてほしいのですが、上記は、
あらゆるディレクトリから "<tt>core</tt>" と呼ばれるファイルをすべて削除する
ものです。
コマンドの出力を見えなくしてしまう場合は、そのコマンドがどういう処理をす
るのか充分認識しておかなければいけません。

<!--この下にある <sect>Pipes までのコメント部分は原文のままです。-->
<!--
redirect stderr and stdout to stdout 
redirect stderr and stdout to stderr
-->



<!--
	<sect1>
	Sample: 
	<p> This will 
	<tscreen><verb>
	command
	</verb></tscreen>
	Explanation
	</sect1>
-->

<!--</sect>-->


<!--######################第四章　パイプ###############################-->

<!-- pipes -->
<!--<sect>Pipes-->
<sect>パイプ (pipe)

<!--<p> This section explains in a very simple and practical way how to use 
pipes, nd why you may want it.-->
<p>この章では、簡単かつ実用的なパイプの使い方とその有用性について説明します。

<!--<sect1>What they are and why you'll want to use them-->
<sect1>何なのか、どこがいいのか

<!--<p> Pipes let you use (very simple, I insist) the output of a program as 
the input of another one</sect1>-->
<p>簡単にいうと、パイプを使えば、プログラムの出力を他のプログラムの入力として
利用できるようになります。

<!--<sect1>Sample: simple pipe with sed--> 
<sect1>サンプル: sed を使った簡単なパイプ処理

<!--<p> This is very simple way to use pipes.-->
<p>次の例は簡単なパイプの使い方を示すものです。

<!--	<tscreen><verb>
	ls -l | sed -e "s/[aeio]/u/g"	
	</verb></tscreen>-->
        <tscreen><verb>
        $ ls -l | sed -e "s/[aeio]/u/g"
        </verb></tscreen>

<!--Here, the following happens: first the command ls -l is executed, and it's
output, instead of being printed, is sent (piped) to the sed program, which in
turn, prints what it has to. </sect1>-->
<p>上記では、次のことが起こります。先ずコマンド '<tt>ls -l</tt>' が実行され、
その出力が画面に表示される代わりに、<tt>sed</tt> プログラムに送られ
(<tt>pipe</tt> 処理され)、今度は <tt>sed</tt> がそれを処理して画面に表示しま
す。

<!--<sect1>Sample: an alternative to ls -l *.txt-->
<sect1>サンプル: 'ls -l *.txt' の代替コマンド 

<!--<p> Probably, this is a more difficult way to do ls -l *.txt, but it is 
here for illustrating pipes,not for solving such listing dilema.--> 
<p>
おそらく次は、'<tt>ls -l *.txt</tt>' とするよりも難しい方法です。これを例題に
取り上げるのは、ファイルをリストアップするときのジレンマを解消するためではな
く、単にパイプの使い方を説明したいからです。

<!--	<tscreen><verb>
	ls -l | grep "&bsol;.txt&dollar;"
	</verb></tscreen>-->
        <tscreen><verb>
        $ ls -l | grep "&bsol;.txt&dollar;"
        </verb></tscreen>

<!--Here, the output of the program ls -l is sent to the grep program, which, 
in turn, will print lines which match the regex "&bsol;.txt$".</sect1>-->
<p>
上記では、'<tt>ls -l</tt>' プログラムの出力が <tt>grep</tt> プログラムに送ら
れ、そこで "<tt>&bsol;.txt&dollar;</tt>" の正規表現に合致する行が画面に出力さ
れます。
(訳注: 上記で円マークが表示される場合、それはバックスラッシュの意味です。)



<!-- 以下の Variable までのコメントは、原文にもとからあったものです。-->
<!--
	<sect1>
	Sample: 
	<p> This will 
	<tscreen><verb>
	command
	</verb></tscreen>
	Explanation
	</sect1>
-->


<!--</sect>-->




<!-- Variables -->

<!--#############################第五章　変数#########################-->


<!--<sect>Variables-->
<sect>変数 (variable)

<!-- Dry Theory --><!--左のコメントアウトはもともとです。-->
<!--<P> You can use variables as in any programming languages. There are no 
data types. A variable in bash can contain a number, a character, a string of 
characters. <P>  You have no need to declare a variable, just assigning a 
value to its reference will create it.-->
<p>
変数を使えないプログラム言語はありません。ただ、<tt>bash</tt> にはデータ
型(data type)がありません。<tt>bash</tt> の変数は、数字、文字、文字列を格納
することができます。
<p>変数を宣言する必要はなく、参照すべき変数に値を代入すれば、それだけで
作成できます。
	

    <!-- Samples --><!-- 左のコメントはもともとです-->
<!--<sect1>Sample: Hello World! using variables-->
<sect1>サンプル: 変数を使った Hello World!
<p>
        <tscreen><verb>
        #!/bin/bash
        STR="Hello World!"
        echo $STR
        </verb></tscreen>

<!--<P> Line 2 creates a variable called STR and assigns the string "Hello 
World!" to it. Then the VALUE of this variable is retrieved by putting 
the '$' in at the beginning. Please notice (try it!) that if you don't use 
the '$' sign, the output of the program will be different, and probably not 
what you want it to be.</sect1>-->
<p>
2 行目で <tt>STR</tt> という変数を作って、それに <tt>"Hello World"</tt> とい
う文字列を代入しています。この変数の「値」は変数の最初に "<tt>$</tt>" を置く
ことで取り出せます。
もし "<tt>$</tt>" という記号を使わなかったらプログラムの出力は違うものとな
り、望んだ結果を得られないということを理解して(あるいは実際にやってみて)くだ
さい。


<!--<sect1>Sample: A very simple backup script (little bit better)-->
<sect1>サンプル: 非常にシンプルなバックアップスクリプト(改良版)

 <P> 
        <tscreen><verb>
        #!/bin/bash          
	OF=/var/my-backup-$(date +%Y%m%d).tgz
	tar -cZf $OF /home/me/
	</verb></tscreen>

<!--<P> This script introduces another thing. First of all, you should be 
familiarized with the variable creation and assignation on line 2. Notice the 
expression '$(date +%Y%m%d)'. If you run the script you'll notice that it runs
the command inside the parenthesis, capturing its output. -->
<p>
上記のスクリプトは、今までとは少し違うことを紹介しています。まず二行目にある
変数の作成と代入についてはもう慣れたと思います。
では、"<tt>$(date +%Y%m%d)</tt>" という表現に注目してください。
スクリプトを実行すると、丸カッコのなかのコマンドが実行されて、その出力が
取り込まれるようになっています。

<!--<P> Notice that in this script, the output filename will be different 
every day, due to the format switch to the date command(+%Y%m%d). You can 
change this by specifying a different format.
<P> Some more examples:
<P> echo ls
<P> echo $(ls)</sect1>-->
<p>
このスクリプトでは、<tt>date</tt> コマンドの(+%Y%m%d)という書式スイッチに従っ
て、出力されるファイル名が毎日変わることに注意してください。違った書式を指定
することで、これとは異なるファイル名にすることもできます。
<p>それ以外の参考事例として、
<itemize>
<item><tt>echo ls</tt>
<item><tt>echo &dollar;(ls)</tt>
</itemize>
といったものがあります。


<!--<sect1>Local variables-->
<sect1>ローカル変数

<!--<p> Local variables can be created by using the keyword <it/local/.-->
<p>ローカル変数は、<tt>local</tt> というキーワードを使って作成することができ
ます。
	<tscreen><verb>
        #!/bin/bash
	HELLO=Hello 
	function hello {
		local HELLO=World
		echo $HELLO
	}
	echo $HELLO
	hello
	echo $HELLO
	</verb></tscreen>
<!--<p> This example should be enought to show how to use a local variable. 
</sect1></sect>-->
<p>ローカル変数の使い方については、以上の例で充分かと思います。




<!--#####################６章　条件制御################################-->


<!-- Conditionals -->
<!--<sect>Conditionals-->
<sect>条件制御(conditionals)
    
<!--<P> Conditionals let you decide whether to perform an action or not, this 
decision is taken by evaluating an expression.-->
<p>条件制御を使えば、ある命令を実行をすべきかどうかの決定ができます。
そして、この決定は、式の評価によりなされます。
    <!-- Dry Theory -->

<!--<sect1>Dry Theory-->
<sect1>セオリー

<!--<P> Conditionals have many forms. The most basic form is: <bf>if</bf> 
<it/expression/ <bf>then</bf> <it/statement/ where 'statement' is only 
executed if 'expression' evaluates to true. '2<1' is an expresion that 
evaluates to false, while '2>1' evaluates to true.xs-->
<p>
条件制御には、多くの形式があります。最も基本的な形式は、「<tt>if</tt> 条件式 
<tt>then</tt> 実行文」 であり、この実行文は
条件式の評価が真であるときだけ実行されます。&dquot; <tt>2 &lt; 1</tt> 
&dquot; というのは偽と評価される条件式であり、&dquot; <tt>2 &gt; 1</tt> 
&dquot; というのは真と評価される条件式です。
	
<!--<p> Conditionals have other forms such as: <bf/if/ <it/expression/ 
<bf/then/ <it/statement1/ <bf/else/ <it/statement2/.  Here 'statement1' is 
executed  if 'expression' is true,otherwise 'statement2' is executed.-->
<p>
また、条件制御は、「<tt>if</tt> 条件式 <tt>then</tt> 実行文<tt>1 </tt> 
<tt>else</tt> 実行文<tt>2 </tt>」という形式もとりま
す。ここでは、条件式が
真のとき実行文 <tt>1</tt> が実行され、それ以外のときは実行文 <tt>2</tt> が実行されます。

<!--<P> Yet another form of conditionals is: <bf/if/ <it/expression1/ 
<bf/then/ <it/statement1/ <bf/else if/ <it/expression2/ <bf/then/ 
<it/statement2/ <bf/else/ <it/statement3/. In this form there's added only the
"ELSE IF 'expression2' THEN 'statement2'" which makes statement2 being
executed if expression2 evaluates to true. The rest is as you may imagine 
(see previous forms).-->
<p>
条件制御にはさらに別の形式もあります。「<tt>if</tt> 条件式<tt>1 </tt> 
<tt>then</tt> 実行文<tt>1 </tt> <tt>else if</tt> 条件式<tt>2 </tt> 実行文
<tt>2 </tt> <tt>else</tt> 実行文<tt>3 </tt>」 です。この形式では、
&dquot;<tt>else if</tt> 条件式<tt>2 </tt> <tt>then</tt> 実行文<tt>2 </tt>
&dquot; というのが加わっただけですが、これはもし条件式<tt>2 </tt>の評価が真
ならば実行文<tt>2 </tt> を実行するものです。
その他の部分については、読者の想像通りです。(これまでの形式をご覧ください)

<!--	<P> A word about syntax:
	<P> The base for the 'if' constructions in bash is this:
	<P> if [expression];
	<P> then
	<P>    code if 'expression' is true.
	<P> fi </sect1>-->
<p>構文に関してですが、
<tt>bash</tt> で <tt>if</tt> 制御構造を使うときの基本は以下のようなものです。
<verb>
        if [条件式]
        then
        (if の条件が真だったときの)実行コード
        fi
</verb>    

<!-- Samples -->
<!--<sect1>Sample: Basic conditional example if .. then-->
<sect1>サンプル: if .. then による基本的な条件制御の例

<P> 
        <tscreen><verb>
	#!/bin/bash
	if [ "foo" = "foo" ]; then
	   echo expression evaluated as true
	fi
	</verb></tscreen>

<!--<P> The code to be executed if the expression within braces is true can 
be found after the 'then' word and before 'fi' which indicates the end of the 
conditionally executed code.</sect1>-->
<p>
ブレース [ ] 内の条件式が真であった場合、&dquot;<tt>then</tt>&dquot; という
文字の後に、実行されるコードが書かれています。そしてその後に、条件制御に基づ
いて実行されるコードの終了を指示する &dquot;<tt>fi</tt>&dquot; が置かれて
います。
	
<!--<sect1>Sample: Basic conditional example if .. then ... else-->
<sect1>サンプル: "if .. then ... else"  を使った基本的な条件制御の例
        <P>
	<tscreen><verb>
	#!/bin/bash
	if [ "foo" = "foo" ]; then
	   echo expression evaluated as true
	else
	   echo expression evaluated as false
	fi
	</verb></tscreen>
	</sect1>

<!--<sect1>Sample: Conditionals with variables-->
<sect1>サンプル: 変数を伴った条件制御
        <P> 
	<tscreen><verb>
	#!/bin/bash
	T1="foo"
	T2="bar"
	if [ "$T1" = "$T2" ]; then
	    echo expression evaluated as true
	else
	    echo expression evaluated as false
	fi
	</verb></tscreen>
	</sect1>

<!-- 下記のコメント部分も原文のままです。-->
<!--
	<sect1>
	Sample: testing if a file exists 
	    <P> one more thank to mike 
	    <tscreen><verb>
	    #!/bin/bash
	    FILE=~/.basrc
	    if [ -f $FILE ]; then
	        echo file $FILE exits
	    else
	        echo file not found
	    fi
	    if [ 'test -f $FILE']
	    </verb></tscreen>
	</sect1>
-->

</sect>


<!--###########第 7 章 for, while, until によるループ #####################-->


<!-- Loops : for, while and until -->
<!--<sect>Loops for, while and until-->
<sect>for, while, until によるループ
    <!-- Dry Theory -->

<!--    <P> In this section you'll find for, while and until loops.
    <P> The <bf/for/ loop is a little bit different from other programming
    languages. Basically, it let's you iterate over a series of 
    'words' within a string.-->
<p>この章では、<tt>for</tt>, <tt>while</tt>, そして <tt>util</tt> によるルー
プについて述べます。
<tt>bash</tt> の <tt>for</tt> は、他の言語での使い方と若干異なります。基本的に
は、ある文字列内の単語をあたまから順に反復処理するものです。

<!--  <P> The <bf/while/ executes a piece of code if the control expression
      is true, and only stops when it is false (or a explicit break is found
      within the executed code.-->
<p>
<tt>while</tt> は、制御式が真の間はコードを実行していて、偽になったとき
(あるいは実行コード内に <tt>break</tt> が明示されている場合)にのみ停止します。

<!--   <P> The <bf/until/ loop is almost equal to the while loop, except that
      the code is executed while the control expression evaluates to false.
      <P> If you suspect that while and until are very similar you are 
      right.-->
<p><tt>until</tt> ループは、制御式が偽と評価されている間はコードが実行される
という点を除くと、<tt>while</tt> ループとほぼ同じです。<tt>while</tt> と 
<tt>until</tt> が似ていると思ったなら、それは正しい考えです。

    <!-- Samples -->    
<!--<sect1>For sample-->
<sect1>For のサンプル
        <P> 
	<tscreen><verb>
        #!/bin/bash
        for i in $( ls ); do
            echo item: $i
        done
        </verb></tscreen>
        <p> 
<!--<p> On the second line, we declare i to be the variable that will take the 
        different values contained in $( ls ).
        <p> The third line could be longer if needed, or there could be more 
        lines before the done (4).
        <p> 'done' (4) indicates that the code that used the value of $i has
        finished and $i can take a new value.
        <p> This script has very little sense, but a more useful way to use the
        for loop would be to use it to match only certain files on the previous
         example<p></sect1>-->
<p>
二行目で、変数 <tt>i</tt> が宣言されています。これには、<tt>$( ls )</tt> によっ
て出力される様々な値が(順番に)代入されていきます。
三行目は、必要であればもっと長くできます。すなわち、四行目の <tt>done</tt> 
の前に複数の行が存在してもかまいません。
四行目の <tt>done</tt> は、変数 <tt>$i</tt> の値を使用していたコードがそこで
終了したことを示していています。したがって、<tt>$i</tt> に新しい値を代入する
ことが可能です。
上記は、はほとんど意味がないスクリプトです。<tt>for</tt> ループを使用するもっと
有益な方法は、以前の例題にあったように、ある種のファイルだけを選択的に処理す
る場合に使うことだと思います。
    
<!--<sect1>C-like for-->
<sect1>C 言語に似た for

<!--<P> fiesh suggested adding this form of looping. It's a for loop
	more similar to C/perl... for.-->
<p>Fiesh さんから以下のような形式のループを付け加えたらどうかと言われました。
この <tt>for</tt> ループは、C 言語や Perl の <tt>for</tt> ループに類似した
使われかたをしています。

        <tscreen><verb>
	#!/bin/bash
	for i in `seq 1 10`;
	do
		echo $i
	done	
	</verb></tscreen>
	<!--</sect1>-->

    <!-- while -->
<!--<sect1> While sample-->
<sect1>while のサンプル

        <P> <tscreen><verb>
        #!/bin/bash 
        COUNTER=0
        while [  $COUNTER -lt 10 ]; do
            echo The counter is $COUNTER
            let COUNTER=COUNTER+1 
        done
        </verb></tscreen>
	<P>
<!--<P> This script 'emulates' the well known 
(C, Pascal, perl, etc) 'for' structure</sect1>-->
<p>上記のスクリプトは、(C, Pascal, Perl といった)有名な言語の <tt>for</tt> の
制御構造を真似たものです。

    <!-- until -->
<!--<sect1>Until sample-->
<sect1>until のサンプル
<p>
        <tscreen><verb>
        #!/bin/bash 
        COUNTER=20
        until [  $COUNTER -lt 10 ]; do
            echo COUNTER $COUNTER
            let COUNTER-=1
        done
	</verb></tscreen>
<!--</sect1></sect>-->


<!--########################第八章　関数#################################-->

<!-- Functions -->
<!--<sect>Functions-->
<sect>関数(functions)

<!--<P> As in almost any programming language, you can use functions to group 
pieces of code in a more logical way or practice the divine art of recursion.
--><p>
ほとんどどんなプログラムに当てはまることですが、関数(function)を使えば、より
論理的に一群のコードをグループ化することができます。また、神秘的ともいえる
再帰法(recursion)を使うことも可能になります。

<!--<P> Declaring a function is just a matter of writing function my_func 
{ my_code }.-->
<p>
関数の宣言は、 &dquot;<tt>function my_func { my_code }</tt>&dquot; と
書くだけでできます。

<!--<P> Calling a function is just like calling another program, you just 
write its name.-->
<p>
関数の呼び出しは、他のプログラムの呼び出しと同じで、その関数名を書くだけ
です。

<P>
<!--<sect1>Functions sample-->
<sect1>関数のサンプル
        <P> <tscreen><verb>
	#!/bin/bash 
	function quit {
	    exit
	}
	function hello {
	    echo Hello!
	}
	hello
	quit
	echo foo 
	</verb></tscreen>

<!--<P> Lines 2-4 contain the 'quit' function. Lines 5-7 contain the 'hello' 
function If you are not absolutely sure about what this script does, please 
try it!.-->
<p>
二行目から四行目にかけては、&dquot;<tt>quit</tt>&dquot; という関数が記述
されています。五行目から七行目にかけては、&dquot;<tt>hello</tt>&dquot; という
関数が記述されています。このスクリプトが何をしているか完全に把握できないな
ら、実際に自分で書いて、実行してみてください。

<!--<P> Notice that a functions don't need to be declared in any specific 
order.-->
<p>
関数は、何らかの特定の様式による宣言を必要としないということに注意してくだ
さい。

<!--<P> When running the script you'll notice that first: the function 'hello'
is called, second the 'quit' function, and the program never reaches line 10.
</sect1>-->
<p>
このスクリプトを実行すると、最初に "<tt>hello</tt>" 関数が呼び出され、次に 
"<tt>quit</tt>" 関数が呼び出されます。そしてプログラムは十行目までは実行され
ないことに気付くでしょう。

<!--<sect1>Functions with parameters sample-->
<sect1>パラメーターを取る関数のサンプル

        <P> 
        <tscreen><verb>
	#!/bin/bash 
	function quit {
 	    exit
	}  
	    function e {
		echo $1 
	    }  
	e Hello
	e World
	quit
	echo foo 

        </verb></tscreen>

<!--<P> This script is almost identically to the previous one. The main 
difference is the funcion 'e'. This function, prints the first argument it 
receives. Arguments, within funtions, are treated in the same manner as 
arguments given to the script.</sect1></sect>-->
<p>
上記のプログラムは、前記のプログラムとほぼ同一です。主な違いは、
関数 "<tt>e</tt>" の存在です。この関数は受け取った第一引数を表示します。
引数は、関数の内部にあるときでも、スクリプトに直接渡したときと同じように扱わ
れます。


<!--#####################第九章　ユーザインターフェイス###############-->

<!-- User interfaces -->
<!--<sect>User interfaces-->
<sect>ユーザインターフェイス
     <!-- Simple menus with select -->

<!--<sect1>Using select to make simple menus-->
<sect1>簡単なメニューを作るために select を使う

        <P>
	<tscreen><verb>
	#!/bin/bash
	OPTIONS="Hello Quit"
	select opt in $OPTIONS; do
	    if [ "$opt" = "Quit" ]; then
	      echo done
	      exit
	    elif [ "$opt" = "Hello" ]; then
	      echo Hello World
	    else
	      clear
	      echo bad option
	    fi
	done
        </verb></tscreen> 

<!--<P> If you run this script you'll see that it is a programmer's dream for 
text based menus. You'll probably notice that it's very similar to the 'for' 
construction, only rather than looping for each 'word' in $OPTIONS, it prompts
the user.<P></sect1>-->
<p>
もし上記のスクリプトを実行したなら、テキストベースのメニューというプログラマ
のひとつの夢が実現しているのが分かると思います。また <tt>for</tt> の制御構造
と似ていることにも気付いたかもしれません。<tt>$OPTIONS</tt> に代入された語
(word)をひとつずつループさせる代わりに、プロンプトを出力しているという違いが
あるだけだからです。

    <!-- Using the command line -->
<!--<sect1>Using the command line-->
<sect1>コマンドラインを使ってみる 

        <P>
	<tscreen><verb>
        #!/bin/bash        
        if [ -z "$1" ]; then 
            echo usage: $0 directory
            exit
        fi
        SRCD=$1
        TGTD="/var/backups/"
        OF=home-$(date +%Y%m%d).tgz
        tar -cZf $TGTD$OF $SRCD
	</verb></tscreen>

<!--<P> <P> What this script does should be clear to you. The expression in 
the first conditional tests if the program has received an argument ($1) and 
quits if it didn't, showing the user a little usage message. The rest of the 
script should be clear at this point.</sect1></sect>-->
<p>上記のスクリプトが何をするかは、もうお分かりでしょう。最初の条件制御の式は、
プログラムが引数 <tt>($1)</tt>を受け取ったかどうかを調べるものです。そして、
引数を受け取らなかったときは簡単な使用方法についてのメッセージを表示して終
了します。スクリプトの残りの部分は、この時点では明瞭だと思います。


<!--#########################十章　その他のスクリプト##################-->


<!-- Misc -->
<!--<sect>Misc-->
<sect>その他のスクリプト
    <!-- Reading user input  -->    

<!--<sect1>Reading user input with read-->
<sect1>read を使ってユーザの入力を読み込む

<!--<P> In many ocations you may want to prompt the user for some input, and 
there are several ways to achive this. This is one of those ways:-->
<p>
ユーザに何らかの入力を促したいと思う場合がよくあるものです。それを実現する方
法はいくつか存在しますが、以下はそれらのうちのひとつです。

        <tscreen><verb>
        #!/bin/bash
        echo Please, enter your name
        read NAME
        echo "Hi $NAME!"
	</verb></tscreen>

<!--<P> As a variant, you can get multiple values with read, this example may 
clarify this.-->
<p>上記の変形として、<tt>read</tt> を使って複数の値を受け取ることもできます。
次の例題はそれを明らかにするものです。

	<tscreen><verb>
        #!/bin/bash
        echo Please, enter your firstname and lastname
        read FN LN 
        echo "Hi! $LN, $FN !"
	</verb></tscreen>
	
<!--    </sect1>-->

    <!-- Arithmetic evaluation  -->    
<!--<sect1>Arithmetic evaluation-->
<sect1>数値の評価

<!--     <P> On the command line (or a shell) try this:
	 <P> echo 1 + 1
	 <P> If you expected to see '2' you'll be disappointed. What if 
	 you want BASH to evaluate some numbers you have? The solution
	 is this:
	 <P> echo $((1+1))-->
<p>
コマンドライン(あるいはシェル)で、次のように打ってみてください。

        <tscreen><verb>
        $ echo 1 + 1
        </verb></tscreen>
<p>
<tt>2</tt> が出力されると期待していたなら、がっかりしたことでしょう。
<tt>bash</tt> に数値を計算させたいときは、どうしたらいいのでしょう？
解決策は次のようにすることです。
<!--        <tscreen><verb>
        echo $(( 1 + 1))
        </verb></tscreen>-->
        <tscreen><verb>
        $ echo $((1 + 1))
        </verb></tscreen>

<!--	 <P> This will produce a more 'logical' output. This is to evaluate an
	 arithmetic expression. You can achieve this also like this:
	 <P> echo $[1+1]
	 <P>
	 <P> If you need to use fractions, or more math or you just want it, 
         you can use bc to evaluate arithmetic expressions.
	 <P> if i ran "echo $[3/4]" at the command prompt, it would return 0 
	 because bash  only uses integers when answering. If you  ran
	  "echo 3/4|bc -l", it would properly return 0.75.</sect1>-->
<p>
上記では、よりロジカルな出力がなされたことと思います。これは数式を評価した
ということです。また、次のような表現でも同じことができます。

        <tscreen><verb>
        $ echo $[ 1 + 1 ]
        </verb></tscreen>

<p>
分数やあるいはもっと複雑な数学的処理が必要になった場合、あるいは数式で遊び
たいときなどは、数式評価のための <tt>bc</tt> というコマンドが使えます。
コマンドプロンプト上で "<tt>echo $[3/4]</tt>" と実行しても、<tt>bash</tt> は
整数でしか答えを返せないので、結果は <tt>0</tt> となります。
しかし、"<tt>echo 3/4 | bc -l</tt>" と実行すれば、<tt>0.75</tt> という正しい答
えが返ってきます。



<!--<sect1>Finding bash-->
<label id="finding_bash">
<sect1>bash を探す  
<!--<P> From a message from mike (see Thanks to)-->
<p>Mike からのメッセージを掲載します。(謝辞を見てください) 

<!--    <P> you always use #!/bin/bash .. you might was to give an example of
        <P> how to find where bash is located.
        <P> 'locate bash' is preferred, but not all machines have locate.
        <P> 'find ./ -name bash' from the root dir will work, usually.-->
<p>
あなたはいつも <tt>#!/bin/bash</tt> というのを使っていますが、<tt>bash</tt> 
がどこにあるのか探す方法を例示したほうがいいと思います。"<tt>bash</tt> をその
場所に置いてしまえばいいのですが、すべてのマシンがそれをしているわけではあり
ません。ルートディレクトリから、'<tt>find ./ -name bash</tt>' を実行すれば通
常は探し出せるはずです。

<!--    <P> Suggested locations to check:
        <P>         ls -l /bin/bash
        <P>         ls -l /sbin/bash
        <P>         ls -l /usr/local/bin/bash
        <P>         ls -l /usr/bin/bash
        <P>         ls -l /usr/sbin/bash
        <P>         ls -l /usr/local/sbin/bash
        <P> (can't think of any other dirs offhand...  i've found it in
        <P> most of these places before on different system).
	<P> You may try also 'which bash'.</sect1>-->
<p>チェックすべきなのは、次のような場所でしょう。
        <tscreen><verb>
        ls -l /bin/bash
        ls -l /sbin/bash
        ls -l /usr/lodal/bin/bash
        ls -l /usr/bin/bash
        ls -l /usr/sbin/bash
        ls -l /usr/local/sbin/bash
        </verb></tscreen>
<p>(これ以外のディレクトリはすぐに思い浮かびません... システムが違っても、
これまではこの中から見つかる場合がほとんどでした。) 
それから、'<tt>which bash</tt>' を使ってみるのもいいと思います。 

    <!-- Capturing a commands output  -->    
<!--<sect1>Getting the return value of a program-->
<sect1>プログラムの戻り値を獲得する方法

<!--<P> In bash, the return value of a program is stored in a special variable
called $?.
<P> This illustrates how to capture the return value of a program, I assume 
that the directory <it/dada/ does not exist. 
(This was also suggested by mike)-->
<p>
<tt>bash</tt> では、プログラムの戻り値を <tt>$?</tt> という特殊な変数に入れて
おくことができます。
以下は、プログラムの戻り値を獲得する方法を書いたものです。ただし、
<tt>/dada</tt> というディレクトリは実在しません。(これも Mike からの提案を
載せたものです)

	<tscreen><verb>
	#!/bin/bash
	cd /dada &> /dev/null
	echo rv: $?
	cd $(pwd) &> /dev/null
	echo rv: $?
	</verb></tscreen>
<!--</sect1>-->
 
 
    <!-- Capturing a commands output  -->    
<!--sect1>Capturing a commands output-->
<sect1>コマンド出力を獲得する方法 

<!--<P> This little scripts show all tables from all databases (assuming you 
got MySQL installed). Also, consider changing the 'mysql' command to use a 
valid username and password.-->
<p>
以下の簡単なスクリプトは、すべてのデータベース上を探して全部のテーブルを
表示するというものです( <tt>MySQL</tt> がインストールされていることが前提で
す)。
この中の '<tt>mysql</tt>' というコマンド部分を変更して、有効なユーザ名とパス
ワードが使えるようにしてください。

	<tscreen><verb>
	#!/bin/bash
	DBS=`mysql -uroot  -e"show databases"`
	for b in $DBS ;
	do
	        mysql -uroot -e"show tables from $b"
	done
	</verb></tscreen>

<!--</sect1>-->
    
    <!-- Multiple source files -->
<!--<sect1>Multiple source files-->
<sect1>複数のソースファイル

<!--    <P> You can use multiple files with the command source. 
	<P> __TO-DO__</sect1></sect>-->
<p>コマンドソースとして複数のファイルを使うことができます。
<p>作成予定( TO DO )


<!--#########################十一章　参照テーブル#######################-->

<!-- Tables -->
<!--<sect>Tables-->
<sect>参照テーブル

    <!-- String operators -->

<!--<sect1>String comparison operators-->
<sect1>文字列比較演算子
<p>    
<!--    <P> (1) s1 = s2
    <P> (2) s1 != s2
    <P> (3) s1 < s2
    <P> (4) s1 > s2
    <P> (5) -n s1 
    <P> (6) -z s1 
    <P> 
    <P> (1) s1 matches s2
    <P> (2) s1 does not match s2
    <P> (3) __TO-DO__
    <P> (4) __TO-DO__
    <P> (5) s1 is not null (contains one or more characters)
    <P> (6) s1 is null 
    </sect1>-->
<verb>
        (1) s1  =  s2    s1 と s2 が同一
        (2) s1 !=  s2    s1 と s2 が同一でない
        (3) s1  <  s2    作成中 
        (4) s1  >  s2    作成中
        (5) -n s1        s1 が null でない(一文字以上を含んでいる)
        (6) -z s1        s1 が null である
</verb>

<!--<sect1>String comparison examples-->
<sect1>文字列比較の実例

<!--<p> Comparing two strings.-->
<p>ふたつの文字列を比較する 

        <tscreen><verb>
	#!/bin/bash
	S1='string'
	S2='String'
	if [ $S1=$S2 ];
	then
	        echo "S1('$S1') is not equal to S2('$S2')"
	fi
	if [ $S1=$S1 ];
	then
	        echo "S1('$S1') is equal to S1('$S1')"
	fi
	</verb></tscreen>

<!--<p> I quote here a note from a mail, sent buy Andreas Beck, refering to use
<it/if [ $1 = $2 ]/.-->
<p>ここで、上記の <tt>if [ $1 = $2 ] </tt>に関して Andrea Beck が送ってくれた
メールの一文を引用します。

<!--<p>This is not quite a good idea, as if either $S1 or $S2 is empty, you 
will get a parse error. x$1=x$2 or "$1"="$2" is better.</sect1>-->
<p>
<verb>
      あまりいい考えとは言えません。もし、$S1 か $S2 のどちらかが空の場合、
      パーサエラーになるからです。x$1=x$2 か "$1"="$2" としたほうがよいで
      しょう。
</verb>


    <!-- Arithmetic operators -->    
<!--<sect1>Arithmetic operators-->
<sect1>数値演算子
<p>
<!--    <P> +
    <P> -
    <P> *
    <P> /
    <P> % (remainder)-->
        <verb>
        +
        -
        *
        /
        % (剰余)
        </verb>
<!--</sect1>-->

    
<!-- Arithmetic relational operators -->    
<!--<sect1>Arithmetic relational operators-->
<sect1>数値関係演算子
<p>
<!--    <P> -lt (<) 
    <P> -gt (>)
    <P> -le (<=)
    <P> -ge (>=)
    <P> -eq (==)
    <P> -ne (!=)-->
        <verb>
        -gt  ( >  )
        -le  ( <= )
        -ge  ( >= )
        -eq  ( == )
        -ne  ( != )
        </verb>

<!--<P> C programmer's should simple map the operator to its corresponding 
parenthesis.</sect1>-->
C プログラマが使っている演算子についても、丸カッコを付けて対応関係を示しまし
た。
    

    <!-- Useful commands  -->    
<!--<sect1>Useful commands-->
<sect1>便利なコマンド

<!--<P> This section was re-written by Kees (see thank to...) 
<P>  Some of these command's almost contain complete programming languages. 
From those commands only the basics will be explained. For a more detailed 
description, have a closer look at the man pages of each command.-->
<p>
このセクションは Kees (謝辞を見てください)によってリライトされたものです。
<p>
以下のコマンドのなかには、完全なプログラミング言語を内包しているといえそう
なものもあります。それらのコマンドを取り上げて、基本的用法だけを説明しよと
思います。詳細な解説については、コマンドのマニュアルページをみてください。

	<!--<bf/sed/ (stream editor)-->
<sect2>sed (stream editor)

<!--<P>Sed is a non-interactive editor. Instead of altering a file by moving 
the cursor on the screen, you use a script of editing instructions to sed, 
plus the name of the file to edit. You can also describe sed as a filter. 
Let's have a look at some examples:-->
<p>
<tt>sed</tt> はノン・インタラクティブなエディタです。カーソルを画面上で動かし
ながらファイルに変更を加えるかわりに、編集に関する指示と編集すべきファイル名
を書き込んだスクリプトを使用します。また、<tt>sed</tt> は一種のフィルタである
と考えることもできます。いくつか例を見てみましょう。

   	<tscreen><verb>
	$ sed 's/to_be_replaced/replaced/g' /tmp/dummy
	</verb></tscreen>

<!--<P> Sed replaces the string 'to_be_replaced' with the string 'replaced' 
and reads from the /tmp/dummy file. The result will be sent to stdout 
(normally the console) but you can also add '> capture' to the end of the 
line above so that sed sends the output to the file 'capture'.-->
<p>
上記において、<tt>sed</tt> は、<tt>/tmp/dummy</tt> というファイルを読み込ん
で、文字列 "<tt>to_be_replaced</tt>" を文字列 "<tt>replaced</tt>" に置換しま
す。その結果は標準出力(通常はコンソール)に出力されますが、上記コマンドライン
の末尾に(例えば) '<tt>&gt; capture</tt>' と付け加えるなら、 "<tt>capture</tt>" 
という名前のファイルに <tt>sed</tt> の出力結果を送ることもできます。

        <tscreen><verb>
        $ sed 12, 18d /tmp/dummy
        </verb></tscreen>	

<!--<P> Sed shows all lines except lines 12 to 18. The original file is not 
altered by this command.-->
<p>
上記の例で、<tt>sed</tt> は <tt>12</tt> から <tt>18</tt> 行目までを除く全ての
行を表示します。このコマンドによって元のファイルに変更が加えられることはあり
ません。

<!--	<bf/awk/ (manipulation of datafiles, text retrieval and processing)-->
<sect2>awk (データファイルやテキストの検索と整形処理)

<!--<P> Many implementations of the AWK programming language exist (most known
interpreters are GNU's gawk and 'new awk' mawk.) The principle is simple: AWK 
scans for a pattern, and for every matching pattern a action will be performed.
<P> Again, I've created a dummy file containing the following lines:-->
<p>
<tt>AWK</tt> プログラミング言語には多くの実装が存在します。(最も有名なイン
タープリタは、GNU の <tt>gawk</tt> と「新しい <tt>awk</tt>」 である 
<tt>mawk</tt> です。) 　
<tt>AWK</tt> の原理は単純です。パターンを探して、合致したパターン(pattern)全
てに対してなんらかの処理(action)を行うということです。
<p>
再度、以下のような行を含むダミーファイルを作りました。

<!--	<P> <it/"test123/
   	<P> <it/test/
	<P> <it/tteesstt"/-->
        <verb>
        test123
        test
        tteesstt 
        </verb>
<!--
   	<tscreen><verb>
	$awk '/test/ {print}' /tmp/dummy
	</verb></tscreen>
   	<P> test123

   	<P> test
-->
        <tscreen><verb>
        $ awk '/test/ {print}' /tmp/dummy
        test123
        test
        </verb></tscreen>

<!--<P> The pattern AWK looks for is 'test' and the action it performs when it
 found a line in the file /tmp/dummy with the string 'test' is 'print'.-->
<p>
上記において、<tt>AWK</tt> が探そうとしたパターン(pattern)は "<tt>test</tt>" 
であり、<tt>/tmp/dummy</tt> ファイルの行の中から "<tt>test</tt>" という文字列
を探し出したときに行った処理(action)が画面への表示 "<tt>print</tt>" です。

<!--
   	<tscreen><verb>
	$awk '/test/ {i=i+1} END {print i}' /tmp/dummy
	</verb></tscreen>

   	<P> 3
-->
        <tscreen><verb>
        $ awk '/test/ {i=i+1} END {print i}' /tmp/dummy
        3
        </verb></tscreen>

<!--<P> When you're searching for many patterns, you should replace the text 
between the quotes with '-f file.awk' so you can put all patterns and actions 
in 'file.awk'.-->
<p>
数多くのパターンを探そうとしているときは、クオテーション('')で囲まれた文字列
を(例えば) '<tt>-f file.awk</tt>' といったファイル名で置き換えます。
そうすれば、すべてのパターンと処理を '<tt>file.awk</tt>' というファイルに書き
込んで、まとめて実行できます。


<!--   	<bf/grep/ (print lines matching a search pattern)-->
<sect2>grep (検索パターンに合致する行の出力)

<!--<P>We've already seen quite a few grep commands in the previous chapters, 
that display the lines matching a pattern. But grep can do more.-->
<p>
<tt>grep</tt> コマンドの、あるパターンに合致する行を表示する機能については、
これまでの章でいくつも見てきたと思います。しかし、<tt>grep</tt> の機能はそれ
だけではありません。

<!--
   	<tscreen><verb>
   	$grep "look for this" /var/log/messages -c
	</verb></tscreen>

        <P> 12
-->
        <tscreen><verb>
        $ grep "look for this" /var/log/messages -c
        12
        </verb></tscreen>

<!--<P> The string "look for this" has been found 12 times in the file 
/var/log/messages.-->
<!--<P> [ok, this example was a fake, the /var/log/messages was tweaked :-)]-->
<p>
上記の例は、"<tt>look for this</tt>" という文字列が <tt>
/var/log/messages</tt> ファイルのなかで <tt>12</tt> 回見つかったことを意味し
ています。
<p>
[ OK, この例題はフェイクなんだ。<tt>/var/log/messages</tt> にちょっと手を加え
といたのさ。 :-)]


<!--   <bf/wc/ (counts lines, words and bytes)-->
<sect2>wc (行、語、バイト数を数えます)

<!--<P> In the following example, we see that the output is not what we 
expected. The dummy file, as used in this example, contains the following text:
       	<it/"bash introduction/
     	<it/ howto test file"/-->
<p>
以下の例題では、出力が期待通りになっていないのが分かると思います。この例題で
使われたダミーファイルには(前に使った内容とは異なる)次のような文字列が含まれ
ていたからでらす。
        <verb>
        bash introduction 
        howto test file 
        </verb>

<!--   <tscreen><verb>
	$wc &#045;-words &#045;-lines &#045-bytes /tmp/dummy
	</verb></tscreen>

   <P> 2 5 34 /tmp/dummy
-->
        <tscreen><verb>
        $ wc --words --lines --bytes /tmp/dummy
        2 5 34 /tmp/dummy
        </verb></tscreen>


<!--<P> Wc doesn't care about the parameter order. Wc always prints them in a 
standard order, which is, as you can see: &lt;lines&gt;&lt;words&gt;&lt;bytes
&lt;&gt;filename&gt;.-->
<p>
<tt>wc</tt> ではパラメータ指定の順番は自由ですが、それらの出力時の順番はいつも
決まっています。上記のように、行数、語数、バイト数、ファイル名 の順番に
なります。

<!--   <bf/sort/ (sort lines of text files)-->
<sect2>sort (テキストファイル内の行の並べ替え)

<!--<P> This time the dummy file contains the following text:-->
<p>今回のダミーファイルの内容は次のようになっています。

<!--
   <P> <it/"b/
   <P> <it/c/
   <P> <it/a"/
   	<tscreen><verb>
	$sort /tmp/dummy
	</verb></tscreen>
-->
        <verb>
        b
        c
        a 
        </verb>
        <tscreen><verb>
        $ sort /tmp/dummy
        </verb></tscreen>

<!--<P> This is what the output looks like:-->
<p>次は、そのアウトプットがどう表示されるかです。

<!--
   <P> <it/a/
   <P> <it/b/
   <P> <it/c/
-->
        <tscreen><verb>
        a
        b
        c
        </verb></tscreen>

<!--<P> Commands shouldn't be that easy :-)-->
<p>コマンドを使うとすると、これほど簡単にはいかないでしょう。


<!--<bf/bc/ (a calculator programming language)-->
<sect2>bc (数値計算用のプログラミング言語)

<!--<P> Bc is accepting calculations from command line (input from file. not 
from redirector or pipe), but also from a user interface. The following 
demonstration shows some of the commands. Note that
<P> I start bc using the -q parameter to avoid a welcome message.-->
<p>
<tt>bc</tt> はコマンドラインで操作するよくできた計算プログラムです。リダイレク
トやパイプからは入力できませんが、ファイルからの入力が可能で、ユーザインター
フェイスもあります。以下では、そのコマンドの中からいくつかを実際にデモンスト
レーションしてみます。なお、<tt>bc</tt> 起動時に <tt>-q</tt> パラメータを使用
しているのは、起動時にウェルカムメッセージを出力させないためです。

<!--
   	<tscreen><verb>
   $bc -q
	</verb></tscreen>

   <P> <it/1 == 5/
   <P> <it/0/
   <P> <it/0.05 == 0.05/
   <P> <it/1/
   <P> <it/5 != 5/
   <P> <it/0/
   <P> <it/2 ^ 8/
   <P> <it/256/
   <P> <it/sqrt(9)/
   <P> <it/3/
   <P> <it/while (i != 9) {/
   <P> <it/i = i + 1;/
	<P> <it/print i/
   <P> <it/}/	
   <P> <it/123456789/
   <P> <it/quit/
-->
        <tscreen><verb>
        $ bc -q
        1 == 5
        0
        5 != 5
        0
        2 ^ 8
        256
        sqrt(9)
        3
        while (i != 9) { i = i + 1; print i }
        12345689
        quit
        </verb></tscreen>

<!--<bf/tput/ (initialize a terminal or query terminfo database)-->
<sect2>tput (端末の初期化や terminfo データベースの検索)

<!--   <P> A little demonstration of tput's capabilities:-->
<p><tt>tput</tt> でなにができるか、簡単に紹介します。

   	<tscreen><verb>
	$ tput cup 10 4
	</verb></tscreen>

<!--   <P> The prompt appears at (y10,x4).-->
<p>
上記の例では、プロンプトが <tt>(y10,x4)</tt> の位置にあらわれます。

   	<tscreen><verb>
	$ tput reset
	</verb></tscreen>

<!--   <P> Clears screen and prompt appears at (y1,x1). Note that (y0,x0) is 
the upper left corner.-->
<p>
上記では、画面をもとに戻して、<tt>(y1,x1)</tt> の位置にプロンプトを表示しま
す。<tt>(y0,x0)</tt> がちょうど左上の隅にあたる位置です。
<!--
   	<tscreen><verb>
	$tput cols
	</verb></tscreen>
   <it/80/
-->
        <tscreen><verb>
        $ tput cols
        80
        </verb></tscreen>

<!-- 　<P> Shows the number of characters possible in x direction.-->
<p>
上記では、<tt>X</tt> 軸方向に文字がいくつ並ぶのかを表示しています。

<!--<P> It it higly recommended to be familiarized with these programs (at 
least). There are tons of little programs that will let you do real magic on 
the command line.
<P>[some samples are taken from man pages or FAQs]</sect1></sect>-->
<p>
(少なくとも)上記のようなプログラムについては、使い慣れておくことを強くお薦め
します。コマンドライン上の操作で驚くほどの処理をしてくれる小さなプログラムは
無数にあります。
<p>
[例題のいくつかは、マニュアルページや FAQ から取りました。]


<!-- More Scripts -->
<!--<sect>More Scripts-->
<sect>いろいろなスクリプト

<!--<sect1>Applying a command to all files in a directory.-->
<sect1>ディレクトリ上の全ファイルにコマンドを適用する方法
<P>(作成中) 
</sect1>

<!--<sect1>Sample: A very simple backup script (little bit better)-->
<sect1>サンプル: 非常にシンプルなバックアップスクリプト(改良版)
        <P>
	<tscreen><verb>
	#!/bin/bash          
	SRCD="/home/"
	TGTD="/var/backups/"
	OF=home-$(date +%Y%m%d).tgz
	tar -cZf $TGTD$OF $SRCD
	</verb></tscreen> 
	   <P>
     </sect1>

<!--<sect1>File re-namer-->
<sect1>ファイル名変更スクリプト
          <P>
	<!--<tscreen><verb>-->
<!--    #!/bin/sh
        # renna: rename multiple files according to several rules
        # written by felix hudson Jan - 2000-->
        <tscreen><verb>
        #!/bin/sh
        # renna: いくつかのルールに従って、複数のファイルをリネームする
        # スクリプト: Felix Hudson Jan 2000 
        </verb></tscreen>
     
<!--    #first check for the various 'modes' that this program has
        #if the first ($1) condition matches then we execute that portion
        #of the program and then exit-->
        <tscreen><verb>
        # 最初にこのプログラムが取るいくつかの「モード」についてチェックする
        # 第一引数($1)の条件が一致するなら、プログラムの該当部分を実行して
        # 終了する
        </verb></tscreen> 

<!--    # check for the prefix condition
        if [ $1 = p ]; then-->
        <tscreen><verb>
        # prefix の条件をチェックする
        if [ $1 = p ] ; then
        </verb></tscreen>
             
<!--    #we now get rid of the mode ($1) variable and prefix ($2)
        prefix=$2 ; shift ; shift-->
        <tscreen><verb>
        # モード変数($1)と prefix ($2)を取り除く
        prefix=$2 ; shift ; shift
        </verb></tscreen>

<!--    # a quick check to see if any files were given
        # if none then its better not to do anything than rename some 
        # non-existent files!!-->
        <tscreen><verb>
        # ファイルが指定されているか簡単にチェックする
        # 指定されていないときは、存在しないファイルをリネームすることは
        # できないので、なにもしない             
        if [$1 = ]; then
             echo "no files given"
             exit 0
        fi
        </verb></tscreen>          

<!--    # this for loop iterates through all of the files that we gave 
        # the program
        # it does one rename per file given-->
        <tscreen><verb>
        # プログラムの引数となるファイルを順にループ処理する
        # ファイルをひとつずつリネームする
        for file in $*
            do
            mv ${file} $prefix$file
        done
        </verb></tscreen>   
<!--    #we now exit the program-->
        <tscreen><verb>
        # ここでプログラムを終了する
            exit 0
        fi
        </verb></tscreen>

<!--    # check for a suffix rename
        # the rest of this part is virtually identical to the previous 
        # section
        # please see those notes-->
        <tscreen><verb>
        # suffix をリネームするかチェックする
        # ここでの残りの部分は上記とおなじ処理なので、そちらを見てほしい
        if [ $1 = s ]; then
            suffix=$2 ; shift ; shift

        if [$1 = ]; then
            echo "no files given"
            exit 0
        fi

        for file in $*
            do
             mv ${file} $file$suffix
            done
            exit 0
        fi
        </verb></tscreen>
             
<!--    # check for the replacement rename-->
        <tscreen><verb>
        # 名前の置き換えかどうかをチェックする
        if [ $1 = r ]; then
            shift
        </verb></tscreen>   
<!--    # i included this bit as to not damage any files if the user does
        #  not specify anything to be done
        # just a safety measure-->
        <tscreen><verb>
        # ユーザが処理の内容を指定しなかったとしてもファイルがダメージを
        # 受けないように、次のコードを含めた
        if [ $# -lt 3 ] ; then
            echo "usage: renna r [expression] [replacement] files... "
            exit 0
        fi
        </verb></tscreen>

<!--    # remove other information-->
        <tscreen><verb>
        # 余計な情報を削除する
        OLD=$1 ; NEW=$2 ; shift ; shift
        </verb></tscreen>
     
<!--    # this for loop iterates through all of the files that we give 
        # the program
        # it does one rename per file given using the program 'sed'
        # this is a sinple command line program that parses standard 
        # input and replaces a set expression with a give string
        # here we pass it the file name ( as standard input) and replace 
        # the nessesary text-->
        <tscreen><verb>
        # この for ループはプログラムに与えられたファイルを順に処理する
        # sed というプログラムを使って、ファイルをひとつずつリネームする
        # sed は標準入力を読み込んで、特定の表現を与えられた文字列に置換する
        # ここでは、標準入力からファイル名を指定して、必要な文字を置換させる
        for file in $*
            do
             new=`echo ${file} | sed s/${OLD}/${NEW}/g`
             mv ${file} $new
            done
            exit 0
        fi
        </verb></tscreen>   

<!--    # if we have reached here then nothing proper was passed to the 
        # program
        # so we tell the user how to use it-->
        <tscreen><verb>
        # ここまで来たとすると、プログラムに引数がなかったということなので、
        # 使い方を表示する
        echo "usage;"
        echo " renna p [prefix] files.."
        echo " renna s [suffix] files.."
        echo " renna r [expression] [replacement] files.."
        exit 0
        </verb></tscreen>   

<!--    # done!-->
        <tscreen><verb>
        # 終了
        </verb></tscreen>
        </sect1>

<!--<sect1>File renamer (simple)-->
<sect1>ファイル名変更スクリプト(簡易版)

     <p>
        <tscreen><verb>
        #!/bin/bash
        # renames.sh
        # basic file renamer

        criteria=$1
        re_match=$2
        replace=$3
     
        for i in $( ls *$criteria* ); 
        do
            src=$i
	    tgt=$(echo $i | sed -e "s/$re_match/$replace/")
	    mv $src $tgt
        done
        </verb></tscreen>
     </sect1>
</sect>


<!--<sect>When something goes wrong (debugging)-->
<sect>うまくいかない時(デバッグ)

<!--<sect1>Ways Calling BASH--> 
<sect1>bash の呼び出し方
          
<!--<p> A nice thing to do is to add on the first line-->
<p>効果的なのは、第一行目にちょっと手を加えることです。
        <tscreen><verb>
	#!/bin/bash -x
	</verb></tscreen>
<!--<P> This will produce some intresting output information-->
<p>こうすると、出力に際に面白い情報が表示されます。
     </sect1>
</sect>

<!--<sect>About the document-->
<sect>この文書について

<!-- <P> Feel free to make suggestions/corrections, or whatever you think 
     it would be interesting to see in this document. I'll try to update
     it as soon as I can.-->
<p>提案や訂正、またこの文書に掲載すべき面白い事柄などなんでもお気軽に
連絡してください。できるだけ早く反映させます。

<!--<sect1>(no) warranty-->
<sect1>(無)保証について
          
<P>This documents comes with no warranty of any kind. and all 
that (ママ)
<p>この文書には、いかなる保証も伴いません。

<!--<sect1>Translations-->
<sect1>翻訳について

<!--<p> Italian: by Willy Ghelfi (wizzy at tiscalinet.it)
<htmlurl name="is here" url="http://web.tiscalinet.it/penguin_rules">-->
<p>イタリア語: by Willy Ghelfi (wizzy at tiscalinet.it)
<htmlurl url="http://web.tiscalinet.it/penguin_rules" 
name="こちら">

<!--<p> French: by Laurent Martelli <htmlurl name="is missed" url="http://">-->
<p>フランス語: by Laurent Martelli ( URL は不明)

<!--<p> Korean: Minseok Park 
<htmlurl url="http://kldp.org" name="http://kldp.org">-->
<p>韓国語: Minseok Park <htmlurl url="http://kldp.org" name="http://kldp.org">

<!--<p> Korean: Chun Hye Jin 
<htmlurl url="" name="unknown">-->
<p>韓国語: Chun Hye Jin ( URL は不明)

<!--<p> Spanish: unknow 
<htmlurl url="http://www.insflug.org" name="http://www.insflug.org">-->
<p>スペイン語: 訳者不明
<htmlurl url="http://www.insflug.org" name="http://www.insflug.org">

<!--<p> I guess there are more translations, but I don't have any info of 
them, if you havit, please, mail it to me so I update this section.</sect1>-->
<p>もっと多くの翻訳があると思うのですが、情報がありません。御存知のかたは、
わたしにメールをください。この章を更新します。

<!--<sect1>Thanks to-->
<sect1>謝辞
<P>
<itemize>
<!--<item> People who translated this document to other languages (previous 
section).-->
<item>この文書を翻訳してくれたひとたち(前章)

<!--<item> Nathan Hurst for sending a lot of corrections.-->
<item>Nathan Hurst は詳細に校正してくれました。

<!--<item> Jon Abbott for sending comments about evaluating arithmetic 
expressions.-->
<item>Jon Abbott は数式の評価についてコメントを送ってくれました。

<!--<item> Felix Hudson for writing the <it/renna/ script-->
<item>Felix Hudson は <it>renna</it> スクリプトを書いてくれました。

<!--<item> Kees van den Broek 
(for sending many corrections, re-writting usefull comands section)-->
<item>Kees van den Broek (校正に協力し、便利なコマンドの章をリライトしてくれ
ました)

<!--<item> Mike (pink) made some suggestions about locating bash and testing 
files-->
<item>Mike (pink) は bash の位置を特定したりファイルをテストしたりすることに
ついていくつか提案してくれました。

<!--<item> Fiesh make a nice suggestion for the loops section.-->
<item>Fiesh はループの章についていい提案をしてくれました。 

<!--<item> Lion suggested to mention a common error (./hello.sh: Command not 
found.)-->
<item>Lion はコマンドエラーについて述べるよう提案してくれました。(./hello.sh:
Command not found.)

<!--<item> Andreas Beck made several corrections and coments.-->
<item>Andrea Beck は校正とコメントについて協力してくれました。
</itemize>
</sect1>
	
<!--<sect1>History-->
<sect1>改訂履歴
<!--	<p> New translations included and minor correcitons.
        <p> Added the section usefull commands re-writen by Kess.
        <p> More corrections and suggestions incorporated.
	<p> Samples added on string comparison.
	<p> v0.8 droped the versioning, I guess the date is enought. 
	<p> v0.7 More corrections and some old TO-DO sections written.
	<p> v0.6 Minor corrections.
	<p> v0.5 Added the redirection section.
	<p> v0.4 disapperd from its location due to my ex-boss and thid doc 
            found it's new place at the proper url: www.linuxdoc.org.
	<p> prior:  I don't rememeber and I didn't use rcs nor cvs :(
	</sect1>-->
<p>
<itemize>
<item>翻訳が新規に追加され、いくつか訂正を加えた。
<item>Kees のリライトによる便利なコマンドの章を付け加えた。
<item>訂正を提案を大幅に取り入れた。
<item>文字列比較について実例を増やした。
<item>v0.8 バージョン情報を外した。日付で充分だと思う。
<item>v0.7 訂正箇所多数。TO-DO の章を書き直し。
<item>v0.6 すこし訂正
<item>v0.5 リダイレクトの章を追加
<item>v0.4 前任のボスの意向でそれまでのページで公開できなくなったが、この文書
にとっては最適の場所が見つかった。<url url="http://www.linuxdoc.org/">
<item>それ以前のことは覚えていない。rcs も cvs も使っていなかった。
</itemize>

<!--<sect1>More resources-->
<sect1>参考ホームページ
<!--          <P>
	  <P> Introduction to bash (under BE)
	      <htmlurl url="http://org.laol.net/lamug/beforever/bashtut.htm"
		name="http://org.laol.net/lamug/beforever/bashtut.htm">
          <P> Bourne Shell Programming 
	      http://207.213.123.70/book/ 
     </sect1>
</sect>-->
<p>
( BeOS での)<tt>bash</tt> に関する紹介
<url url="http://org.laol.net/lamug/beforever/bashtut.htm">
<p>
Bourne Shell のプログラミング
<url url="http://207.213.123.70/book/">



<sect1>日本語訳について
<p>
<verb>
翻訳 千旦 裕司  &lt;ysenda@pop01.odn.ne.jp&gt;
校正 井上 秀悟  &lt;shyugoro@pop12.odn.ne.jp&gt;
     武井 伸光  &lt;takei@webmasters.gr.jp&gt;
     伊藤 祐一  &lt;kade@kadesoft.com&gt;
</verb>

</article>
