<?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; Command</title>
	<atom:link href="http://www.yukun.info/blog/tag/command/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>Apacheでよく使うコマンドと設定項目</title>
		<link>http://www.yukun.info/blog/2008/12/how-to-write-apache-conf.html</link>
		<comments>http://www.yukun.info/blog/2008/12/how-to-write-apache-conf.html#comments</comments>
		<pubDate>Thu, 25 Dec 2008 13:50:51 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Setting]]></category>

		<guid isPermaLink="false">http://www.yukun.info/?p=1275</guid>
		<description><![CDATA[設置環境はFedoraを想定しています。 注：ソースからインストールした場合や他の環境だと一部ファイルのパスが違うところがあります。 今後も少しずつ書き足し・修正していきます。 コマンド 起動 # /etc/rc.d/i &#8230; <a href="http://www.yukun.info/blog/2008/12/how-to-write-apache-conf.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/12/how-to-write-apache-conf.html">Apacheでよく使うコマンドと設定項目</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>設置環境はFedoraを想定しています。<br />
注：ソースからインストールした場合や他の環境だと一部ファイルのパスが違うところがあります。<br />
今後も少しずつ書き足し・修正していきます。</p>
<h2>コマンド</h2>
<dl>
<dt>起動</dt>
<dd># /etc/rc.d/init.d/httpd start<br />
または、<br />
# service httpd start</dd>
<dt>終了</dt>
<dd># /etc/rc.d/init.d/httpd stop<br />
または、<br />
# service httpd stop</dd>
<dt>再起動</dt>
<dd># /etc/rc.d/init.d/httpd restart<br />
または、<br />
# service httpd restart</dd>
<dt>自動起動に設定</dt>
<dd># chkconfig httpd on</dd>
<dt>自動起動の確認(Run level 3:on)</dt>
<dd># chkconfig &#8211;list httpd</dd>
<dt>設定の反映</dt>
<dd># /etc/rc.d/init.d/httpd reload</dd>
<dt>ディレクトリの所有者の変更</dt>
<dd># chown ＜ユーザ名＞. /var/www/html/</dd>
<dt>ディレクトリをApache実行ユーザに変更</dt>
<dd># chown -R apache:apache /var/www/html/cgi-bin/</dd>
</dl>
<h2>httpd.confの設定</h2>
<dl>
<dt>設定ファイルhttpd.confのパス</dt>
<dd>/etc/httpd/conf/httpd.conf</dd>
<dt>外部設定ファイル*.confを置くパス</dt>
<dd>/etc/httpd/conf.d/*.conf<br />
・起動時に読み込まれる<br />
・AliasとDirectoryを合わせて用いる場合が多い
</dd>
<dt>DocumentRoot</dt>
<dd>ルートディレクトリの設定<br />
 e.g. DocumentRoot &#8220;/var/www/html&#8221; で<br />
www.example.com/へのアクセスは/var/www/htmlのインデックスページとなる。</dd>
<dt>Alias</dt>
<dd>Alias ＜ドメイン以下のURLパス＞ ＜サーバ内のディレクトリパス＞<br />
<em>e.g.</em> Alias /blog /var/www/blog<br />
の場合は、http://www.example.com/blog/にアクセスした際、サーバの/var/www/blog/内の既定のインデックスファイルが読み込まれる。</dd>
<dt>Directoryタグ</dt>
<p>e.g.</p>
<pre>&lt;directory &quot;/var/www/html&quot;&gt;〜&lt;directory&gt;</pre>
<p>属性にディレクトリパスを指定している。</p>
<dl>
<dt>.htaccessを許可する場合</dt>
<dd>AllowOverride All</dd>
<dt>IPアドレスによるアクセス制限</dt>
<dd>
<pre>
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
Allow from 192.168.1.0/24
</pre>
<p>↑はlocalhost、イントラネット以外からのアクセスを拒否
</dd>
</dl>
</dl>
<h2>.htaccessによるパスワード認証</h2>
<h3>.htaccessファイル内の設定例</h3>
<pre>
SSLRequireSSL # SSL経由のアクセス
AuthUserFile ＜認証するユーザリストのパス＞
AuthGroupFile ＜パス＞
AuthName "＜ページ名＞"
AuthType Basic # 認証タイプ
require valid-user
</pre>
<h3>認証するユーザの登録</h3>
<p>初回は<br />
# htpasswd -b -c ＜保存先＞ ＜ユーザ名＞ ＜パスワード＞<br />
二件目以降は既にファイルが作成されているので-cを抜く<br />
# htpasswd -b ＜保存先＞ ＜ユーザ名＞ ＜パスワード＞<br />
ここで作成したファイルのパスを上のAuthUserFile項目に書く。<br />
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/06/linux-users.html" rel="bookmark" title="2008年6月26日">ユーザー管理に関するLinuxコマンド</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/sshd-log-file.html" rel="bookmark" title="2008年6月24日">sshdのログファイルの確認方法</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/r-set-work-directory.html" rel="bookmark" title="2008年9月3日">Rで統計: 作業ディレクトリの設定と確認 &#8211; setwd()、getwd()関数</a></li>
<li><a href="http://www.yukun.info/blog/2011/08/wordpress-file-access.html" rel="bookmark" title="2011年8月9日">WordPress: 解決策→「要求されたアクションを実行するには、WordPress が Web サーバーにアクセスする必要があります。」</a></li>
<li><a href="http://www.yukun.info/blog/2011/12/linux-redmine-subversion-install.html" rel="bookmark" title="2011年12月6日">Linux: RedmineとSubversionのインストール・設定例</a></li>
</ul>
<p><!-- Similar Posts took 8.719 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/12/how-to-write-apache-conf.html">Apacheでよく使うコマンドと設定項目</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/2008/12/how-to-write-apache-conf.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python: ファイル読み込み時の例外の扱い例 &#8211; try、except、else、finallyブロック</title>
		<link>http://www.yukun.info/blog/2008/09/python-read-file-try-except-else-finally.html</link>
		<comments>http://www.yukun.info/blog/2008/09/python-read-file-try-except-else-finally.html#comments</comments>
		<pubDate>Thu, 18 Sep 2008 11:40:50 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[Exception]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[read]]></category>

		<guid isPermaLink="false">http://www.yukun.info/?p=1191</guid>
		<description><![CDATA[ファイルのパスや名前のミス、パーミッションの権限が無い等が原因でファイルを読み込めない場合があります。そのような場合、すなわち例外が発生した際にそこで処理を中断して、発生した例外に合わせた処理ブロックにジャンプする構文が &#8230; <a href="http://www.yukun.info/blog/2008/09/python-read-file-try-except-else-finally.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/09/python-read-file-try-except-else-finally.html">Python: ファイル読み込み時の例外の扱い例 &#8211; try、except、else、finallyブロック</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>ファイルのパスや名前のミス、パーミッションの権限が無い等が原因でファイルを読み込めない場合があります。そのような場合、すなわち例外が発生した際にそこで処理を中断して、発生した例外に合わせた処理ブロックにジャンプする構文が、try:～except Error:～構文です。</p>
<p>今回は、コマンドライン引数で英文テキストファイル名を指定し、スクリプト内でファイル内容を読み込み、単語数をカウントし出力するスクリプトを以って、オプションのelse、finallyブロックを含めた例外ブロックの扱いを確認します。<br />
ただし、ここでの「単語」とは、１つの空白文字で区切られた文字列とします（簡単に）。</p>
<h2>ソースコード</h2>
<pre>
# -*- coding: UTF-8 -*-

import sys

script_name = sys.argv[0]
try:
    arg = sys.argv[1]
    f = open(arg, 'r')
except IndexError:
    print 'Usage: %s TEXTFILE' % script_name
except IOError:
    print '"%s" cannot be opened.' % arg
else:
    print arg, 'contains', len(f.read().split(' ')), 'words.'
    f.close()
finally:
    print 'n"%s" process end.' % script_name
    quit()
print 'Not reach this line.'
</pre>
<h2>コードの説明</h2>
<h3>tryブロック</h3>
<p>まず、例外を発生させる恐れのある行は、tryブロック内に書きます。</p>
<p>ここで発生しそうなのは、コマンドライン引数が格納されているリストへのアクセス部分であるsys.argv[1]です。[]演算子で存在しない要素を参照するエラー、もとい例外が発生する恐れがあります。</p>
<p>また、ファイルを開くopen(arg, &#8216;r&#8217;)も冒頭の理由で例外を発生させるかもしれません。</p>
<h3>exceptブロック</h3>
<p>exceptブロックは、tryブロックで例外が発生した場合にのみ実行されるブロックです。その為、例外が発生しない場合は実行されません。</p>
<p>発生する可能性のある例外のタイプ毎にexceptブロックを書けば、そのタイプ毎の例外への対処処理を書くことが出来ます。</p>
<pre>
except <em>TYPE_A</em>Error:
    <em>TYPE_A</em>Errorが発生した際のとある処理...
except <em>TYPE_B</em>Error:
    <em>TYPE_B</em>Errorが発生した際のとある処理...
</pre>
<h3>elseブロック　（オプション）</h3>
<p>elseブロックは全てのexceptブロックの後に書きます（任意なので書かなくても構わない）。</p>
<p>elseブロックはtryブロックで例外が発生<strong>しなかった場合にのみ</strong>実行されるブロックです。今回は、ファイルの読み込みとクローズに使用しています。</p>
<h3>finallyブロック　（オプション）</h3>
<p>finallyブロックはtryブロックで例外が発生<strong>するしないに関わらず</strong>実行されるブロックです（任意なので書かなくても構わない）。</p>
<h2>実行結果</h2>
<p>読み込むテキストファイルを以下の<em>aLine.txt</em>とします。<br />
<em>aLine.txt</em></p>
<pre>
Unless I set the standard where I am in any level, I'll be puzzled about what I should do from now on.
</pre>
<h3>コマンドラインで適切な引数を与えた場合</h3>
<pre>
$ python excp01.py aLine.txt
aLine.txt contains 22 words.

"excp01.py" process end.
</pre>
<h3>存在しないファイル名を引数を与えた場合</h3>
<pre>
$ python excp01.py texfile.tex
"texfile.tex" cannot be opened.

"excp01.py" process end.
</pre>
<h3>コマンドライン引数を与えなかった場合</h3>
<pre>
$ python excp01.py
Usage: excp01.py TEXTFILE

"excp01.py" process end.
</pre>
<h2>例外オブジェクト名をpython処理系に聞いてみる</h2>
<p>どんなメソッドや関数、演算子が、どのような例外を投げるのか予測が付かない場合があるかと思います。<br />
そんな場合でもとりあえずスクリプトを実行してエラーを確認してみましょう。<br />
<code>SyntaxError: invalid syntax</code>以外のエラーがある場合、例えば、</p>
<pre>
$ python excp01.py
Traceback (most recent call last):
  File "excp01.py", line 5, in <module>
    arg = sys.argv[1]
IndexError: list index out of range
</pre>
<p>のような場合は、最終行の行頭のIndexErrorが例外名となります。</p>
<h2>何でも例外任せにしていいの？</h2>
<p>計算量からみてあまり良いとは言えません。今プログラムのコマンドライン引数の確認を、<br />
<code>if len(sys.argv) != 2: ホニャララ</code><br />
とすると整数の比較ですみますが、例外のキャッチに任せると、例外インスタンスを生成し送出するという、結構な計算量がかかります。ネットワーク接続やファイル読み書きなどのIOでは有効ですが、それ以外では積極的に使わないほうがいいかもしれません。</p>
<h2>ドキュメント</h2>
<ul>
<li><a href="http://docs.python.org/lib/module-exceptions.html" title="2.3 Built-in Exceptions" target="_blank">2.3 Built-in Exceptions</a></li>
</ul>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/09/python-add-line-number-to-text-file.html" rel="bookmark" title="2008年9月8日">Python: テキストファイルの行頭に行番号を追加</a></li>
<li><a href="http://www.yukun.info/blog/2008/07/python-command-line-arguments.html" rel="bookmark" title="2008年7月26日">Python: コマンドライン引数の取得 &#8211; sys.argv変数</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-csv-read.html" rel="bookmark" title="2008年6月17日">Python: CSVファイルの読み込み &#8211; csv.readerオブジェクト</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-file.html" rel="bookmark" title="2008年6月9日">Python: テキストファイルの読み込み &#8211; read()、readlines()、readline()メソッド</a></li>
<li><a href="http://www.yukun.info/blog/2008/08/python-directory-listdir-glob.html" rel="bookmark" title="2008年8月9日">Python: 指定したパスのディレクトリ中のファイル一覧を出力</a></li>
</ul>
<p><!-- Similar Posts took 9.386 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/09/python-read-file-try-except-else-finally.html">Python: ファイル読み込み時の例外の扱い例 &#8211; try、except、else、finallyブロック</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/2008/09/python-read-file-try-except-else-finally.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FedoraにGUI環境GNOMEをyumでインストール</title>
		<link>http://www.yukun.info/blog/2008/09/fedora-yum-install-gnome.html</link>
		<comments>http://www.yukun.info/blog/2008/09/fedora-yum-install-gnome.html#comments</comments>
		<pubDate>Mon, 15 Sep 2008 12:50:11 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[Setting]]></category>

		<guid isPermaLink="false">http://www.yukun.info/?p=1190</guid>
		<description><![CDATA[Fedoraのセットアップ時にGUI環境(GNOME, KDE)をインストールしていないが、セットアップ後にyumコマンドでGNOMEをインストールする手順を以下に示します。 XとGNOMEのインストール 以下のコマンド &#8230; <a href="http://www.yukun.info/blog/2008/09/fedora-yum-install-gnome.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/09/fedora-yum-install-gnome.html">FedoraにGUI環境GNOMEをyumでインストール</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Fedoraのセットアップ時にGUI環境(GNOME, KDE)をインストールしていないが、セットアップ後にyumコマンドでGNOMEをインストールする手順を以下に示します。</p>
<h2>XとGNOMEのインストール</h2>
<p>以下のコマンドを打ち込みます。</p>
<pre>
# yum groupinstall "X Window System" "GNOME Desktop Environment"
</pre>
<p>もしKDEをインストールする場合は、</p>
<pre>
yum groupinstall "X Window System" "KDE (K Desktop Environment)"
</pre>
<p>何事も無ければ、大体230MBぐらいD&#038;Iしますので、他のことでもしながら待ちます。<br />
しかし、以下のようなエラーが出た場合、</p>
<pre>
Error: Missing Dependency: policycoreutils = 2.0.31-7.fc8 is needed by package policycoreutils-gui
</pre>
<p>Error: Missing Dependencyは依存関係の問題です。上の例では、policycoreutils-guiをインストールするのに必要なpolicycoreutils(2.0.31-7.fc8)が無いですよーと言っています。<br />
念のため、以下のコマンドで必要なものがインストールされているか調べます。</p>
<pre>
# yum info policycoreutils
</pre>
<p>仮にインストールされている場合、そのバージョン番号も表示されますので、その番号が必要とされているものと一致しているかどうか確認します。ここでは2.0.31-7.fc8ですね。<br />
一致していない場合は、yumでupdateか再インストール(removeとinstall)しましょう。もし、一致しているのに上記のエラーが出た場合でも、yum updateで一回システム全体をアップデートしてみてください。その後再度、</p>
<pre>
# yum groupinstall "X Window System" "GNOME Desktop Environment"
</pre>
<p>を行えば、インストールが始まります。</p>
<h2>Fedora起動時にXを立ち上げる</h2>
<p>毎回コマンドでstartxと打ってX Window SystemとGNOMEを立ち上げるのも手間ですので以下のファイルを修正して、起動時にGNOMEセッションが使えるようにします。<br />
ファイル/etc/inittabの18行目付近にある<br />
id:3:initdefault: を<br />
id:5:initdefault: に書き換えます。<br />
その後、再起動すればOKです。</p>
<h2>日本語入力環境(scim)などのインストール</h2>
<p>以下のyumコマンドをroot権限で実行しインストールてください。</p>
<pre>
# yum groupinstall 'Japanese Support'
＜中略＞
Installing:
 scim-anthy              i386      1.2.4-2.fc8       fedora               365 k
 scim-lang-japanese      i386      1.4.7-7.fc8       fedora                23 k
Installing for dependencies:
 im-chooser              i386      0.5.3-1.fc8       fedora                82 k
 scim                    i386      1.4.7-7.fc8       fedora               475 k
 scim-bridge             i386      0.4.14-1.fc8      updates-newkey        99 k
 scim-bridge-gtk         i386      0.4.14-1.fc8      updates-newkey        39 k
＜後略＞
</pre>
<p>再起動すれば、「半角/全角」キーで日本語入力が可能になります。</p>
<p>………大学ではLinuxメインですが、自宅ではWindowsでCygwin、MinGWや仮想OS、クロスプラットフォームなboostライブラリとかを用いながら騙し々々開発していました。が、もうそんなことやってられる段階でなくなったので(socket, threadまわりの評価の為)今回デプロイ用のサーバに一応の開発環境を整えました。<br />
あんまり大学での作業を持ち込みたくないんだけれどなぁ。スケジュール見直そうかな。</p>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/12/how-to-write-apache-conf.html" rel="bookmark" title="2008年12月25日">Apacheでよく使うコマンドと設定項目</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/install-lxml.html" rel="bookmark" title="2008年6月13日">Python: lxmlのインストール方法</a></li>
<li><a href="http://www.yukun.info/blog/2011/02/vmware-player-centos.html" rel="bookmark" title="2011年2月23日">VMware PlayerでCentOSをインストール</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/sshd-log-file.html" rel="bookmark" title="2008年6月24日">sshdのログファイルの確認方法</a></li>
<li><a href="http://www.yukun.info/blog/2011/12/linux-redmine-subversion-install.html" rel="bookmark" title="2011年12月6日">Linux: RedmineとSubversionのインストール・設定例</a></li>
</ul>
<p><!-- Similar Posts took 10.453 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/09/fedora-yum-install-gnome.html">FedoraにGUI環境GNOMEをyumでインストール</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/2008/09/fedora-yum-install-gnome.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python: テキストファイルの行頭に行番号を追加</title>
		<link>http://www.yukun.info/blog/2008/09/python-add-line-number-to-text-file.html</link>
		<comments>http://www.yukun.info/blog/2008/09/python-add-line-number-to-text-file.html#comments</comments>
		<pubDate>Mon, 08 Sep 2008 12:50:42 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[Module]]></category>
		<category><![CDATA[read]]></category>
		<category><![CDATA[write]]></category>

		<guid isPermaLink="false">http://www.yukun.info/?p=1017</guid>
		<description><![CDATA[コマンドラインで指定されたテキストファイルの行頭に行番号を追加し、そのデータを新たなファイルに書き出すスクリプトです。 ソースコード #!/usr/bin/python # coding: UTF-8 import sy &#8230; <a href="http://www.yukun.info/blog/2008/09/python-add-line-number-to-text-file.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/09/python-add-line-number-to-text-file.html">Python: テキストファイルの行頭に行番号を追加</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>
#!/usr/bin/python
# coding: UTF-8

import sys

argvs = sys.argv
argc = len(argvs)
if (argc != 3):
    print 'Usage: $ python %s target_file making_file' % argvs[0]
    quit()
f = open(argvs[1])
lines2 = f.readlines()
f.close()
nf = open(argvs[2], 'w')
i = 1
for line in lines2:
    nf.write('%3d: %s' % (i, line))
    i = i + 1
nf.close()
</pre>
<p>仮にこのソースコードをfile05a.pyというファイルで保存した場合、使用する際はプロンプトに以下のように打ち込みます。</p>
<h3>実行結果</h3>
<h4>プロンプト</h4>
<pre>
> python file05a.py file05a.py file05a-l.txt
</pre>
<p>この結果、生成されたファイルが下記になります。</p>
<h4>file05a-l.txt</h4>
<pre>
  1: #!/usr/bin/python
  2: # coding: UTF-8
  3:
  4: import sys
  5:
  6: argvs = sys.argv
  7: argc = len(argvs)
  8: if (argc != 3):
  9:     print 'Usage: $ python %s target_file making_file' % argvs[0]
 10:     quit()
 11: f = open(argvs[1])
 12: lines2 = f.readlines()
 13: f.close()
 14: nf = open(argvs[2], 'w')
 15: i = 1
 16: for line in lines2:
 17:     nf.write('%3d: %s' % (i, line))
 18:     i = i + 1
 19: nf.close()
</pre>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/07/python-command-line-arguments.html" rel="bookmark" title="2008年7月26日">Python: コマンドライン引数の取得 &#8211; sys.argv変数</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-file.html" rel="bookmark" title="2008年6月9日">Python: テキストファイルの読み込み &#8211; read()、readlines()、readline()メソッド</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-csv-read.html" rel="bookmark" title="2008年6月17日">Python: CSVファイルの読み込み &#8211; csv.readerオブジェクト</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/python-file-write-writelines.html" rel="bookmark" title="2008年9月6日">Python: テキストファイルに書き込み &#8211; write()、writelines()メソッド</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/python-read-file-try-except-else-finally.html" rel="bookmark" title="2008年9月18日">Python: ファイル読み込み時の例外の扱い例 &#8211; try、except、else、finallyブロック</a></li>
</ul>
<p><!-- Similar Posts took 10.746 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/09/python-add-line-number-to-text-file.html">Python: テキストファイルの行頭に行番号を追加</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/2008/09/python-add-line-number-to-text-file.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Python: コマンドライン引数の取得 &#8211; sys.argv変数</title>
		<link>http://www.yukun.info/blog/2008/07/python-command-line-arguments.html</link>
		<comments>http://www.yukun.info/blog/2008/07/python-command-line-arguments.html#comments</comments>
		<pubDate>Fri, 25 Jul 2008 15:00:55 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[File]]></category>
		<category><![CDATA[Module]]></category>
		<category><![CDATA[read]]></category>

		<guid isPermaLink="false">http://trumpcode.yukun.info/?p=213</guid>
		<description><![CDATA[コマンドラインで与える引数によってプログラムの挙動を変えたいという場面はよくあります。Python ではコマンドライン引数は sys モジュールの argv 属性に文字列を要素とするリストとして格納されています。そして、 &#8230; <a href="http://www.yukun.info/blog/2008/07/python-command-line-arguments.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/07/python-command-line-arguments.html">Python: コマンドライン引数の取得 &#8211; sys.argv変数</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>コマンドラインで与える引数によってプログラムの挙動を変えたいという場面はよくあります。Python ではコマンドライン引数は <strong>sys モジュールの argv 属性に文字列を要素とするリスト</strong>として格納されています。そして、リストの先頭要素(sys.argv[0])はスクリプトファイル名となっています。</p>
<h2>ソースコード</h2>
<pre>
# coding: Shift_JIS

import sys # モジュール属性 argv を取得するため

argvs = sys.argv  # コマンドライン引数を格納したリストの取得
argc = len(argvs) # 引数の個数
# デバッグプリント
print argvs
print argc
print
if (argc != 2):   # 引数が足りない場合は、その旨を表示
    print 'Usage: # python %s filename' % argvs[0]
    quit()         # プログラムの終了

print 'The content of %s ...n' % argvs[1]
f = open(argvs[1])
line = f.readline() # 1行読み込む(改行文字も含まれる)
while line:
    print line
    line = f.readline()
f.close
</pre>
<h4>text.txt</h4>
<pre>
It is meaningless only to think my long further aims idly.
It is important to set my aims but at the same time I should confirm my present condition.
Unless I set the standard where I am in any level, I'll be puzzled about what I should do from now on.
</pre>
<h2>実行結果</h2>
<h3>引数を指定しない場合</h3>
<pre>
$ python argv01.py
['argv01.py']
1

Usage: # python SCRIPTNAME.py filename
</pre>
<h3>引数を指定した場合</h3>
<pre>
$ python argv01.py text.txt
['argv01.py', 'text.txt']
2

The content of text.txt ...

It is meaningless only to think my long further aims idly.

It is important to set my aims but at the same time I should confirm my present condition.

Unless I set the standard where I am in any level, I'll be puzzled about what I should do from now on.
</pre>
<h3>リファレンス</h3>
<ul>
<li><a href="http://docs.python.org/tut/node12.html" title="10. Brief Tour of the Standard Library" target="_blank">10. Brief Tour of the Standard Library</a>
<ul>
<li><a href="http://docs.python.org/tut/node12.html#SECTION0012300000000000000000" title="10.3 Command Line Arguments" target="_blank">10.3 Command Line Arguments</a></li>
</ul>
</li>
</ul>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/09/python-add-line-number-to-text-file.html" rel="bookmark" title="2008年9月8日">Python: テキストファイルの行頭に行番号を追加</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-file.html" rel="bookmark" title="2008年6月9日">Python: テキストファイルの読み込み &#8211; read()、readlines()、readline()メソッド</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/python-read-file-try-except-else-finally.html" rel="bookmark" title="2008年9月18日">Python: ファイル読み込み時の例外の扱い例 &#8211; try、except、else、finallyブロック</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/python-file-write-writelines.html" rel="bookmark" title="2008年9月6日">Python: テキストファイルに書き込み &#8211; write()、writelines()メソッド</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-csv-read.html" rel="bookmark" title="2008年6月17日">Python: CSVファイルの読み込み &#8211; csv.readerオブジェクト</a></li>
</ul>
<p><!-- Similar Posts took 11.167 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/07/python-command-line-arguments.html">Python: コマンドライン引数の取得 &#8211; sys.argv変数</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/2008/07/python-command-line-arguments.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ユーザー管理に関するLinuxコマンド</title>
		<link>http://www.yukun.info/blog/2008/06/linux-users.html</link>
		<comments>http://www.yukun.info/blog/2008/06/linux-users.html#comments</comments>
		<pubDate>Wed, 25 Jun 2008 15:00:00 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[User]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/19700101/%e3%83%a6%e3%83%bc%e3%82%b6%e3%83%bc%e7%ae%a1%e7%90%86%e3%81%ab%e9%96%a2%e3%81%99%e3%82%8blinux%e3%82%b3%e3%83%9e%e3%83%b3%e3%83%89</guid>
		<description><![CDATA[最近ファイルやディレクトリのパーミッション設定と合わせてよく使っているコマンドなので、備忘録を兼ねて使用例を書き出してみます。 ./app ディレクトリを someuser ユーザー、 apache グループに変更する。 &#8230; <a href="http://www.yukun.info/blog/2008/06/linux-users.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/06/linux-users.html">ユーザー管理に関するLinuxコマンド</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>最近ファイルやディレクトリのパーミッション設定と合わせてよく使っているコマンドなので、備忘録を兼ねて使用例を書き出してみます。</p>
<p>./app ディレクトリを someuser ユーザー、 apache グループに変更する。</p>
<pre># chown -R someuser:apache ./app</pre>
<p>ユーザーのファイルやディレクトリ所有者がひょんなことから root に変わってしまったとき、例えば、スーパーユーザで一般ユーザのファイルやディレクトリを操作した場合等、に設定しなおす際は以下のように。</p>
<pre># chown -R hoge:hoge ./hoge</pre>
<p>apache ユーザを nagios グループに所属させる。</p>
<pre># usermod -G nagios apache</pre>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/12/how-to-write-apache-conf.html" rel="bookmark" title="2008年12月25日">Apacheでよく使うコマンドと設定項目</a></li>
<li><a href="http://www.yukun.info/blog/2008/01/euc-to-utf8.html" rel="bookmark" title="2008年1月12日">Linuxコマンドで複数ファイルの文字コードを一括変換</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/sshd-log-file.html" rel="bookmark" title="2008年6月24日">sshdのログファイルの確認方法</a></li>
<li><a href="http://www.yukun.info/blog/2011/08/wordpress-file-access.html" rel="bookmark" title="2011年8月9日">WordPress: 解決策→「要求されたアクションを実行するには、WordPress が Web サーバーにアクセスする必要があります。」</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/r-set-work-directory.html" rel="bookmark" title="2008年9月3日">Rで統計: 作業ディレクトリの設定と確認 &#8211; setwd()、getwd()関数</a></li>
</ul>
<p><!-- Similar Posts took 11.211 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/06/linux-users.html">ユーザー管理に関するLinuxコマンド</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/2008/06/linux-users.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>sshdのログファイルの確認方法</title>
		<link>http://www.yukun.info/blog/2008/06/sshd-log-file.html</link>
		<comments>http://www.yukun.info/blog/2008/06/sshd-log-file.html#comments</comments>
		<pubDate>Mon, 23 Jun 2008 15:00:00 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[Server]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/19700101/sshd%e3%81%ae%e3%83%ad%e3%82%b0%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%ae%e7%a2%ba%e8%aa%8d%e6%96%b9%e6%b3%95</guid>
		<description><![CDATA[サーバで sshd サービスを使っていると、招かれざる人(大抵bot)も結構な頻度でアクセスしてきます。何時何処からどのようなアクセスがあるのかは知っておく為にログファイルをモニタリング。 sshd のログファイルのパス &#8230; <a href="http://www.yukun.info/blog/2008/06/sshd-log-file.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/06/sshd-log-file.html">sshdのログファイルの確認方法</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>サーバで sshd サービスを使っていると、招かれざる人(大抵bot)も結構な頻度でアクセスしてきます。何時何処からどのようなアクセスがあるのかは知っておく為にログファイルをモニタリング。</p>
<p>sshd のログファイルのパスは Fedora では通常 /var/log/secure です。</p>
<pre>
# cat /var/log/secure
</pre>
<p>ちなみに、ssh のログインに失敗/成功したユーザ数は以下の様なコマンド等でカウントできます。</p>
<pre>
# grep -c invalid /var/log/secure
# grep -c Failed /var/log/secure
# grep -c Accepted /var/log/secure
</pre>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/12/how-to-write-apache-conf.html" rel="bookmark" title="2008年12月25日">Apacheでよく使うコマンドと設定項目</a></li>
<li><a href="http://www.yukun.info/blog/2008/01/euc-to-utf8.html" rel="bookmark" title="2008年1月12日">Linuxコマンドで複数ファイルの文字コードを一括変換</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/linux-users.html" rel="bookmark" title="2008年6月26日">ユーザー管理に関するLinuxコマンド</a></li>
<li><a href="http://www.yukun.info/blog/2011/04/openssh-server.html" rel="bookmark" title="2011年4月27日">OpenSSH サーバのインストールと公開鍵・秘密鍵の作成・設置</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 9.302 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/06/sshd-log-file.html">sshdのログファイルの確認方法</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/2008/06/sshd-log-file.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Linuxコマンドで複数ファイルの文字コードを一括変換</title>
		<link>http://www.yukun.info/blog/2008/01/euc-to-utf8.html</link>
		<comments>http://www.yukun.info/blog/2008/01/euc-to-utf8.html#comments</comments>
		<pubDate>Fri, 11 Jan 2008 15:00:00 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Character]]></category>
		<category><![CDATA[Command]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/19700101/linux%e3%82%b3%e3%83%9e%e3%83%b3%e3%83%89%e3%81%a7%e8%a4%87%e6%95%b0%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%ae%e6%96%87%e5%ad%97%e3%82%b3%e3%83%bc%e3%83%89%e3%82%92%e4%b8%80%e6%8b%ac%e5%a4%89</guid>
		<description><![CDATA[Linux系OSのfedora6のデフォルト文字コードはUTF8なので、先日久々に参照したEUCのC++ソースコード中のコメントや出力が文字化けしていました。 そこで、ファイルの文字コードをEUCからUTF8に変換するコ &#8230; <a href="http://www.yukun.info/blog/2008/01/euc-to-utf8.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/01/euc-to-utf8.html">Linuxコマンドで複数ファイルの文字コードを一括変換</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Linux系OSのfedora6のデフォルト文字コードはUTF8なので、先日久々に参照したEUCのC++ソースコード中のコメントや出力が文字化けしていました。</p>
<p>そこで、ファイルの文字コードをEUCからUTF8に変換するコマンドを調べたところ、<a href="http://www.phppro.jp/phptips/archives/vol30/">PHPプロ！TIPS+</a>のページの中程にそれに関するコマンドがあったので参考にしました。</p>
<pre>$find -name '*.cc' | xargs nkf --overwrite -w</pre>
<p>↑は拡張子がccの全てのテキストファイルの文字コードをutf8に変換します。</p>
<pre>$find . -type f -print0 | xargs -0 nkf --overwrite -w -Lu</pre>
<p>↑このコマンドの意味を簡単に示しますと、まずファイルを検索するfindコマンドで、カレントディレクトリ「.」から通常ファイル「-type f」を探索し出力します「-print0」(常に真)。</p>
<blockquote><p>% find [検索開始ディレクトリ] (option)<br />
<span style="font-style:italic;">参考:<a href="http://www.k-tanaka.net/unix/find.html">UNIXコマンド [find]</a></span></p></blockquote>
<p>ここで、findコマンドの結果をパイプ「|」をもって渡し、そこでxargsでコマンドを実行します。ここでxargsは以下の機能を持ちます。</p>
<blockquote><p>xargs[えっくす・あーぐす]<br />
標準入力から引数を読み込み、指定のコマンドを実行するコマンド<br />
<span style="font-style:italic;">参考:<a href="http://x68000.q-e-d.net/~68user/unix/pickup?xargs">UNIXの部屋 検索:xargs (*BSD/Linux/Solaris)</a></span></p></blockquote>
<p>文字コード変換コマンドである nkf のオプション&#8211;overwriteは変換した文字コードのデータを元のファイルに上書きするもので、-wが文字コードをUTF8に指定するものです。ちなみに、EUCに変換したい場合は-e、Windowsで使われているSJISにする場合は-sを代わりに指定します。</p>
<p>最後の-Luオプションは改行コードをLFに指定するものです。</p>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/06/linux-users.html" rel="bookmark" title="2008年6月26日">ユーザー管理に関するLinuxコマンド</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/sshd-log-file.html" rel="bookmark" title="2008年6月24日">sshdのログファイルの確認方法</a></li>
<li><a href="http://www.yukun.info/blog/2008/12/how-to-write-apache-conf.html" rel="bookmark" title="2008年12月25日">Apacheでよく使うコマンドと設定項目</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/fedora-yum-install-gnome.html" rel="bookmark" title="2008年9月15日">FedoraにGUI環境GNOMEをyumでインストール</a></li>
<li><a href="http://www.yukun.info/blog/2008/01/ruby-string-index.html" rel="bookmark" title="2008年1月5日">Rubyで文字列から日本語文字をインデックス指定する</a></li>
</ul>
<p><!-- Similar Posts took 11.202 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/01/euc-to-utf8.html">Linuxコマンドで複数ファイルの文字コードを一括変換</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/2008/01/euc-to-utf8.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

