The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-25-2009
fearboy fearboy is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 9
(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.
  #2 (permalink)  
Old 02-25-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,932
Take a look at Getopt::Std instead, might be more of use for you.
  #3 (permalink)  
Old 02-28-2009
fearboy fearboy is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 9
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.
  #4 (permalink)  
Old 04-13-2009
vishal kumar vishal kumar is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 1
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;
}
}
  #5 (permalink)  
Old 04-13-2009
fearboy fearboy is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 9
hi vishal -

thanks for your reply - i couldn't get anywhere with this problem, so i'd put it on the shelf for a while.

can you please explain a bit about what your code does? i can't quite figure it out.

thanks,
-john.
  #6 (permalink)  
Old 04-13-2009
quirkasaurus's Avatar
quirkasaurus quirkasaurus is offline
Registered User
  
 

Join Date: Jan 2009
Location: canton, michigan
Posts: 373
for the most part.. .. . i hate GetOpts commands. Using them is typically
more confusing than just creating a nasty if/then/else statement. This belief
of mine is once again supported by seeing the trouble you're having with it.

So, here's how I would typically approach this problem:


Code:
#!/usr/local/bin/perl

### 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


for $x ( 0 .. $#ARGV ){

  $arg = $ARGV[$x];

  if ( $arg eq "-f" ){
    $find_only = 1;
    $x++;
    $item = ${ARGV[$x]};
    next;
    }

  if ( $arg eq "-v" ){
    ### no such perl option.
    next;
    }

  if ( $arg eq "-t" ){
    $x++;
    $days_back = ${ARGV[$x]};
    next;
    }

#----------------------------------------------------------------------#
# etc...                                                               #
#----------------------------------------------------------------------#
  }

print "find_only: $find_only \n";
print "item:      $item \n";
print "days_back: $days_back \n";

Test:


Code:
% girl.pl -f superfrogs -t 123
find_only: 1
item:      superfrogs
days_back: 123

  #7 (permalink)  
Old 04-13-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0