Hello goldenblue.

I am not sure if I fully understand what you are trying to accomplish. I am sure there is a better way to do this, but hopefully what I have listed below will help you. I placed the commands into a Perl module. I inserted tokens in the command hash that will be replaced based on the command input.

From a script you can include the Commands.pm module. The code below will need to be tweaked in order to parse the correct commands.

Commands.pm

#!/usr/bin/perl use strict; use warnings; package Commands; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); use Exporter; our $VERSION=1.0; our @ISA = qw(Exporter); our @EXPORT = qw(%cmd_tmpl eval_cmd); our @EXPORT_OK = qw(); our sub eval_cmd ($); #################################################################### #################################################################### ## Command Hash template our %cmd_tmpl = ( 'backup' => 'token token > token', 'list' => 'token -le token' ); ## Evaluate command , accepts Command , Client, and Arguments our sub eval_cmd ($) { my $array_ref = shift; my $cmd = @{$array_ref}[0]; my $client = @{$array_ref}[1]; my @args; if ( defined @{$array_ref}[2] ) { @args = split(/ /,@{$array_ref}[2]); } die "No such command: $cmd!" unless exists $cmd_tmpl{lc($cmd)}; ## + convert command to lc internally my $cmd_value = $cmd_tmpl{$cmd}; ## update command , replaces first 'token' encountered. $cmd_value =~ s/token/$cmd/; ## update client , replaces first 'token' encountered. $cmd_value =~ s/token/$client/; ## update arguments , replaces first 'token' encountered. for(my $i = 0; $i <=$#args; $i++) { $cmd_value =~ s/token/$args[$i]/; } if ($cmd_value =~ m/token/) { die "Not enough aruments on [@{$array_ref}]"; } return $cmd_value; } #################################################################### #################################################################### 1;

Command_parser.pl

#/usr/bin/perl use strict; use warnings; use Commands 1.0; sub Get_Client(); my @clients; die "Error. Usage \'perl Command_parser.pl -Noclient | [-Command Clien +t1 Arguments -Command Client2 Arguments...] \'\n $!" unless $#ARGV > += 0; #################################################################### #################################################################### ## If a client was not provided at the command line, get one if (lc($ARGV[0]) eq "-noclient") { Get_Client(); } else { my @current_command; for(my $i = 0; $i <=$#ARGV; $i++) { if ( $ARGV[$i] =~ m/^\-/ && $i != 0) { # eval current command , then wipe it and keep processing my $command_value = eval_cmd(\@current_command); + print "Command is :$command_value\n"; ##system($command_value); @current_command = (); push @current_command , $ARGV[$i]; $current_command[0] =~ s/\-//g; } elsif ( $i == $#ARGV ) { # eval last command push @current_command , $ARGV[$i]; my $command_value = eval_cmd(\@current_command); print "Command is :$command_value\n"; ##system($command_value); } else { push @current_command , $ARGV[$i]; if ($i == 0) { $current_command[0] =~ s/\-//g; } } } } #################################################################### #################################################################### ## No client was provided at the command line, fill one in sub Get_Client() { my @current_command = ('backup','mars','file1.txt'); my $cmd_value = eval_cmd(\@current_command); # here magic happens! ##security_check($cmd); ##system("$cmd"); }

Input / Output

C:\monks\command_template>perl Command_parser.pl -backup client1 myfil +e1.txt -backup client2 myfile2.txt -list client3 Command is :backup client1 > myfile1.txt Command is :backup client2 > myfile2.txt Command is :list -le client3

In reply to Re: using a cmd-template by VincentK
in thread using a cmd-template by goldenblue

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.