带选项的Perl脚本

时间:2016-10-05 20:40:40

标签: perl shell

我正在寻找执行以下操作的Perl脚本:

  1. 有两个选项,例如script.pl -option1 -option2

  2. 选项1有两个选项,比如choice1choice2,我有shell代码 做两个动作(我希望我可以移植到Perl)

  3. 此处选项2是路径并且是可选的。如果未指定,则使用当前目录

  4. 所需脚本

    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
    

1 个答案:

答案 0 :(得分:-2)

(未经测试)

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

my ($action, $path) = @ARGV;
$path //= '.';
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: $?";

script.pl choice1 /path运行。