Operating System - Linux
1828038 Members
2124 Online
109973 Solutions
New Discussion

Re: Enter a variable from the cli when executing a perl script

 
SOLVED
Go to solution
Steve_617
Advisor

Enter a variable from the cli when executing a perl script


Hi There
If I may ask another silly question

I amtrying to run the script below like I would a normal korn script, in the following format

#./MrtgNew.pl 10.10.10.10 steve

However for some reason it does not pick up my variable from the command line
$1 being the ip address and $2 the community name. What am I doing wrong ?

#! /usr/bin/perl -w
### Format is MrtgNew ipaddress communityname
$NEWRTR=~$1;
$COMMUNITY=~$2;
#`mkdir /home1/mrtg/$NEWRTR`;
print "$1 $2 \n";
print " made directory for $NEWRTR and a comunity of $COMMUNITY\n";
sleep 10;
print "`/usr/local/mrtg-2/bin/cfgmaker --global WorkDir: /home1/mrtg/$NEWRTR -ifref=ip --global 'Options[_]: bits,growri
ght' --output /home1/mrtg/cfg/$NEWRTR.cfg $COMMUNITY@$NEWRTR`";
exit;

#### end of script
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: Enter a variable from the cli when executing a perl script

Hi Steve:

In Perl, the command line arguments appear in @ARGV and can be addressed individually as $ARGV[0], $ARGV[1], etc. for argument-1, argument-2, etc. Note the difference from C or from the shell.

# perl -e 'print $ARGV[0] if @ARGV;shift;print " $ARGV[0]\n" if @ARGV' hello world

Regards!

...JRF...
Jannik
Honored Contributor
Solution

Re: Enter a variable from the cli when executing a perl script

Well try this out

#!/usr/bin/perl -w

### Format is MrtgNew ipaddress communityname
$NEWRTR=$ARGV[0];
$COMMUNITY=$ARGV[1];
#`mkdir /home1/mrtg/$NEWRTR`;
print "$NEWRTR $COMMUNITY \n";
print " made directory for $NEWRTR and a comunity of $COMMUNITY\n";
sleep 10;
print "`/usr/local/mrtg-2/bin/cfgmaker --global WorkDir: /home1/mrtg/$NEWRTR -ifref=ip --global 'Options[_]: bits,growri
ght' --output /home1/mrtg/cfg/$NEWRTR.cfg $COMMUNITY@$NEWRTR`";
exit;

#### end of script
jaton
Steve_617
Advisor

Re: Enter a variable from the cli when executing a perl script


OK
I have tried to assign argv[0] and argv[1], in my script, but still get a blank reply.

My script looks like this:-
#! /usr/bin/perl -w
### Format is MrtgNew ipaddress communityname
$NEWRTR=$ARGV[0];
$COMMUNITY=$ARGV[1];
#`mkdir /home1/mrtg/$NEWRTR`;
print "$1 $2 \n";
print " made directory for $NEWRTR and a comunity of $COMMUNITY\n";

exit;

It still does not work!
I execute it as ./MrtgNew 10.10.10.10 steve

Regards
James R. Ferguson
Acclaimed Contributor

Re: Enter a variable from the cli when executing a perl script

Hi steve:

Your problem is the use of $1 and $2. These variables are valid when you have used a regular expression to capture data but do not otherwise represent what you want.

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: Enter a variable from the cli when executing a perl script

Args do not end up in $1 and $2 in perl

$1 in shell scripts is $ARGV[0] in perl
$2 in shell scripts is $ARGV[1] in perl
etc

If you *really* want it in $1 and $2 (but they then get lost after the first matching regex), you can use obfuscation like

$"="\t";"@ARGV\t"=~/^(.*?)\t(.*?)\t/;

Now you have the first two args in $1 and $2, but I bet that the rest of your co-workers will curse you for that

and mkdir is a builtin function. Don't escape to the shell for that. See

# perldoc -f mkdir

--8<---
#!/usr/bin/perl
use strict;
use warnings;
### Format is MrtgNew ipaddress communityname
my ($NEWRTR, $COMMUNITY) = @ARGV;
mkdir "/home1/mrtg/$NEWRTR", 0777;
print "$NEWRTR $COMMUNITY\n";
print " made directory for $NEWRTR and a comunity of $COMMUNITY\n";

exit;
-->8---

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Steve_617
Advisor

Re: Enter a variable from the cli when executing a perl script

Thanks all for your help.
This is an excelent service, and a great help