#adsense(728x90)
./sample.pl -d /var/tmp オプションと引数 ./sample.pl -h オプションのみ
getopt "opt[opt2...]";
print "$opt_opt";
script.pl -arg value [ -arg2 value2...]
script.pl -argvalue [ -arg2value2...]
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.
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 "opt[:]";
print "$opt_opt";
getopts("opt", \%arg);
print "$opt{arg}";
script.pl -arg value [ -arg2 value2]
script.pl -argvalue [ -arg2value2]
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.
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.
#adsense(728x90)