| | |
passing command line arguements to script
Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2007
Posts: 49
Reputation:
Solved Threads: 0
Hi,
I have a my script here--
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
I have a my script here--
Perl Syntax (Toggle Plain Text)
print "The Perl Script does the User health check and system health check...\n"; print "---------------------------------------------------------------------\n"; # use strict; my($OS); $OS = $^O; # need to test @ARGV before GetOptions shifts it if (@ARGV == 0) { print "\nNo options provided, using defaults (use -h to view options)\n"; } #GetOptions( 'h|help' => \$_help # ,'u|userdata' => \$_userdata # ,'s|systemdata' => \$_systemdata # ,'a|authresponsecode' => \$_authresponsecode # ,'o|outpur_dir' => \$_output_dir); #if( $_help ) { # printUsage(); # automatically exits the program #} use Getopt::Std; ############################################################################## # Print the usage info and automatically exit ############################################################################## sub printUsage() { print "\n\nUsage:"; print "\n perl MSLogStat.pl [options] [logfile1] [logfile2] ... [logfile(n)]"; print "\n\nOptions:"; print "\n -h * display usage help"; print "\n -o * redirect the output to a directory"; print "\n -u * display user related data"; print "\n -a * display auth response codes"; print "\n -s * output only system related data"; print "\n"; exit; } ############################################################################## # Print the authresponsecode ############################################################################## sub authResponseCode () { print "AAFW_AUTH_SUCCESS = 0"; print "AAFW_AUTH_MORESTEPS = 1"; print "AAFW_AUTH_ID_NOT_FOUND = 2"; print "AAFW_AUTH_INVALID_CREDENTIAL = 3"; print "AAFW_AUTH_ACCOUNT_EXPIRED = 4"; print "AAFW_AUTH_ACCOUNT_INACTIVE = 5"; print "AAFW_AUTH_ATTEMPTS_EXHAUSTED = 6"; print "AAFW_AUTH_TOKEN_EXPIRED = 7"; print "AAFW_AUTH_CHALLENGE_EXPIRED = 8"; print "AAFW_AUTH_INVALID_REQUEST = 9"; print "AAFW_AUTH_CRED_REISSUED = 10"; print "AAFW_AUTH_INTERNAL_ERROR = 11"; print "AAFW_AUTH_UNSUPPORTED_MECH = 12"; print "AAFW_AUTH_LAST = 13"; } # redirect output to a directory: if( $_output_dir ) { # does this directory already exist? if (! -e $_output_dir) { if( $OS =~ "Win32" ) { `mkdir $_output_dir`; } else { `mkdir -p $_output_dir`; } } if (! -e $_output_dir) { die "Failed to create output directory $_output_dir\n"; } } if( $_authresponsecode ) { print "Printing the Auth Response Codes"; authresponsecode(); } sub userData() { #$LOG = $_logfile_name; $username=$2; open($LOG,"logfile.txt") or die "\nUnable to open log file:\n"; @stock = <$LOG>; @matches = (grep(/$username/, @stock)); print @matches; } my $opt = "hu:sao"; my %options; getopts( $opt , \%options ); printUsage() if defined $options{h} ; authResponseCode() if defined $options{a} ; 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
You are not using properly. If you want command line switches you can use Getopt and then say:
So in your username example use:
Perl Syntax (Toggle Plain Text)
Getopt::Std
Perl Syntax (Toggle Plain Text)
getopts("a:b:u:hs"); 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.
Perl Syntax (Toggle Plain Text)
use Getopt:Std; getopts("u:"); #the colon makes it an input field vs a switch $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
Larry Wall
![]() |
Similar Threads
- Passing command line argument to function (C++)
- executing scripts via command line (Python)
- debugging with C++ command line arguements (C++)
- command line arguments (Java)
- Problem with interpretation of the command line arguments in the script (Shell Scripting)
Other Threads in the Perl Forum
- Previous Thread: Regular expression to get multi line comment
- Next Thread: Copyfile between two server
| Thread Tools | Search this Thread |
Tag cloud for Perl





