passing command line arguements to script

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2007
Posts: 49
Reputation: larryperl is an unknown quantity at this point 
Solved Threads: 0
larryperl larryperl is offline Offline
Light Poster

passing command line arguements to script

 
0
  #1
May 29th, 2009
Hi,

I have a my script here--

  1. print "The Perl Script does the User health check and system health check...\n";
  2. print "---------------------------------------------------------------------\n";
  3. # use strict;
  4. my($OS);
  5. $OS = $^O;
  6. # need to test @ARGV before GetOptions shifts it
  7. if (@ARGV == 0) {
  8. print "\nNo options provided, using defaults (use -h to view options)\n";
  9. }
  10.  
  11. #GetOptions( 'h|help' => \$_help
  12. # ,'u|userdata' => \$_userdata
  13. # ,'s|systemdata' => \$_systemdata
  14. # ,'a|authresponsecode' => \$_authresponsecode
  15. # ,'o|outpur_dir' => \$_output_dir);
  16.  
  17. #if( $_help ) {
  18. # printUsage(); # automatically exits the program
  19. #}
  20. use Getopt::Std;
  21.  
  22.  
  23. ##############################################################################
  24. # Print the usage info and automatically exit
  25. ##############################################################################
  26. sub printUsage() {
  27. print "\n\nUsage:";
  28. print "\n perl MSLogStat.pl [options] [logfile1] [logfile2] ... [logfile(n)]";
  29. print "\n\nOptions:";
  30. print "\n -h * display usage help";
  31. print "\n -o * redirect the output to a directory";
  32. print "\n -u * display user related data";
  33. print "\n -a * display auth response codes";
  34. print "\n -s * output only system related data";
  35. print "\n";
  36. exit;
  37. }
  38. ##############################################################################
  39. # Print the authresponsecode
  40. ##############################################################################
  41. sub authResponseCode () {
  42. print "AAFW_AUTH_SUCCESS = 0";
  43. print "AAFW_AUTH_MORESTEPS = 1";
  44. print "AAFW_AUTH_ID_NOT_FOUND = 2";
  45. print "AAFW_AUTH_INVALID_CREDENTIAL = 3";
  46. print "AAFW_AUTH_ACCOUNT_EXPIRED = 4";
  47. print "AAFW_AUTH_ACCOUNT_INACTIVE = 5";
  48. print "AAFW_AUTH_ATTEMPTS_EXHAUSTED = 6";
  49. print "AAFW_AUTH_TOKEN_EXPIRED = 7";
  50. print "AAFW_AUTH_CHALLENGE_EXPIRED = 8";
  51. print "AAFW_AUTH_INVALID_REQUEST = 9";
  52. print "AAFW_AUTH_CRED_REISSUED = 10";
  53. print "AAFW_AUTH_INTERNAL_ERROR = 11";
  54. print "AAFW_AUTH_UNSUPPORTED_MECH = 12";
  55. print "AAFW_AUTH_LAST = 13";
  56. }
  57. # redirect output to a directory:
  58. if( $_output_dir ) {
  59. # does this directory already exist?
  60. if (! -e $_output_dir) {
  61. if( $OS =~ "Win32" ) {
  62. `mkdir $_output_dir`;
  63. }
  64. else
  65. {
  66. `mkdir -p $_output_dir`;
  67. }
  68. }
  69. if (! -e $_output_dir) {
  70. die "Failed to create output directory $_output_dir\n";
  71. }
  72. }
  73.  
  74. if( $_authresponsecode ) {
  75. print "Printing the Auth Response Codes";
  76. authresponsecode();
  77. }
  78.  
  79. sub userData() {
  80. #$LOG = $_logfile_name;
  81. $username=$2;
  82. open($LOG,"logfile.txt") or die "\nUnable to open log file:\n";
  83. @stock = <$LOG>;
  84. @matches = (grep(/$username/, @stock));
  85. print @matches;
  86. }
  87. my $opt = "hu:sao";
  88. my %options;
  89. getopts( $opt , \%options );
  90. printUsage() if defined $options{h} ;
  91. authResponseCode() if defined $options{a} ;
  92. userData() if defined $options{u} ;

in function userData() i want to make changes so that my script take username as command line arguement and logfile name also to be entered from command line,it can be multiple log files.
Please advice me how can i do that. Please do suggest me if you find something to be changed here to work it more efficiently.

Thanks
NT
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 58
Reputation: mitchems is an unknown quantity at this point 
Solved Threads: 2
mitchems's Avatar
mitchems mitchems is offline Offline
Junior Poster in Training

Re: passing command line arguements to script

 
0
  #2
Jun 5th, 2009
You are not using
  1. Getopt::Std
properly. If you want command line switches you can use Getopt and then say:
  1. getopts("a:b:u:hs");
  2.  
  3. for example. That will automatically put -a data into the variable $opt_a. h and s will be switches and if set (-h for example) will set $opt_h = 1.
So in your username example use:
  1. use Getopt:Std;
  2. getopts("u:"); #the colon makes it an input field vs a switch
  3. $username=$opt_u;
Last edited by mitchems; Jun 5th, 2009 at 12:52 pm.
And don't tell me there isn't one bit of difference between null and space, because that's exactly how much difference there is.

Larry Wall
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC