I need to capture the output of a bash command in perl which I've done a million times but for some reason this specific issue is hanging me up.

What I need to do it capture the output from a git fetch --tags command and parse the results. The issue is if I run the following code

#!/usr/bin/perl use warnings; use strict; my $git = system('git fetch --tags'); print "What's here? --- $git\n";
The output of the command is displayed in terminal (which is to be expected since I haven't redirected it). However, if I run the command as followed:
my $git = system('git fetch --tags &> /dev/null');
The variable is empty (which is also expected because I am redirecting it to /dev/null). The only way I can get it to work is if I redirect to a file first as such:
git fetch --tags &> /tmp/git.txt
and then parsing the output file. I am trying to avoid having to do that if at all possible meaning I do not want an extra step of checking a tmp file, would much rather do it in one step. Help would be greatly appreciated. Thanks

********** UPDATE **********

I managed to solve this issue based on some of your suggestions using Capture::Tiny. The following code is doing exactly what I wanted it to do.

#!/usr/bin/perl use warnings; use strict; use Capture::Tiny ':all'; use Version::Compare; use Sort::Key::Natural qw( natsort ); my $GIT_HOME = '/usr/local/src/Arelle_Web/DQC_Test/'; my (@git) = capture{system("git --git-dir=${GIT_HOME}.git fetch --tags +");}; @git = grep { $_ ne '' } @git; my @tags; if(scalar(@git) > 1){ print "Tags have been updated\n"; my $string = join("\n", @git); @tags = ($string =~ m/\[new tag\]\s+(.+?)\s/g); } else { @tags = split("\n",capture{system("git --git-dir=${GIT_HOME}.git t +ag")}); print "No New Tags\n"; } @tags = natsort @tags; chomp(my $running_version = capture{system("git --git-dir=${GIT_HOME}. +git describe --tag");}); $running_version =~ s/^v//; my ($ver_major) = $running_version =~ /^(\d)/; my @version = grep(/^v$ver_major.+/,@tags); if(@version){ @version = natsort @version; my $newest = pop(@version); $newest =~ s/^v//; if(&Version::Compare::version_compare($newest,$running_version) == + 1){ print "v$newest is greater than the running version v$running_ +version\n"; } }

Thanks again for all your suggestions.


In reply to Capturing bash output without displaying STOUT in terminal by edimusrex

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.