rahulme81 has asked for the wisdom of the Perl Monks concerning the following question:

our($opt_e,$opt_r,$opt_l,$opt_s,$opt_h,$opt_d,$opt_u, $opt_m, $opt_v, +$opt_t,$opt_n); my $option_spec ="e:r:l:s:u:v:t:hdm:n" ; my $debug = 0; unless(&getopts($option_spec,\%opt)) ..... ..... ..... print "OPT_V ===> $opt_v \n"; print "OPT_N ===> $opt_n \n"; if(defined $opt_v && $opt_v ne "" ){ $ENV{ROOT} = $ENV{HOME}."/build_XX/".$opt_v if(defined + $opt_n); $ENV{ROOT} = $ENV{HOME}."/build_AB/".$opt_v if(defined + $opt_n && $opt_n ne ""); } else{ $ENV{ROOT} = $ENV{HOME}."/build_XX/" if(defined $opt_n +); $ENV{ROOT} = $ENV{HOME}."/build_AB/" if(defined $opt_n + && $opt_n ne ""); } print " ---- $ENV{ROOT} \n"; print "OPT_V ===> $opt_v \n"; print "OPT_N ===> $opt_n \n";

PROBLEM I am running my script and issue is with the value of argument $opt_n.

I call my script as $HOME/myfiles -r XX10.0.0.1 -v XX10.0.0.1_1 -n SRV_PROD this produces the output as below:

---- /local/home/mylocation/build_AB/EB10.2.3.3_1

OPT_V ===> EB10.2.3.3_1

OPT_N ===> 1 {This is the value where I expect OPT_N value should be SRV_PROD or anything which I passed on command line

but this always comes as 0 or 1}

How can I get the value of OPT_N as which I passed on command line instead of getting 0 or 1.

Please help on this.

  • Comment on Issue with getopts : Not getting the value as passed on command line
  • Download Code

Replies are listed 'Best First'.
Re: Issue with getopts : Not getting the value as passed on command line
by Corion (Patriarch) on Apr 06, 2016 at 14:10 UTC

    See getopts. The synopsis shows how to differentiate between an option that does not take an argument and one that takes an argument.

    You most likely want n: as option instead of n.

      Thanks Corion