perl script command line option driven script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl script command line option driven script
# 1  
Old 12-20-2010
perl script command line option driven script

could someone show me a sample command line option driven script?

i want to see an easy way to write one and how i can execute it using command line options such as typing in

read.pl -i <id> -c <cmds> -s <start> -e <end>

would read out all the commands run by ID . from start time to end time.
# 2  
Old 12-21-2010
Code:
$
$ cat perl_getopt
#! /usr/bin/perl -w
use Getopt::Std;

our($opt_c, $opt_s, $opt_e);
getopt('c:s:e:');

if (defined($opt_c)) {print "option c is $opt_c \n";}
if (defined($opt_s)) {print "option s is $opt_s \n";}
if (defined($opt_e)) {print "option e is $opt_e \n";}

foreach $arg (@ARGV) { print "arg = $arg\n"; }
$
$
$
$
$ ./perl_getopt -s sval  -c cval   one two 333 fore
option c is cval
option s is sval
arg = one
arg = two
arg = 333
arg = fore
$

This User Gave Thanks to Perderabo For This Post:
# 3  
Old 12-21-2010
Hi.

Also:
Code:
-s enables rudimentary switch parsing for switches on the command
line after the program name but before any filename arguments (or
before an argument of --). Any switch found there is removed from
@ARGV and sets the corresponding variable in the Perl program. The
following program prints "1" if the program is invoked with a -xyz
switch, and "abc" if it is invoked with -xyz=abc.

#!/usr/bin/perl -s
if ($xyz) { print "$xyz\n" }

-- excerpt from man perlrun , q.v.

So, for a file p1:
Code:
#!/usr/bin/perl -s
if ($xyz) { print "$xyz\n" }

Running as directed, produces:
Code:
% ./p1 -xyz="Hello, world."
Hello, world.

My current preference is to use Getopt::Euclid because it generates a man page and a command line parser. I would not call it easy, so Getopt::Std is preferable from that respect. The option -s is probably the easiest, but the man page should be read carefully ... cheers, drl
This User Gave Thanks to drl For This Post:
# 4  
Old 12-21-2010
Quote:
Originally Posted by Perderabo
Code:
$
$ cat perl_getopt
#! /usr/bin/perl -w
use Getopt::Std;
 
our($opt_c, $opt_s, $opt_e);
getopt('c:s:e:');
 
if (defined($opt_c)) {print "option c is $opt_c \n";}
if (defined($opt_s)) {print "option s is $opt_s \n";}
if (defined($opt_e)) {print "option e is $opt_e \n";}
 
foreach $arg (@ARGV) { print "arg = $arg\n"; }
$
$
$
$
$ ./perl_getopt -s sval  -c cval   one two 333 fore
option c is cval
option s is sval
arg = one
arg = two
arg = 333
arg = fore
$


thank you for your reply.

excuse me but why are there $ in all the lines?
# 5  
Old 12-21-2010
Quote:
Originally Posted by kpddong
excuse me but why are there $ in all the lines?
That's how you indicate a scalar variable. You can't be very far into Perl if you are not acquanted with that use of the dollar sign. You may want to download the Perl tutorial from here: Featured Books and Articles by Active Forum Members - Links
# 6  
Old 12-21-2010
Sorry, I should have been more clear.

I know the $ indicates that it is a scalar variable but i have never seen $ by itself on a line.

I know that $opt_c is a variable and i know what $_ does but what does $ do by itself on it's own line.
# 7  
Old 12-21-2010
Oh. That is called a "prompt". The shell displays it to indicate that I can type something.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. Shell Programming and Scripting

Menu Driven Bash Shell Script with Default Option

Hi All, I have written a menu driven bash shell script. Current Output is as below: ------------------------------------- Main Menu ------------------------------------- Option 1 Option 2 Option 3 Option 4 Exit ===================================== Enter your... (3 Replies)
Discussion started by: kiran_j
3 Replies

3. UNIX for Dummies Questions & Answers

Command line / script option to filter a data set by values of one column

Hi all! I have a data set in this tab separated format : Label, Value1, Value2 An instance is "data.txt" : 0 1 1 -1 2 3 0 2 2 I would like to parse this data set and generate two files, one that has only data with the label 0 and the other with label -1, so my outputs should be, for... (1 Reply)
Discussion started by: gnat01
1 Replies

4. UNIX for Dummies Questions & Answers

Executing a tar command with the --exclude option in a Debian shell script.

Hi All, I am trying to execute the following tar command with two --exclude options to suppress extract of the two directories specified. Do I need to single quote the directory paths ?? Many thanks for your help. The relevant code excerpt from the script is: cd /var/www/${SITE} ... (7 Replies)
Discussion started by: daveu7
7 Replies

5. Shell Programming and Scripting

perl script - command line parameter

i am a beginner, i want to make a program that takes any command line arguments... and print it out in reverse. ie. if the command line argument is "thanks for helping me" i want it to output "me helping for thanks" :D i have tried using the reverse command, but i cant get it working!! ... (3 Replies)
Discussion started by: bshell_1214
3 Replies

6. Shell Programming and Scripting

Run perl script, with command-line options

Hello everyone, I have a perl script which takes various command line options from user like : test.pl -i <input_file> -o <output_file> -d <value> -c <value> Now I have multiple input files in a directory: <input_file_1> <input_file_2> <input_file_3> <input_file_4> ..... .... ...... (6 Replies)
Discussion started by: ad23
6 Replies

7. Shell Programming and Scripting

perl command driven script

so i have 3 options - i, d, a - for id, date, and arguments therefore perl test.pl -i admin -d 11/1/1 would show me the results of a table with the id admin on the date of 11/1/1. i would like perl test.pl -i admin -d 11/1/1 -a to show me results of a table with the id, admin on the... (0 Replies)
Discussion started by: kpddong
0 Replies

8. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

9. UNIX for Dummies Questions & Answers

What is a menu or command line option driven script?

i'm confused what this means. i was asked to design a menu or command line option driven script that reads out of a DB and displays info such as read_data.pl -u <user> -e <event> which would print commands run by <user>with the <event> in the db. any suggestions? i've been using... (2 Replies)
Discussion started by: kpddong
2 Replies

10. Shell Programming and Scripting

changing from command line to perl script

I had posted previously about this problem I had. I have multiple text files with hundreds of lines of the following type: 2000001 34 54 234 2000001 32 545 2000001 -2000001 77 2000001 44 2000001 998 2000001 77 32 2000001 45 23 111 89 98 75 23 34 999 . . . etc... What I wanted was... (2 Replies)
Discussion started by: xchen89x
2 Replies
Login or Register to Ask a Question