<?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>Yukun&#039;s Blog &#187; Bash</title>
	<atom:link href="http://www.yukun.info/blog/tag/bash/feed" rel="self" type="application/rss+xml" />
	<link>http://www.yukun.info</link>
	<description>難しいことは分かりやすく、簡単なことは面白く紹介</description>
	<lastBuildDate>Thu, 26 Jan 2012 03:33:59 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Linux: シェル(Bash)の機能の概要 &#8211; 復習のための覚書(前編)</title>
		<link>http://www.yukun.info/blog/2011/08/linux-bash.html</link>
		<comments>http://www.yukun.info/blog/2011/08/linux-bash.html#comments</comments>
		<pubDate>Mon, 08 Aug 2011 13:10:19 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[Shell]]></category>

		<guid isPermaLink="false">http://www.yukun.info/?p=1949</guid>
		<description><![CDATA[シェルとは ユーザーとカーネルの間のインターフェイス。ユーザーが入力したコマンドや文字列のメタキャラクタを解釈するインタープリタ。 ログインシェル ユーザーがシステムへログインしたときに起動するシェル。ログインするシェル &#8230; <a href="http://www.yukun.info/blog/2011/08/linux-bash.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2011/08/linux-bash.html">Linux: シェル(Bash)の機能の概要 &#8211; 復習のための覚書(前編)</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<h4>シェルとは</h4>
<ul>
<li>ユーザーとカーネルの間のインターフェイス。ユーザーが入力したコマンドや文字列のメタキャラクタを解釈するインタープリタ。
</li>
</ul>
<p><span id="more-1949"></span></p>
<h4>ログインシェル</h4>
<ul>
<li>ユーザーがシステムへログインしたときに起動するシェル。ログインするシェルを変更する場合はchshコマンドを使用。
</li>
<li>ログインシェル上でさらに別のシェルを起動することができるが、ユーザーが使用することができるのは最後に起動したシェルのみ。
</li>
<li>ログインシェルと後に起動したシェルの見分け方はps -f。最初にハイフン「-」がついているシェルがログインシェルのプロセス。
</li>
</ul>
<pre>
$ ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
xxx      13450 13449  0 21:26 pts/1    00:00:00 -bash
xxx     13476 13450  0 21:26 pts/1    00:00:00 bash
xxx      13499 13476  0 21:27 pts/1    00:00:00 ksh
xxx      13501 13499  0 21:27 pts/1    00:00:00 ps –f
</pre>
<h4>シェルの種類</h4>
<ul>
<li>古いものから順にBourneシェル、Cシェル、Kornシェル、Bashとなる。
</li>
<li>BashはUNIX上のBourneシェル互換としてGUNより開発されたLinux標準シェル。
</li>
<li>LinuxでのBashの起動は$ shで(LinuxではBourneシェルはライセンス上使用できない為)。
</li>
<li>↓確かにリンクがbashへと張られている(CentOSで実行)
</li>
</ul>
<pre>
$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4  7月  9 15:06 /bin/sh -> bash
</pre>
<h4>シェルスクリプトの使いどころ</h4>
<ul>
<li>Linuxコマンドの組み合わせで実現できる処理(grep, sort, sed, awk, etc…)であればシェルスクリプトを使用。特にテキスト行の処理など。
</li>
<li>数値や文字列ごとの処理を使用する場合は型変換等の負荷のため実行速度が低下するため、C言語等の高級言語を使用するのが望ましい。
</li>
</ul>
<h4>シェルスクリプトの作成と実行方法</h4>
<ul>
<li>vi, vim等のテキストエディタで作成。対象のスクリプトファイルに実行権限を付与。
</li>
<li>chmod u+x xxxxxx.sh か、
</li>
<li>chmod 744 xxxxxx.sh でOK.
</li>
<li>実行は ./xxxxxx.sh　ただし、カレントディレクトリにpathが通っている場合は先頭の./は不要となる。
</li>
<li>スクリプトの1行目のシェルの指定(絶対パス)は、#!/bin/sh, #!/bin/bashのどちらを用いてもLinux上ではBashで実行される。
</li>
<li>1行目を省略した場合は、LinuxではBashで実行される。
</li>
<li>#のみを記述すると、現在起動中のシェルと同じシェルで実行される。
</li>
</ul>
<h4>コマンドの種類</h4>
<ul>
<li>組み込みコマンド：シェル内部のコマンド(cd, exitなど)
</li>
<li>一般コマンド：実行形式ファイルのコマンド(cp, lsなど)。/binや/usr/binディレクトリ以下に実行ファイルが存在する。
</li>
<li>組み込み・一般の判別にはtypeコマンドを使用する。
</li>
<li>以下にtypeコマンドの実行例。
</li>
</ul>
<pre>
$ type cp
cp is /bin/cp
$ type cd
cd is a shell builtin
</pre>
<ul>
<li>一般コマンドがシェルで実行されると、子プロセス上で実行される。
</li>
<li>実行順序としては、一般コマンド実行時→親プロセスのforkシステムコールによる子プロセスの生成→親プロセスのwaitシステムコールによる子プロセスの処理実行待ち→子プロセスのexecシステムコールによりコマンドのロード→コマンドプログラムのexitシステムコールにより子プロセスの終了→親プロセスの後続の処理を実行…。
</li>
<li>基本的にシェルスクリプトも実行時に特に指定がなければ子プロセスを生成し実行される。
</li>
<li>シェルの変数定義のスコープはプロセス毎。
</li>
</ul>
<h4>入出力リダイレクション</h4>
<ul>
<li>標準入力(0)、標準出力(1)、標準エラー出力(2)。リダイレクト記号はそれぞれ<, 1>, 2>であるが、標準出力の1は省略可能のため、>で可能。
</li>
<li> 2>&#038;1の場合は、コマンド実行時のエラーを標準出力(1)に割り当て。
</li>
<li> $ find / -name help > findlist 2>&#038;1
</li>
</ul>
<h4>パイプによるコマンドの連携</h4>
<ul>
<li>パイプ「|」は→とみた方が直感的にわかりやすい。|の左で実行されたコマンドの出力をパイプの後に指定したコマンドの入力として使用。
</li>
<li># grep Invalid /var/log/secure | wc –l
</li>
</ul>
<h4>その他のシェルのメタキャラクタ</h4>
<ul>
<li>連続実行：１行内でのコマンドの連続実行をする場合はコマンド間にセミコロンを挿入「;」。(C言語のステートメント終了みたいだぁ)
</li>
<li>グループ化：()のコマンドを子プロセスを生成して実行。cd /; pwd ; lsの三種コマンドに括弧をつけるとカレントディレクトリを変更せずに実行結果を取得できる。
</li>
<li>親プロセスの環境の変更したくない場合や複数コマンドの実行結果を出力したい場合に利用。
</li>
</ul>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2011/02/shell-for-wordlist-command.html" rel="bookmark" title="2011年2月6日">Shell Script: for文 &#8211; ワードリストに変数、コマンドを使用</a></li>
<li><a href="http://www.yukun.info/blog/2011/02/shell-script-test.html" rel="bookmark" title="2011年2月13日">Shell Script: testコマンドによる条件判定 (数値、文字列)</a></li>
<li><a href="http://www.yukun.info/blog/2008/01/shell-script-kill-process-2.html" rel="bookmark" title="2008年1月10日">プロセスの監視＆自動復旧(簡易版)</a></li>
<li><a href="http://www.yukun.info/blog/2008/01/shell-script-kill-process.html" rel="bookmark" title="2008年1月7日">シェルスクリプトでプロセスを監視し自動実行＆自動kill</a></li>
<li><a href="http://www.yukun.info/blog/2011/01/shell-for-wordlist.html" rel="bookmark" title="2011年1月30日">Shell Script: for文 &#8211; ワードリストを順に出力</a></li>
</ul>
<p><!-- Similar Posts took 7.651 ms --></p>
<p><a href="http://www.yukun.info/blog/2011/08/linux-bash.html">Linux: シェル(Bash)の機能の概要 &#8211; 復習のための覚書(前編)</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2011/08/linux-bash.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bash: 数値と文字列の判別</title>
		<link>http://www.yukun.info/blog/2011/08/bash-if-num-str.html</link>
		<comments>http://www.yukun.info/blog/2011/08/bash-if-num-str.html#comments</comments>
		<pubDate>Fri, 05 Aug 2011 12:40:06 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Bash]]></category>
		<category><![CDATA[RegExp]]></category>
		<category><![CDATA[Shell]]></category>

		<guid isPermaLink="false">http://www.yukun.info/?p=1944</guid>
		<description><![CDATA[手っ取り早く数字文字列とそれ以外の文字列を判別する条件式は以下のとおり。 コード #!/bin/sh if expr "$1" : '[0-9]*' > /dev/null ; then echo "数値です" else &#8230; <a href="http://www.yukun.info/blog/2011/08/bash-if-num-str.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2011/08/bash-if-num-str.html">Bash: 数値と文字列の判別</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>手っ取り早く数字文字列とそれ以外の文字列を判別する条件式は以下のとおり。</p>
<h3>コード</h3>
<pre>
#!/bin/sh

if expr "$1" : '[0-9]*' > /dev/null ; then
  echo "数値です"
else
  echo "数値以外です"
fi
</pre>
<h3>実行結果</h3>
<pre>
$ ./if_numstr.sh 123
数値です
$ ./if_numstr.sh abc
数値以外です
</pre>
<h3>解説</h3>
<p>exprで正規表現を用いて数値を判定。exprは内部コード以外にも標準出力にも結果を返すので、不要なそれは/dev/nullへリダイレクトする。<br />
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2011/08/linux-bash.html" rel="bookmark" title="2011年8月8日">Linux: シェル(Bash)の機能の概要 &#8211; 復習のための覚書(前編)</a></li>
<li><a href="http://www.yukun.info/blog/2011/02/shell-script-test.html" rel="bookmark" title="2011年2月13日">Shell Script: testコマンドによる条件判定 (数値、文字列)</a></li>
<li><a href="http://www.yukun.info/blog/2011/02/shell-for-wordlist-command.html" rel="bookmark" title="2011年2月6日">Shell Script: for文 &#8211; ワードリストに変数、コマンドを使用</a></li>
<li><a href="http://www.yukun.info/blog/2011/01/shell-for-wordlist.html" rel="bookmark" title="2011年1月30日">Shell Script: for文 &#8211; ワードリストを順に出力</a></li>
<li><a href="http://www.yukun.info/blog/2008/01/shell-script-kill-process-2.html" rel="bookmark" title="2008年1月10日">プロセスの監視＆自動復旧(簡易版)</a></li>
</ul>
<p><!-- Similar Posts took 6.646 ms --></p>
<p><a href="http://www.yukun.info/blog/2011/08/bash-if-num-str.html">Bash: 数値と文字列の判別</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2011/08/bash-if-num-str.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

