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 7678429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:38:45+00:00 2026-05-31T17:38:45+00:00

In the code below I am trying to clone a git repository to another

  • 0

In the code below I am trying to clone a git repository to another machine using a Perl script.

If I print $output, I am getting all the login messages i.e, the contents of /etc/motd/, but not the actual command output.

How to resolve this issue? Am I doing something wrong in the execution of the ssh command?

    sub myexec_remote    
    {
       my($cmd, $hostname, $filename) = @_;

       my $stdout = $stderr = $exit= "";
       my ($output) = `ssh $hostname $cmd 2>&1`;
       ## this is the command i.e, executed
       ##command is  ssh 111.22.11.32 "git clone --bare gt@l.com:/nfs/git/ /nfs/new123/"
       $exit = $?;
       if (defined $output)
       {
       open(MYOUTFILE, ">$filename");
       print MYOUTFILE "$output";
       close(MYOUTFILE);
       }

    }

I am using the backticks because sometimes ssh is done without passwordless and I have seen that the NET:SSH module doesn’t support it…

  • 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-31T17:38:46+00:00Added an answer on May 31, 2026 at 5:38 pm

    It is because you use the backticks operator in a list context:

    my ($output) = `ssh $hostname $cmd 2>&1`;
    

    When called in list context, backticks will return a list of lines. You only capture the first line, and put it in $output. The other lines are ignored. From perldoc perlop:

    In scalar context, it comes back as a single (potentially multi-line)
    string, or undef if the command failed. In list context, returns a
    list of lines (however you’ve defined lines with $/ or
    $INPUT_RECORD_SEPARATOR), or an empty list if the command failed.

    Solution:

    1. my $output = `ssh $hostname $cmd 2>&1`;
    

    Calling backticks in scalar context will cause $output to be a multiline string.

    2. my @output = `ssh $hostname $cmd 2>&1`;
    

    You can call backticks fine in array context, but you’ll have to print the entire array to the MYOUTPUTFILE later on

    Other comments:

    These are not part of the solution. The solution itself should be enough to fix your bug. However, they can decrease the risk of bugs, and increase your Perl knowledge.

    1. Try using the three-argument version of open, instead of your two-argument with a global, and catch the error when the file could not be opened:

      open(my $handle, '>', $filename) or die "Unable to open file, $!";

    2. Many people decide to use IPC::System::Simple instead of backticks, because you have more information when something goes wrong and you can avoid the shell.

    3. Another option is IPC::Open3, which even lets you capture STDOUT, STDERR and the exitcode easily. It is a bit harder to use though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Afternoon all, Using the code below I'm trying to load what is render by
I am trying to generate images using ImageMagick, please check the code below: <?
I have some code that performs a deep copy using Object.clone, but I'm trying
I am trying to insert values into Oracle 10g database using the code below
I am getting a syntax error for the code below. I am trying to
I am trying to run the timer in random interval by using code below.
In the code below i`m trying to read a list of selected projects and
In the code below I'm trying to load some images and put them in
The code below is what I am trying to use in order to get
I'm trying to use the code below to send messages via System.Net.Mail and am

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.