1 and 3. What it's actually being used for is this - In the course of my work, I've got to login to a bunch of different servers under a bunch of different ids - sometimes my own, sometimes an anonymous id. So what this application does is provide a menu of servers to login into - which saves me from having to type a lot or set up/remember a bunch of aliases.
After selecting a number, the application calls (with exec()), "rlogin <server_name> -l <user_id>". Here's what the code looks like with the implementation of tye's suggestion (I had to made one minor change in the regex - '@\$' instead of '@$'. With the original one, I was getting '${$user_value}' instead of '{user_value}').
#######################################################
# Setup variables and packages
#######################################################
my $user = $ENV{USER};
my %servernames = ();
my $i = 1;
#######################################################
# MAIN
#######################################################
open(CONFIG, "<lgn.config");
chomp(my @lines = <CONFIG>);
close CONFIG;
foreach my $line(@lines) {
# Add the -l between "server user_name" pattern in $line
$line =~ s/\s/ \-l /;
# Interpolate the value for "$" values
$line =~ s/([@\$]\w[\w\[\]{}']+)/'"'.$1.'"'/gee;
$servernames{"$i"} = $line;
$i++
}
print "***Log in****\n";
print "Select the number of a server and user name combination\n";
foreach my $key(sort{ $a <=> $b }(keys %servernames)) {
print "\($key\) $servernames{$key}\n";
}
print ": ";
chomp(my $ans = <STDIN>);
if (($ans eq "") || ($ans =~ m/\D+/g)) {
die "Please specify a number\n";
}
if (defined($servernames{$ans})) {
my $rlogin = "rlogin $servernames{$ans}";
print qq(Executing "$rlogin"\n);
exec("$rlogin");
} else {
die "Please specify a valid number\n";
}
The config file (lgn.config) would look like:
server1 foo
server2 $user
server2 foo
server3 $user
etc.
Thanks to everyone for their help and suggestions....
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.