user2047036 user2047036 - 2 months ago 22
Bash Question

Perl script with options



I am looking for a Perl script which does the following:


  1. takes two options, like
    script.pl -option1 -option2

  2. Option 1 has two choices, say
    choice1
    and
    choice2
    and I have shell code
    to do both actions (which I hope I can port to Perl)

  3. Here option 2 is the path and is optional. If it's not specified it uses the current directory



Desired script



checks arguments

if choice 1 :

go to path (arg2)

run a code (i have it ready)

If choice 2

go to path (arg2 )

run a code

Answer

(not tested)

#! /usr/bin/perl
use warnings;
use strict;

my ($action, $path) = (@ARGV, '.');
chdir $path or die "Cannot chdir to $path.\n";

my $script = {
    choice1 => 'script1.sh',
    choice2 => 'script2.sh',
}->{$action};

die "Invalid choice $action.\n" unless defined $script;
0 == system $script or die "Status: $?";

Run as script.pl choice1 /path.