- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Enter a variable from the cli when executing a...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2006 12:26 AM
06-26-2006 12:26 AM
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
Solved! Go to Solution.
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2006 12:38 AM
06-26-2006 12:38 AM
Re: Enter a variable from the cli when executing a perl script
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...
- Tags:
- argv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2006 12:58 AM
06-26-2006 12:58 AM
Solution#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2006 12:59 AM
06-26-2006 12:59 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2006 01:16 AM
06-26-2006 01:16 AM
Re: Enter a variable from the cli when executing a perl script
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2006 01:19 AM
06-26-2006 01:19 AM
Re: Enter a variable from the cli when executing a perl script
$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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2006 11:36 PM
06-27-2006 11:36 PM
Re: Enter a variable from the cli when executing a perl script
This is an excelent service, and a great help