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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.