soramineの日記: シェルの動作実習
おはようございますですの。今日も一日がんばりましょうです。
ところで、引数表示プログラムはこんな風になったみたいですの。
って、会社で何をやっているのだろう(笑):いやいやこれも仕事のうちということで(ほんとかな)。
[soramine]$ ./argsdisp 1 2 3 4
arg 0: './argsdisp'
arg 1: '1'
arg 2: '2'
arg 3: '3'
arg 4: '4'
[soramine]$ ./argsdisp this is a pen
arg 0: './argsdisp'
arg 1: 'this'
arg 2: 'is'
arg 3: 'a'
arg 4: 'pen'
[soramine]$ ./argsdisp this\ is\ a\ pen
arg 0: './argsdisp'
arg 1: 'this is a pen'
[soramine]$ ./argsdisp 'this is a pen'
arg 0: './argsdisp'
arg 1: 'this is a pen'
[soramine]$ ./argsdisp "this is a pen"
arg 0: './argsdisp'
arg 1: 'this is a pen'
[soramine]$ ./argsdisp 2> foo
arg 0: './argsdisp'
[soramine@ns perltest]$ ./argsdisp *
arg 0: './argsdisp'
arg 1: 'argsdisp'
arg 2: 'argsdisp.c'
arg 3: 'foo'
arg 4: 'jcode.pl'
arg 5: 'mimew.pl'
[soramine]$
空白をデリミタとして分割してるのかな。
[soramine]$ ./argsdisp 1 2 3 4
は、そのまんま。引数が4つですの。
[soramine]$ ./argsdisp this is a pen
も、空白で区切るので、引数が4つ。単語ごと。
[soramine]$ ./argsdisp this\ is\ a\ pen
は、空白がエスケープシーケンスされていて分割されない。
[soramine]$ ./argsdisp 'this is a pen'
は、シングルクォートで括られているのでひとつ。
[soramine]$ ./argsdisp "this is a pen"
も同じくひとつ。
[soramine]$ ./argsdisp 2> foo
は、エラー出力がfooに入るのだけれど、引数はない。fooというファイルは作成される(エラーがあるならエラー文が入る)。
[soramine@ns perltest]$ ./argsdisp *
*は、条件にあうものすべてにパターンマッチするものを表す記号。この場合は同じディレクトリにあるファイル名が全部入る。たぶん、シェルが * を処理してからプログラムを実行してるですの。
シェルにある組み込みコマンドと、普通のコマンド(プログラムの実行)と、あるみたいですの。あと、リダイレクトとか、パイプラインとかも。
おまけ:
#! /usr/bin/perl
# show arguments perl script
# argsdisp.pl
# original c programed by BAK
# http://srad.jp/journal.pl?op=display&uid=1020
# main
{
# variables initialize
$i = 0;
$argc = @ARGV;
# show arguments
for($i=0; $i$argc; ++$i)
{
printf("arg %2d: '%s'\n", $i, $ARGV[$i]);
}
}# end of main