in reply to Interpolating variables from a file

Rich36,

I'm not quite understanding the flow here. Are you

     1. reading in unix "shell" commands
     2. trying to "fill" the shell variables via perl
     3. execute the input via system, or backticks, or qx

That's pretty wild! I guess there's no changing the config to perl code? Oh well, worth a try. I think you'll need to set your environment and then execute the commands:
$user = $ENV{USER}; $ENV{user} = $user; # Case sensitivity. Now $user should # be visible to your script snippets foreach $line (@lines) { system( $line ); }
And of course - all normal danger caveats do apply.

-derby

Replies are listed 'Best First'.
Re: Re: Interpolating variables from a file
by Rich36 (Chaplain) on Sep 19, 2001 at 01:28 UTC
    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....