943,786 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 895
  • Perl RSS
May 29th, 2009
0

passing command line arguements to script

Expand Post »
Hi,

I have a my script here--

Perl Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
larryperl is offline Offline
49 posts
since Aug 2007
Jun 5th, 2009
0

Re: passing command line arguements to script

You are not using
Perl Syntax (Toggle Plain Text)
  1. Getopt::Std
properly. If you want command line switches you can use Getopt and then say:
Perl Syntax (Toggle Plain Text)
  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:
Perl Syntax (Toggle Plain Text)
  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.
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Regular expression to get multi line comment
Next Thread in Perl Forum Timeline: Copyfile between two server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC