#!/usr/bin/perl -w
use strict; # always use strict, it's a good habit
use Getopt::Std; # see "perldoc Getopt::Std"
my %options;
getopts('f:hl', /%options); # read the options with getopts
# uncomment the following two lines to see what the options hash contains
#use Data::Dumper;
#print Dumper /%options;
$options{h} && usage(1); # the -h switch, with exit option
# use the -f switch, if it's given, or use a default configuration filename
my $config_file = $options{f} || 'first.conf';
print "Configuration file is $config_file/n";
# check for the -l switch
if ($options{l})
{
system('/bin/ls -l');
}
else
{
my $input; # a variable to hold user input
do {
print "Type 'help' for help, or 'quit' to quit/n-> ";
$input = <>;
print "You entered $input/n"; # let the user know what we got
# note that 'listlong' matches /list/, so listlong has to come first
# also, the i switch is used so upper/lower case makes no difference
if ($input =~ /listlong/i)
{
system('/bin/ls -l');
}
elsif ($input =~ /list/i)
{
system('/bin/ls');
}
elsif ($input =~ /help/i)
{
usage();
}
elsif ($input =~ /quit/i)
{
exit;
}
}
while (1); # only 'quit' or ^C can exit the loop
}
exit; # implicit exit here anyway
# print out the help and exit
sub usage
{
my $exit = shift @_ || 0; # don't exit unless explicitly told so
print <<EOHIPPUS;
first.pl [-l] [-h] [-f FILENAME]
The -l switch lists the files in the current directory, using /bin/ls -l.
The -f switch selects a different configuration file. The -h
switch prints this help. Without the -l or -h arguments, will show
a command prompt.
Commands you can use at the prompt:
list: list the files in the current directory
listlong: list the files in the current directory in long format
help: print out this help
quit: quit the program
EOHIPPUS
exit if $exit;
}