![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| perl -write values in a file to @array in perl | meghana | Shell Programming and Scripting | 27 | 06-07-2009 06:05 PM |
| How to Turn perl one-liners into full perl script? | EDALBNUG | UNIX for Dummies Questions & Answers | 1 | 02-04-2009 10:49 AM |
| [Perl] Accessing array elements within a sed command in Perl script | userix | Shell Programming and Scripting | 2 | 10-03-2008 01:05 PM |
| [PERL] Running unix commands within Perl Scripts | userix | Shell Programming and Scripting | 1 | 05-28-2008 07:06 PM |
| Replace Perl Module name in all Perl scripts | rahulrathod | Shell Programming and Scripting | 2 | 12-02-2005 01:00 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
(Perl) GetOptions
hi all -
i'm trying to teach myself some perl, and my first project is to take a bash script i wrote to process some logfiles and port it. that script works well, it's just slow, and the whole thing seemed like a good way to learn. one really nice thing about my bash script is that it looks at any arguments passed, assigns values to variables, and if it finds something's missing, prompts the user for that information before proceeding. i believe i can make the same logic work using perl, but i'm stumped about some specifics. my googling tells me that i probably want to use Getoptions::Long to handle command line arguments; i've gotten fair results using Switch, but i think GetOptions is likely cleaner. i am, of course, open to suggestions. anyway, since the script will do a few different operations (a basic find, count occurrences of $string, etc.), i'm using $operation to hold, well, the operation we're performing, and $string to hold the string we're looking for. i believe i can use GetOptions to process arguments properly if i call the script like, for example, this: logChecker --operation find --address me@domain.com but what i'd really like to do is to call it like this: logChecker.pl -f me@domain.dom (so, find (-f) all instances of the supplied email address) but i can't figure out how to tell GetOptions to assign values to multiple variables based on one pair of options like that. so that's my question: am i asking too much of GetOptions? is it possible for GetOptions to look at something like '-f me@domain.dom' and assign $operation=find AND $string=me@domain.dom? i'm hoping it's possible, but i haven't found an example of it yet. thanks in advance, -john. |
|
|||||
|
Take a look at Getopt::Std instead, might be more of use for you.
|
|
||||
|
thanks for the reply - i actually looked at Getopt::Std in the beginning, and i don't think it's what i'm looking for. i also realized i could have been more specific about what i was trying to do. to that end, here's the relevant snippet from the existing bash script i'm trying to replicate: Code:
while [ $# -gt 0 ]; do case $1 in -f) findOnly=1; item=$2; shift 2;; -v) set -x; shift;; -t) daysBack=$2; shift 2;; -d) op=deliveries; shift;; -h) op=ham; shift;; -s) op=spam; shift;; -A) op=all; item=$2; shift;; esac done as you can see, in a few of those cases, we're assigning a couple of variables at once, which is what's currently tripping me up. there's logic elsewhere that checks for which of those variables have been set, and prompts the user for anything that's missing. later on, the script simply calls 'op' - that is, whichever subroutine was defined by the flag(s) when the script was called. in bash, it was pretty easy, and i'm sure there's a way to replicate the logic in perl, i just can't get my head around it. to be clear, the script wants to work with whatever it's given, whether it's a full set of flags & arguments, a partial set, or nothing at all...for that reason, i (think i) can't simply call a subroutine as soon as i see the flag for it (like -d for 'deliveries') because we need to finish assigning variables first, whether by looking at the rest of @ARGV or by prompting the user. the only way i've been able to come up with is to 1) sort out any passed flags & arguments, 2) check for anything missing & poke at the user, and THEN 3) go do stuff. that seemed like the most sane approach, but i am, as i say, open to any suggestions. thanks again to all, -john. |
|
||||
|
for john
sub readFromCmdLine()
{ my $helpInfo=0; GetOptions("type=s" => \$type "delay=i" => \$configHash{amr_db_mon_delay}, "mode=s" => \$mode, "help" => \$helpInfo); if ($helpInfo) { print "usage:amr_db_monitor.pl [ --delay=<seconds> ] [--mode=bg & ]\n"; exit 1; } } |
|
||||
|
GetOpts is for the benefit of people that use a program, not necessarily the programmer. It emulates/implements the POSIX syntax for command line options, with GNU extensions. That way users (or programmers) that are familiar with those conventions/syntax don't have anything new to learn.
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|