#amazon(4797336803)
#amazon(4873115671)
#contents
#br
#adsense(728x90)
#br
* 概要 [#s2d68b60]
- Getopt::Std
- スクリプト実行時のオプションと引数を受け取る
- オプションのみを受け取ることも可能
- オプションは 1 文字しか指定できない
#pre(soft){{
./sample.pl -d /var/tmp &color(Red){オプションと引数};
./sample.pl -h &color(Red){オプションのみ};
}}
+ getopt
-- オプションと引数を受け取る
-- 各オプションには必ず引数が必要
-- ただし、getopt で定義しなければオプションのみを受け取ることが可能
+ getopts
-- オプションを受け取る
-- 定義次第で引数を受け取ることも可能
-- オプションのみの場合、引数の代わりに 1 が入る
* getopt [#oc546866]
** 書式 [#y6d25647]
- オプションの定義
#pre(soft){{
getopt "'''opt'''['''opt2'''...]";
}}
-- '''opt'''
--- オプション、一つのオプションにつきアルファベット 1 文字
- 引数の使用
#pre(soft){{
print "$opt_'''opt'''";
}}
-- $opt_'''opt'''
--- 引数が代入される変数
** 実行方法 [#w12992cc]
- スペース区切りで受け取る場合
#pre(soft){{
'''script'''.pl -'''arg''' '''value''' [ -'''arg2''' '''value2'''...]
}}
- 区切らない場合
#pre(soft){{
'''script'''.pl -'''arg''''''value''' [ -'''arg2''''''value2'''...]
}}
-- 引数名 '''arg''' はアルファベット一文字
** 使用例 [#h9154942]
- ex) 引数を一つ受け取る場合
#pre(soft){{
host# cat sample.pl
#!/usr/bin/perl
use strict;
use Getopt::Std;
our $opt_a;
getopt "a";
if ($opt_a) {
print "argument is $opt_a.\n";
} else {
print "argument is not set.\n";
}
host# ./sample.pl -a value
argument is value.
host# ./sample.pl -a
argument is not set.
host# ./sample.pl
argument is not set.
}}
- ex) 引数を複数受け取る場合
#pre(soft){{
host# cat sample.pl
#!/usr/bin/perl
use strict;
use Getopt::Std;
our ($opt_a, $opt_b, $opt_c);
getopt "abc";
if ($opt_a) {
print "a argument is $opt_a.\n";
} else {
print "a argument is not set.\n";
}
if ($opt_b) {
print "b argument is $opt_b.\n";
} else {
print "b argument is not set.\n";
}
if ($opt_c) {
print "c argument is $opt_c.\n";
} else {
print "c argument is not set.\n";
}
host# ./sample.pl -a value1 -b value2 -c value3
a argument is value1.
b argument is value2.
c argument is value3.
host# ./sample.pl -a value1 -b value2
a argument is value1.
b argument is value2.
c argument is not set.
host# ./sample.pl
a argument is not set.
b argument is not set.
c argument is not set.
}}
* getopts [#d1c83596]
** 書式 [#u1516124]
- オプションの定義
#pre(soft){{
getopts "'''opt'''[:]";
}}
-- '''opt'''
--- オプション、一つのオプションにつきアルファベット 1 文字
--- : (Colon) を付けると引数を受け取ることが出来る
- 引数の使用
#pre(soft){{
print "$opt_'''opt'''";
}}
-- $opt_'''opt'''
--- 引数が代入される変数
--- オプションのみの場合で指定された場合は 1 が入る
*** ハッシュ関数の利用 [#l1511aab]
- $opt_'''opt''' 変数ではなく、ハッシュ変数を使用することも出来る
#pre(soft){{
getopts("'''opt'''", \%'''arg''');
}}
- 引数の使用
#pre(soft){{
print "$'''opt'''{'''arg'''}";
}}
** 実行方法 [#d5ca0d18]
- スペース区切りで受け取る場合
#pre(soft){{
'''script'''.pl -'''arg''' '''value''' [ -'''arg2''' '''value2''']
}}
- 区切らない場合
#pre(soft){{
'''script'''.pl -'''arg''''''value''' [ -'''arg2''''''value2''']
}}
-- 引数名 '''arg''' はアルファベット一文字
** 使用例 [#o901aa41]
- ex) オプションを一つ指定する場合
#pre(soft){{
host# cat sample.pl
#!/usr/bin/perl
use strict;
use Getopt::Std;
our $opt_a;
getopts "a";
if ($opt_a) {
print "argument is $opt_a.\n";
} else {
print "option is not set.\n";
}
host# ./sample.pl -a
argument is 1.
host# ./sample.pl
option is not set.
}}
- ex) 複数のオプションを指定し、オプション a のみ引数を受け取る場合
#pre(soft){{
host# cat sample.pl
#!/usr/bin/perl
use strict;
use Getopt::Std;
our ($opt_a, $opt_b, $opt_c);
getopts "a:bc";
if ($opt_a) {
print "a argument is $opt_a.\n";
} else {
print "a option is not set.\n";
}
if ($opt_b) {
print "b argument is $opt_b.\n";
} else {
print "b option is not set.\n";
}
if ($opt_c) {
print "c argument is $opt_c.\n";
} else {
print "c option is not set.\n";
}
host# ./sample.pl -a aaa -b -c
a argument is aaa.
b argument is 1.
c argument is 1.
host# ./sample.pl -a aaa -b
a argument is aaa.
b argument is 1.
c option is not set.
host# ./sample.pl -a
a option is not set.
b option is not set.
c option is not set.
}}
#br
#adsense(728x90)
#br