By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,178 Members | 987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,178 IT Pros & Developers. It's quick & easy.

how to expand a perl variable within a unix command

P: 4
Hi, i have just started using Perl and I'm trying to expand a perl variable within a Unix command in the perl script, for example: -
Expand|Select|Wrap|Line Numbers
  1. foreach $host (@lines)
  3. {
  5.         print "$host\n";
  6.         $sshcmd = "ssh";
  7.         $get = `$sshcmd $host "uptime"`;
  8.         print "$get\n";
  10. }
I want to use the contents of $host for the hostname but when i run the script it just hangs. if anyone could let me know how to expand the variable within the Unix command that would be great.

Feb 18 '10 #1
Share this Question
Share on Google+
6 Replies

Expert Mod 100+
P: 589
What makes you think it's not expanding/interpolating the variable?

It's most likely waiting for user input for the username/password.
Feb 18 '10 #2

P: 4
Hi RonB

If i explicitly specify a hostname instead of $host it works, but nothing happens when I replace the real hostname with $host variable. Also, no username and password is required as I am using ssh keys to connect to all of the servers in my array.
Feb 18 '10 #3

Expert Mod 100+
P: 589
$host does not hold the value you think it holds.

Add this just before the foreach loop and post its output.
Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2. print Dumper \@lines;
Also, always post your code within the code tags, as I did. The code tag button is not available in the "Quick Reply" window. You need to click on the "Go Advanced" button to get it and the other features.
Feb 18 '10 #4

P: 4
Sure, below are my hostnames: -

Expand|Select|Wrap|Line Numbers
  1. bash-3.00# ./
  2. $VAR1 = [
  3.           'lonprodd1
  4. ',
  5.           'cradevd1
  6. ',
  7.           'lonprodgz1
  8. '
  9.         ];
Feb 18 '10 #5

Expert Mod 100+
P: 589
That shows that your hostnames need to have the "\n" stripped off.

Expand|Select|Wrap|Line Numbers
  1. foreach my $host (@lines)
  2. {
  3.     chomp $host;
  4.     print "$host\n";
  5.     $sshcmd = "ssh";
  6.     $get = `$sshcmd $host uptime`;
  7.     print "$get\n";
  8. }
Feb 18 '10 #6

P: 4
Oh man, can't believe I missed that. Thanks for your help Ron, it working fine now.
Feb 18 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.