<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bash &#8211; REONTOSANTA</title>
	<atom:link href="https://knowledge.reontosanta.com/archives/category/programming/bash/feed" rel="self" type="application/rss+xml" />
	<link>https://knowledge.reontosanta.com</link>
	<description>Collection of personal knowledge</description>
	<lastBuildDate>Tue, 24 Jul 2018 05:43:55 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
	<atom:link rel='hub' href='https://knowledge.reontosanta.com/?pushpress=hub'/>
<site xmlns="com-wordpress:feed-additions:1">83369280</site>	<item>
		<title>Bashの特殊な変数</title>
		<link>https://knowledge.reontosanta.com/archives/282</link>
					<comments>https://knowledge.reontosanta.com/archives/282#respond</comments>
		
		<dc:creator><![CDATA[mogice]]></dc:creator>
		<pubDate>Thu, 15 Jan 2015 06:20:55 +0000</pubDate>
				<category><![CDATA[Bash]]></category>
		<guid isPermaLink="false">http://knowledge.reontosanta.com/?p=282</guid>

					<description><![CDATA[Bashの特殊な変数について。 &#160; $@ $@は、スクリプ&#46;&#46;&#46;]]></description>
										<content:encoded><![CDATA[<p>Bashの特殊な変数について。</p>

<table id="tablepress-15" class="tablepress tablepress-id-15">
<thead>
<tr class="row-1">
	<th class="column-1">変数</th><th class="column-2">説明</th>
</tr>
</thead>
<tbody class="row-striping row-hover">
<tr class="row-2">
	<td class="column-1">$@</td><td class="column-2">スクリプトの引数配列</td>
</tr>
<tr class="row-3">
	<td class="column-1">$数値</td><td class="column-2">スクリプトの引数</td>
</tr>
<tr class="row-4">
	<td class="column-1">$!</td><td class="column-2">直前に実行したプロセスのID</td>
</tr>
</tbody>
</table>
<!-- #tablepress-15 from cache -->
<p>&nbsp;</p>
<h2>$@</h2>
<p>$@は、スクリプトの引数の配列を示す。</p>
<p>各引数は空白で連結されている。</p>
<p>以下は、引数をそのまま出力するサンプル。</p><pre class="urvanov-syntax-highlighter-plain-tag">#!/bin/bash
echo "args = $@"</pre><p>実行結果。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ ./test_args.sh a b c
args = a b c</pre><p>&nbsp;</p>
<div class="knowl-content" style="margin-top: 40px;" id="knowl-2611514628"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="4196732648" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><h2>$数値</h2>
<p>$数値は、スクリプトの引数の1つを示す。</p>
<p>どの引数を指定するかは、数値で指定する。</p>
<p>$0は、スクリプトの実行パスを示す。</p>
<p>以下は、$0から$3までを出力するだけのサンプル。</p><pre class="urvanov-syntax-highlighter-plain-tag">#!/bin/bash
echo "arg[0] = $0"
echo "arg[1] = $1"
echo "arg[2] = $2"
echo "arg[3] = $3"</pre><p>実行結果。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ ./test_arg_split.sh a b
arg[0] = ./test_arg_split.sh
arg[1] = a
arg[2] = b
arg[3] =</pre><p>引数は2つなので、$3は空白になっている。</p>
<p>&nbsp;</p>
<h2>$!</h2>
<p>$!は、直前に実行したプロセスのプロセスIDが格納されている。</p>
<p>例えば、以下のようにすると、JavaプログラムのプロセスIDを出力できる。</p><pre class="urvanov-syntax-highlighter-plain-tag">#!/bin/bash
java test.sample.MyApp &amp;
pid=$!
echo $pid</pre><p>プロセスIDを取得できると後々、killすることも可能。</p>
<p>&nbsp;</p>
<div class="knowl-after-content-ad" style="margin-top: 40px;" id="knowl-1742617338"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="2719999442" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><div class="knowl-after-content-relation" id="knowl-482283755"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="7848218644" 
data-ad-format="autorelaxed"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div>]]></content:encoded>
					
					<wfw:commentRss>https://knowledge.reontosanta.com/archives/282/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">282</post-id>	</item>
		<item>
		<title>Bashで区切り文字を変更する</title>
		<link>https://knowledge.reontosanta.com/archives/280</link>
					<comments>https://knowledge.reontosanta.com/archives/280#respond</comments>
		
		<dc:creator><![CDATA[mogice]]></dc:creator>
		<pubDate>Tue, 13 Jan 2015 05:38:04 +0000</pubDate>
				<category><![CDATA[Bash]]></category>
		<guid isPermaLink="false">http://knowledge.reontosanta.com/?p=280</guid>

					<description><![CDATA[Bashで複数の文字列を配列として扱うとき、文字列の中に空白が含まれ&#46;&#46;&#46;]]></description>
										<content:encoded><![CDATA[<p>Bashで複数の文字列を配列として扱うとき、文字列の中に空白が含まれていると、そこで自動的に分割される。</p>
<p>これは、Bashのデフォルト区切り文字が「空白」となっている為。</p>
<p>Bashは区切り文字を&#8217;IFS&#8217;という特殊変数で管理している。</p>
<p>つまり、&#8217;IFS&#8217;の値を変更すれば、区切り文字を変更できる。</p>
<p>以下は、区切り文字を「改行コード」とするサンプル。</p><pre class="urvanov-syntax-highlighter-plain-tag"># 区切り文字のバックアップを取得
IFS_BACKUP=$IFS

# 区切り文字を変更
IFS=$'\n'

# 任意の処理を実行

# 区切り文字を元に戻す
IFS=$IFS_BACKUP</pre><p>スクリプトの最後に、&#8217;IFS&#8217;の値を元に戻しておく。</p>
<p>&nbsp;</p>
<div class="knowl-after-content-ad" style="margin-top: 40px;" id="knowl-3477393615"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="2719999442" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><div class="knowl-after-content-relation" id="knowl-2713105531"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="7848218644" 
data-ad-format="autorelaxed"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div>]]></content:encoded>
					
					<wfw:commentRss>https://knowledge.reontosanta.com/archives/280/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">280</post-id>	</item>
		<item>
		<title>Bashでワンライナー（OneLiner）を利用する</title>
		<link>https://knowledge.reontosanta.com/archives/272</link>
					<comments>https://knowledge.reontosanta.com/archives/272#respond</comments>
		
		<dc:creator><![CDATA[mogice]]></dc:creator>
		<pubDate>Mon, 12 Jan 2015 00:16:34 +0000</pubDate>
				<category><![CDATA[Bash]]></category>
		<guid isPermaLink="false">http://knowledge.reontosanta.com/?p=272</guid>

					<description><![CDATA[ワンライナー（OneLiner）、つまり一行でbashのスクリプトを&#46;&#46;&#46;]]></description>
										<content:encoded><![CDATA[<p>ワンライナー（OneLiner）、つまり一行でbashのスクリプトを記述する。</p>
<h2>基本的なこと</h2>
<p>基本的にBashのスクリプトと同様。</p>
<p>ポイントは、以下の２点。</p>
<ul>
<li>コマンドの出力結果は、パイプ(|)を使うと、次のコマンドに渡すことができる。</li>
<li>別のコマンドを実行する場合は、セミコロン(;)でコマンドを区切る。</li>
</ul>
<div class="knowl-content" style="margin-top: 40px;" id="knowl-2890682061"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="4196732648" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><h2>定期的に処理を実行</h2>
<p>以下の例は、1秒間隔で5回、dateコマンドを実行する。</p>
<p>最初に、ループ用の変数iを初期化。</p>
<p>&#8216;do&#8217;の後から、whileループ内で実行するコマンドを記述。</p>
<p>「dateコマンドの実行」「sleepコマンドの実行」「変数iのインクリメント」の3つを実行。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ i=1; while [ $i -le "5" ]; do date; sleep 1; i=$(($i+1)); done
Wed Jan 7 17:26:44 JST 2015
Wed Jan 7 17:26:45 JST 2015
Wed Jan 7 17:26:46 JST 2015
Wed Jan 7 17:26:47 JST 2015
Wed Jan 7 17:26:49 JST 2015</pre><p></p>
<h2>ファイルを検索して、各ファイルに処理を実行</h2>
<p>以下の例は、検索したファイルにsedで置換処理をかける。</p>
<p>最初に&#8217;find&#8217;コマンドでファイルを絞り込み、その後に&#8217;for-in&#8217;でループして処理。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ files=$(find . -name '*.java'); for file in $files; do sed -i '.bak' -e 's/foo/bar/g' $file; done</pre><p>&#8216;grep&#8217;を使い、さらに絞り込むことも可能。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ files=$(find . -name '*.java' | xargs grep 'foo'); for file in $files; do sed -i '.bak' -e 's/foo/bar/g' $file; done</pre><p>特定のファイルを全て削除する場合は、以下の通り。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ files=$(find . -name '*.java' | xargs grep 'foo'); for file in $files; do rm $file; done</pre><p>&nbsp;</p>
<div class="knowl-after-content-ad" style="margin-top: 40px;" id="knowl-2277250969"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="2719999442" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><div class="knowl-after-content-relation" id="knowl-3218863140"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="7848218644" 
data-ad-format="autorelaxed"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div>]]></content:encoded>
					
					<wfw:commentRss>https://knowledge.reontosanta.com/archives/272/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">272</post-id>	</item>
		<item>
		<title>Bashでリダイレクトを利用する</title>
		<link>https://knowledge.reontosanta.com/archives/255</link>
					<comments>https://knowledge.reontosanta.com/archives/255#respond</comments>
		
		<dc:creator><![CDATA[mogice]]></dc:creator>
		<pubDate>Sat, 10 Jan 2015 01:51:09 +0000</pubDate>
				<category><![CDATA[Bash]]></category>
		<guid isPermaLink="false">http://knowledge.reontosanta.com/?p=255</guid>

					<description><![CDATA[リダイレクトを使うと、コマンドの処理結果を、ファイルに書き込むことが&#46;&#46;&#46;]]></description>
										<content:encoded><![CDATA[<p>リダイレクトを使うと、コマンドの処理結果を、ファイルに書き込むことができる。</p>
<p>&#8216;&gt;&#8217;の使用方法。</p>
<p>&#8216;&gt;&#8217;は、ファイルの内容を上書きする。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ echo "test" &gt; ./data.txt</pre><p>ファイルの中身を確認。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ cat ./data.txt
test</pre><p>尚、&#8217;&gt;&#8217;は標準出力のみをリダイレクトする。</p>
<p>エラー出力も一緒にリダイレクトしたい場合は、以下の通り。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ command &amp;&gt; file.txt</pre><p>エラー出力のみをリダイレクトしたい場合は、以下の通り。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ command 2&gt; file.txt</pre><p>&#8216;&gt;&gt;&#8217;の使用方法。</p>
<p>&#8216;&gt;&gt;&#8217;は、&#8217;&gt;&#8217;と違い、ファイルに「追記」する。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ echo "test" &gt;&gt; ./data2.txt
-bash-3.2$ echo "test" &gt;&gt; ./data2.txt</pre><p>ファイルの中身を確認。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ cat ./data2.txt
test
test</pre><p>&nbsp;</p>
<div class="knowl-after-content-ad" style="margin-top: 40px;" id="knowl-2444407392"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="2719999442" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><div class="knowl-after-content-relation" id="knowl-1233602050"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="7848218644" 
data-ad-format="autorelaxed"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div>]]></content:encoded>
					
					<wfw:commentRss>https://knowledge.reontosanta.com/archives/255/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">255</post-id>	</item>
		<item>
		<title>Bashでコマンドの実行結果を変数に取得</title>
		<link>https://knowledge.reontosanta.com/archives/253</link>
					<comments>https://knowledge.reontosanta.com/archives/253#respond</comments>
		
		<dc:creator><![CDATA[mogice]]></dc:creator>
		<pubDate>Fri, 09 Jan 2015 07:05:09 +0000</pubDate>
				<category><![CDATA[Bash]]></category>
		<guid isPermaLink="false">http://knowledge.reontosanta.com/?p=253</guid>

					<description><![CDATA[構文 コマンドの実行結果を変数に代入する方法。 コマンド(ls、gr&#46;&#46;&#46;]]></description>
										<content:encoded><![CDATA[<h2>構文</h2>
<p>コマンドの実行結果を変数に代入する方法。</p>
<p>コマンド(ls、grep、find等)の実行結果を変数に代入する。</p>
<p>構文は、以下の通り。</p><pre class="urvanov-syntax-highlighter-plain-tag">変数=$(コマンド)</pre><p></p>
<div class="knowl-content" style="margin-top: 40px;" id="knowl-3526113856"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="4196732648" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><h2>サンプル</h2>
<p>以下、サンプル。</p>
<p>lsコマンドの実行</p>
<p>結果を変数filesに代入し、echoで出力する。</p><pre class="urvanov-syntax-highlighter-plain-tag">#!/bin/bash
files=$(ls)
echo ${files}</pre><p>実行結果。</p>
<p>lsコマンドに続けて、test_store.shを実行。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ ls
test_case.sh         test_for1.sh  test_search_string.sh  test_while2.sh
test_check           test_for2.sh  test_select.sh
test_compare_num.sh  test_for3.sh  test_store.sh
test_compare_str.sh  test_if.sh    test_while1.sh
-bash-3.2$ ./test_store.sh
test_case.sh test_check test_compare_num.sh test_compare_str.sh test_for1.sh test_for2.sh test_for3.sh test_if.sh test_search_string.sh test_select.sh test_store.sh test_while1.sh test_while2.sh</pre><p>lsコマンドの結果を変数に取得した場合、結果が半角スペース区切りで取得される為、表示上は異なるが結果は同等となる。</p>
<p>&nbsp;</p>
<div class="knowl-after-content-ad" style="margin-top: 40px;" id="knowl-2094125793"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="2719999442" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><div class="knowl-after-content-relation" id="knowl-2535036683"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="7848218644" 
data-ad-format="autorelaxed"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div>]]></content:encoded>
					
					<wfw:commentRss>https://knowledge.reontosanta.com/archives/253/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">253</post-id>	</item>
		<item>
		<title>Bashで文字列を比較する</title>
		<link>https://knowledge.reontosanta.com/archives/251</link>
					<comments>https://knowledge.reontosanta.com/archives/251#respond</comments>
		
		<dc:creator><![CDATA[mogice]]></dc:creator>
		<pubDate>Thu, 08 Jan 2015 05:08:21 +0000</pubDate>
				<category><![CDATA[Bash]]></category>
		<guid isPermaLink="false">http://knowledge.reontosanta.com/?p=251</guid>

					<description><![CDATA[if文で文字列比較 文字列は、if文を使って比較。 以下は、if文で&#46;&#46;&#46;]]></description>
										<content:encoded><![CDATA[<h2>if文で文字列比較</h2>
<p>文字列は、if文を使って比較。</p>
<p>以下は、if文で利用するオプション。</p>

<table id="tablepress-12" class="tablepress tablepress-id-12">
<thead>
<tr class="row-1">
	<th class="column-1">オプション</th><th class="column-2">比較の内容</th>
</tr>
</thead>
<tbody class="row-striping row-hover">
<tr class="row-2">
	<td class="column-1">=</td><td class="column-2">文字列同士が等しいか</td>
</tr>
<tr class="row-3">
	<td class="column-1">!=</td><td class="column-2">文字列同士が異なるか</td>
</tr>
<tr class="row-4">
	<td class="column-1">-n</td><td class="column-2">文字列の長さが1以上か</td>
</tr>
<tr class="row-5">
	<td class="column-1">-z</td><td class="column-2">文字列の長さが0か</td>
</tr>
</tbody>
</table>

<div class="knowl-content" style="margin-top: 40px;" id="knowl-576505270"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="4196732648" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><h2>サンプル</h2>
<p>以下、サンプル。</p><pre class="urvanov-syntax-highlighter-plain-tag">#!/bin/bash
foo=aaa
var=aaa

if [ ${foo} = ${var} ]; then
    echo "equal"
fi

if [ ${foo} != "bbb" ]; then
    echo "not equal"
fi

# 変数fooのサイズが1以上であれば真
if [ -n ${foo} ]; then
    echo "not null"
fi

# 変数fooのサイズが0であれば真
foo=
if [ -z ${foo} ]; then
    echo "null"
fi</pre><p>実行結果。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ ./test_compare_str.sh
equal
not equal
not null
null</pre><p>文字列の中に、特定の文字が含まれていることを判定する方法。</p>
<p>以下、サンプル。</p><pre class="urvanov-syntax-highlighter-plain-tag">#!/bin/bash
text="hoge"

if [ $(echo $text | grep -e 'h') ]; then
    echo "hoge include h"
else
    echo "hoge doesn't include h"
fi

if [ $(echo $text | grep -e 'a') ]; then
    echo "hoge include a"
else
    echo "hoge doesn't include a"
fi</pre><p>実行結果。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ ./test_search_string.sh
hoge include h
hoge doesn't include a</pre><p>&nbsp;</p>
<div class="knowl-after-content-ad" style="margin-top: 40px;" id="knowl-2720863672"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="2719999442" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><div class="knowl-after-content-relation" id="knowl-2096838815"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="7848218644" 
data-ad-format="autorelaxed"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div>]]></content:encoded>
					
					<wfw:commentRss>https://knowledge.reontosanta.com/archives/251/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">251</post-id>	</item>
		<item>
		<title>Bashで数値を比較する</title>
		<link>https://knowledge.reontosanta.com/archives/244</link>
					<comments>https://knowledge.reontosanta.com/archives/244#respond</comments>
		
		<dc:creator><![CDATA[mogice]]></dc:creator>
		<pubDate>Thu, 08 Jan 2015 05:07:43 +0000</pubDate>
				<category><![CDATA[Bash]]></category>
		<guid isPermaLink="false">http://knowledge.reontosanta.com/?p=244</guid>

					<description><![CDATA[構文 Bashで数値を比較する方法。 数値の比較は、以下の構文。 [&#46;&#46;&#46;]]></description>
										<content:encoded><![CDATA[<h2>構文</h2>
<p>Bashで数値を比較する方法。</p>
<p>数値の比較は、以下の構文。</p><pre class="urvanov-syntax-highlighter-plain-tag">数値A 演算子 数値B</pre><p>演算子の種類は以下の通り。</p>

<table id="tablepress-11" class="tablepress tablepress-id-11">
<thead>
<tr class="row-1">
	<th class="column-1">演算子</th><th class="column-2">意味</th>
</tr>
</thead>
<tbody class="row-striping row-hover">
<tr class="row-2">
	<td class="column-1">-lt</td><td class="column-2">AはBより小さい(less than)</td>
</tr>
<tr class="row-3">
	<td class="column-1">-le</td><td class="column-2">AはB以下(less or equal)</td>
</tr>
<tr class="row-4">
	<td class="column-1">-gt</td><td class="column-2">AはBより大きい(greater than)</td>
</tr>
<tr class="row-5">
	<td class="column-1">-ge</td><td class="column-2">AはB以上(greater or equal)</td>
</tr>
<tr class="row-6">
	<td class="column-1">-eq</td><td class="column-2">AとBは等しい(equal)</td>
</tr>
<tr class="row-7">
	<td class="column-1">-ne</td><td class="column-2">AとBは等しくない(not equal)</td>
</tr>
</tbody>
</table>
<!-- #tablepress-11 from cache -->
<p>数値の比較は、if文やwhile文の条件式として利用可能。</p>
<div class="knowl-content" style="margin-top: 40px;" id="knowl-3001932923"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="4196732648" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><h2>サンプル</h2>
<p>以下、サンプル。</p><pre class="urvanov-syntax-highlighter-plain-tag">#!/bin/bash
A=1
B=2
C=1

# true
if [ $A -lt $B ]; then
    echo "$A &lt; $B"
fi

# false
if [ $B -le $A ]; then
    echo "$B &lt;= $A"
else
    echo "$B &gt; $A"
fi

# true
if [ $B -gt $A ]; then
    echo "$B &gt; $A"
else
    echo "$B &lt;= $A"
fi

# true
if [ $A -eq $C ]; then
    echo "$A == $C"
else
    echo "$A != $C"
fi</pre><p>実行結果。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ ./test_compare_num.sh
1 &lt; 2
2 &gt; 1
2 &gt; 1
1 == 1</pre><p>&nbsp;</p>
<div class="knowl-after-content-ad" style="margin-top: 40px;" id="knowl-3146695581"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="2719999442" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><div class="knowl-after-content-relation" id="knowl-3784842707"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="7848218644" 
data-ad-format="autorelaxed"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div>]]></content:encoded>
					
					<wfw:commentRss>https://knowledge.reontosanta.com/archives/244/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">244</post-id>	</item>
		<item>
		<title>Bashで小数の比較を行う</title>
		<link>https://knowledge.reontosanta.com/archives/238</link>
					<comments>https://knowledge.reontosanta.com/archives/238#respond</comments>
		
		<dc:creator><![CDATA[mogice]]></dc:creator>
		<pubDate>Mon, 05 Jan 2015 05:20:17 +0000</pubDate>
				<category><![CDATA[Bash]]></category>
		<guid isPermaLink="false">http://knowledge.reontosanta.com/?p=238</guid>

					<description><![CDATA[大小比較のコマンド Bashで小数の大小比較を行うにはbcを使う。 &#46;&#46;&#46;]]></description>
										<content:encoded><![CDATA[<h2>大小比較のコマンド</h2>
<p>Bashで小数の大小比較を行うにはbcを使う。</p>
<p>&#8220;test&#8221;コマンドでは小数の大小比較はできない。</p>
<p>数値計算コマンドの&#8221;bc&#8221;に不等号での大小比較表現を与えると、その結果が1または0で標準入力に返ってくる。</p>
<p>trueのときが1で、falseのときが0。</p><pre class="urvanov-syntax-highlighter-plain-tag">$ bc
3.14 &gt; 1
1</pre><p>イコール付きの表現でもOK。</p><pre class="urvanov-syntax-highlighter-plain-tag">$ bc
3.14 &gt;= 3.14
1</pre><p>&#8220;bc&#8221;は対話的に入力を処理する為、シェルスクリプトから使用する場合には表現を標準入力から与える。</p><pre class="urvanov-syntax-highlighter-plain-tag">$ echo "10 &gt; 3.14" | bc
1</pre><p>標準入力から表現を与える代わりに&#8221;-e&#8221;オプションで与えることも可能。</p><pre class="urvanov-syntax-highlighter-plain-tag">$ bc -e "10 &lt; 3.14"
0</pre><p>上記の場合、引数で与えた表現を処理したあとに標準入力からの入力を待つ為、&#8221;-e quit&#8221;を指定することで1回処理を実行したあとにプロセスが終了させることができる。</p><pre class="urvanov-syntax-highlighter-plain-tag">$ bc -e "10 &lt; 3.14" -e quit
0</pre><p>&#8220;if&#8221;文と&#8221;test&#8221;コマンドを組み合わせて使用する場合は、以下の通り。</p><pre class="urvanov-syntax-highlighter-plain-tag">if [ "$(echo "10 &gt; 3.14" | bc)" -eq 1 ]; then
    echo "9800 is greater than 33.4"
fi</pre><p>&nbsp;</p><pre class="urvanov-syntax-highlighter-plain-tag">if [ "$(bc -e "10 &gt; 3.14" -e quit)" -eq 1 ]; then
    echo "9800 is greater than 33.4"
fi</pre><p>&nbsp;</p>
<div class="knowl-content" style="margin-top: 40px;" id="knowl-2822608392"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="4196732648" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><h2>サンプル</h2>
<p>以下、サンプル。</p><pre class="urvanov-syntax-highlighter-plain-tag">#!/bin/sh
# $1 &gt; $2 が真なら1、偽なら0
X=`echo "$1 &gt; $2" | bc`
if [ $X -eq 1 ]; then
    echo "$1 is large"
else
    echo "$2 is large"
fi</pre><p>&nbsp;</p>
<p>&nbsp;</p>
<div class="knowl-after-content-ad" style="margin-top: 40px;" id="knowl-1735825257"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="2719999442" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><div class="knowl-after-content-relation" id="knowl-1473327624"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="7848218644" 
data-ad-format="autorelaxed"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div>]]></content:encoded>
					
					<wfw:commentRss>https://knowledge.reontosanta.com/archives/238/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">238</post-id>	</item>
		<item>
		<title>Bashでselect文を利用する</title>
		<link>https://knowledge.reontosanta.com/archives/226</link>
					<comments>https://knowledge.reontosanta.com/archives/226#respond</comments>
		
		<dc:creator><![CDATA[mogice]]></dc:creator>
		<pubDate>Sat, 27 Dec 2014 07:37:13 +0000</pubDate>
				<category><![CDATA[Bash]]></category>
		<guid isPermaLink="false">http://knowledge.reontosanta.com/?p=226</guid>

					<description><![CDATA[構文 Bashにおけるselect文の使い方。 ※select文は、&#46;&#46;&#46;]]></description>
										<content:encoded><![CDATA[<h2>構文</h2>
<p>Bashにおけるselect文の使い方。</p>
<p>※select文は、選択肢を表示してユーザに選択させる処理を実現する。</p><pre class="urvanov-syntax-highlighter-plain-tag">PS3=選択をうながす文字列
select 変数 in 選択肢の配列
do
    選択された変数に応じた処理
done</pre><p>変数PS3に代入した内容が、select文のところで標準出力される。</p>
<div class="knowl-content" style="margin-top: 40px;" id="knowl-455685360"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="4196732648" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><h2>サンプル</h2>
<p>以下、サンプル。</p>
<p>finish選択で処理を終了させる。</p><pre class="urvanov-syntax-highlighter-plain-tag">#!/bin/bash
PS3="Input number: "
menu="aaa bbb ccc finish"
select var in ${menu}
do
    echo "You selected ${var}"
    if [ "${var}" = "finish" ]; then
        exit 1
    fi
done
echo "Finish script."</pre><p>実行結果。</p>
<p>選択肢への回答は、数値で行う。</p>
<p>今回の場合は、4(finish)を選択するまで、処理がループする。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ ./test_select.sh
1) aaa
2) bbb
3) ccc
4) finish
Input number: 1
You selected aaa
Input number: 2
You selected bbb
Input number: 3
You selected ccc
Input number: 4
You selected finish</pre><p>&nbsp;</p>
<div class="knowl-after-content-ad" style="margin-top: 40px;" id="knowl-1526581964"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="2719999442" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><div class="knowl-after-content-relation" id="knowl-3895142828"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="7848218644" 
data-ad-format="autorelaxed"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div>]]></content:encoded>
					
					<wfw:commentRss>https://knowledge.reontosanta.com/archives/226/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">226</post-id>	</item>
		<item>
		<title>Bashでwhile文を利用する</title>
		<link>https://knowledge.reontosanta.com/archives/228</link>
					<comments>https://knowledge.reontosanta.com/archives/228#respond</comments>
		
		<dc:creator><![CDATA[mogice]]></dc:creator>
		<pubDate>Sat, 27 Dec 2014 07:37:00 +0000</pubDate>
				<category><![CDATA[Bash]]></category>
		<guid isPermaLink="false">http://knowledge.reontosanta.com/?p=228</guid>

					<description><![CDATA[構文 Bashにおけるwhile文の使い方。 [crayon-684&#46;&#46;&#46;]]></description>
										<content:encoded><![CDATA[<h2>構文</h2>
<p>Bashにおけるwhile文の使い方。</p><pre class="urvanov-syntax-highlighter-plain-tag">while [ 条件式 ]
do
    処理
done</pre><p>条件式が真であれば、処理を繰り返す。</p>
<div class="knowl-content" style="margin-top: 40px;" id="knowl-154242207"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="4196732648" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><h2>サンプル</h2>
<p>以下のサンプル。</p>
<p>変数iの値が5になるまでwhileでループ。</p><pre class="urvanov-syntax-highlighter-plain-tag">#!/bin/bash
i=1
while [ $i -lt 5 ]
do
    echo $i
    #adds 1 to the i.
    i=$(($i+1))
done</pre><p>実行結果。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ ./test_while1.sh
1
2
3
4</pre><p>条件式に、文字列を指定することも可能。</p>
<p>スクリプトの引数をwhile文でループ。</p>
<p>文字列が空になったらループ終了。</p>
<p>以下のサンプル。</p><pre class="urvanov-syntax-highlighter-plain-tag">#!/bin/bash
while [ $1 ]
do
    echo $1
    #Shift the argument ($1 to $2).
    shift
done</pre><p>実行結果。</p><pre class="urvanov-syntax-highlighter-plain-tag">-bash-3.2$ ./test_while2.sh aaa bbb ccc
aaa
bbb
ccc</pre><p>引数が順番に出力される。</p>
<p>&nbsp;</p>
<div class="knowl-after-content-ad" style="margin-top: 40px;" id="knowl-3393432796"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="2719999442" 
data-ad-format="auto"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div><div class="knowl-after-content-relation" id="knowl-3742376586"><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-8913642688016174" crossorigin="anonymous"></script><ins class="adsbygoogle" style="display:block;" data-ad-client="ca-pub-8913642688016174" 
data-ad-slot="7848218644" 
data-ad-format="autorelaxed"></ins>
<script> 
(adsbygoogle = window.adsbygoogle || []).push({}); 
</script>
</div>]]></content:encoded>
					
					<wfw:commentRss>https://knowledge.reontosanta.com/archives/228/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">228</post-id>	</item>
	</channel>
</rss>
