Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7088525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:48:42+00:00 2026-05-28T07:48:42+00:00

I have this Perl code that is only printing the first line instead of

  • 0

I have this Perl code that is only printing the first line instead of all the lines.

use Net::SSH::Perl;
my $user = "user"; 
my $cmd = "df -m | grep data";
my $host = "host1.example.com"; 

my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user);
my ($dflines,$errors,$exit) = $ssh->cmd($cmd);
foreach $line ($dflines) {
  print "$line";
  my @values = split(' ',$line);
  my ($MBsize, $MBused, $MBavail, $dir) =
      ($values[1], $values[2], $values[3], $values[5]);
  print "MBsize=$MBsize MBused=$MBused MBavail=$MBavail dir=$dir\n";
}

It prints:

/dev/sdb1              1407118    931813    403828  70% /data1
/dev/sdc1              1407118    921739    413902  70% /data2
/dev/sdd1              1407118    909408    426233  69% /data3
/dev/sde1              1407118    918828    416813  69% /data4
/dev/sdf1              1407118    922335    413306  70% /data5
MBsize=1407118 MBused=931813 MBavail=403828 dir=/data1

I would expect:

/dev/sdb1              1407118    931813    403828  70% /data1
/dev/sdc1              1407118    921739    413902  70% /data2
/dev/sdd1              1407118    909408    426233  69% /data3
/dev/sde1              1407118    918828    416813  69% /data4
/dev/sdf1              1407118    922335    413306  70% /data5
MBsize=1407118 MBused=931813 MBavail=403828 dir=/data1
MBsize=1407118 MBused=921739 MBavail=413902 dir=/data2
MBsize=1407118 MBused=909408 MBavail=426233 dir=/data3
MBsize=1407118 MBused=918828 MBavail=416813 dir=/data4
MBsize=1407118 MBused=922335 MBavail=413306 dir=/data5

I’m almost certain it’s something basic.
Any help is appreciated.
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:48:42+00:00Added an answer on May 28, 2026 at 7:48 am

    The problem is this line:

    foreach $line ($dflines) {
    

    You are only executing one iteration, since that is not an array, but a scalar. When you print "$line" it actually prints all of the lines that you captured, but I guess it looks like you printed many values in a loop. In the subsequent part:

    my @values = split(' ',$line);
    my ($MBsize, $MBused, $MBavail, $dir) = 
            ($values[1], $values[2], $values[3], $values[5]);
    print "MBsize=$MBsize MBused=$MBused MBavail=$MBavail dir=$dir\n";
    

    You only use the first few values of the split, but the rest of that line is in there as well. In other words, @values contains all of the values you expect. Range 0..5 contains the first row, 6..10 the next, and so on. Since you only use the first 6 values, you don’t see them.

    A quick fix might be to do:

    foreach $line (split /\n/, $dflines) {
    

    Which would break up your input the way you were expecting it to be.

    Some tips:

    Always use warnings; use strict;

    And you should make use of some proper perl features:

    for my $line (split /\n/, $dflines) {
       print $line;
       my @values = split ' ', $line;
       printf "MBsize=%s MBused=%s MBavail=%s dir=%s\n", @values[1,2,3,5];
    }
    

    It looks like you want to print the MBsize ... lines after the regular output. If so, you can just store the lines in an array and print after the loop:

    my @print;
    for my $line (split /\n/, $dflines) {
       print $line;
       my @values = split ' ', $line;
       push @print, sprintf "MBsize=%s MBused=%s MBavail=%s dir=%s\n", @values[1,2,3,5];
    }   # note  ----^ sprintf instead
    print @print;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Perl code that translates new-lines and line-feeds to a normalized form.
I have this legacy code base (Compaq PERL) , about 1500 lines of code,
So I have some perl code that goes something like: use strict; use XML::XPath;
Hi guys i have this line of code in a perl script where i
I have some perl code that looks something like this: my @array = map
I have 2 sub s in my perl code that are all but identical.
I have this code: $coder = JSON::XS->new->utf8->pretty->allow_nonref; %perl = $coder->decode ($json); When I write
Let us say if I have a Perl module Resounces.pm with this code snippet
I have a Perl script, that's supposed to match this string: Sometimes, he says
I have this code in jQuery, that I want to reimplement with the prototype

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.