getopt
- From: theal@xxxxxxxxxxxx ("Tony Heal")
- Date: Wed, 24 Jan 2007 00:22:04 -0500
I found an example using getopt on the web and I am trying to convert it to
my use. Everything works except the last part. What am attempting to do is
create a script which I can pass switches as arguments. Eventually this
script will replace the rm command on my linux server, so that I can create
a trashcan.
The end process will be myscript.pl -rf /home/myfolder and that will pass
the -rf and /home/myfolder arguments to the rm command after copying
everything to the trashcan.
The script as is gets an error when run. My problem is how to make the
argument '-rf' pass to the system() command then I plan to use ARGV[0] to
pass the directory.
#!/usr/bin/perl
use warnings;
use strict;
use vars qw/ %opt /;
sub init()
{
use Getopt::Std;
my $opt_string = 'adfirv';
getopts( "$opt_string", \%opt ) or usage();
usage() if $opt{h};
}
sub usage()
{
print STDERR <<EOF;
This program does...
usage: $0 [-adfirv] [-f file]
bla bla bla
example: $0 -adfirv file
EOF
exit;
}
init();
my @option;
my $options;
push @option => "a" if $opt{a};
push @option => "d" if $opt{d};
push @option => "f" if $opt{f};
push @option => "i" if $opt{i};
push @option => "r" if $opt{r};
push @option => "v" if $opt{v};
$options = `print @option`;
system("ls -$options");
print "\n";
- Follow-Ups:
- Re: getopt
- From: Tom Phoenix
- Re: getopt
- Prev by Date: Re: trouble with list context assignment for substitution inside File::Find &wanted function
- Next by Date: I need to fetch the data from the fixed columns
- Previous by thread: Re: trouble with list context assignment for substitution inside File::Find &wanted function
- Next by thread: Re: getopt
- Index(es):
Relevant Pages
|