below is a snippet of code from a larger perl code:
my $uname = ( -e '/usr/bin/uname' ) ? '/usr/bin/uname' : '/bin/uname';
my $os = ( `$uname 2>/dev/null` );
when i run this code, it seems to be complaining about the backticks. is there any efficient way i can get rid of the backticks and still be able to run the command and assign its value to the variable $os???
i tried this:
my $uname = ( -e '/usr/bin/uname' ) ? '/usr/bin/uname' : '/bin/uname';
my $os = ( $( $uname 2>/dev/null) );
akshay@nio:/tmp$ cat p.pl
#!/usr/bin/perl
use warnings;
use strict;
foreach my $cmd (('/bin/uname -a','ls -ltr','pwd')){
open CMD,'-|',"$cmd" or die $@;
my $line;
print "\nCommand : $cmd \n";
while (defined($line=<CMD>)) {
print $line;
}
close CMD;
}
akshay@nio:/tmp$ perl p.pl
Command : /bin/uname -a
Linux Aix 3.8.0-29-generic #42~precise1-Ubuntu SMP Wed Aug 14 15:31:16 UTC 2013 i686 i686 i386 GNU/Linux
Command : ls -ltr
total 72
drwx------ 2 akshay akshay 4096 Jan 1 1970 orbit-akshay
drwx------ 2 root root 4096 Oct 25 20:38 pulse-PKdhtXMmr18n
drwx------ 2 root root 4096 Oct 25 20:38 pulse-2L9K88eMlGn7
-rw-rw-r-- 1 akshay akshay 0 Oct 25 20:39 unity_support_test.0
drwx------ 2 akshay akshay 4096 Oct 25 20:39 ssh-SRRdshaL1886
drwx------ 2 akshay akshay 4096 Oct 25 20:39 keyring-cfR6TC
drwx------ 2 akshay akshay 4096 Oct 25 20:39 pulse-ZUhHRVImGZzf
-rw------- 1 akshay akshay 0 Oct 25 20:40 tmpx9PyKR
-rw-r----- 1 akshay akshay 32 Oct 25 20:51 adb.log
drwxr--r-- 2 akshay akshay 4096 Oct 25 20:51 android-akshay
-rw-rw-r-- 1 akshay akshay 1838 Oct 25 20:51 filef8zK8X
drwxrwxrwx 2 akshay akshay 4096 Oct 25 20:52 swtlib-32
drwxr-xr-x 2 akshay akshay 4096 Oct 25 20:52 hsperfdata_akshay
-rw-rw-r-- 1 akshay akshay 314 Oct 25 21:16 cmp.awk~
-rw-rw-r-- 1 akshay akshay 314 Oct 25 21:16 cmp.awk
-rw-rw-r-- 1 akshay akshay 1125 Oct 25 21:16 p~
-rw-rw-r-- 1 akshay akshay 1156 Oct 25 21:16 p
-rw-rw-r-- 1 akshay akshay 127 Oct 25 21:28 f
-rw-rw-r-- 1 akshay akshay 232 Oct 25 22:21 p.pl~
-rw-rw-r-- 1 akshay akshay 234 Oct 25 22:21 p.pl
Command : pwd
/tmp
---------- Post updated at 11:39 PM ---------- Previous update was at 11:24 PM ----------
OR you can create small subroutine like this
#!/usr/bin/perl
use warnings;
use strict;
sub system_cmd{
my @return;
open CMD,'-|',"@_" or die $@;
my $line;
while (defined($line=<CMD>))
{
push(@return, $line);
}
close CMD;
return @return;
}
foreach my $cmd (('/bin/uname -a','ls -ltr','pwd'))
{
my @output =system_cmd($cmd);
print "\nCommand : $cmd \n";
print "@output";
}
It would be interesting to know what kind of backticks complains you get. The posted snippet should work exactly what you want.
You could remove the 2>/dev/null out of it since the backtick captures only the STDOUT by default and ignores the SDTERR
Also, you can remove the backticks if you use qx.
e.i
my $os = qx{$uname};
The most common ways of executing external commands:
There are some examples in that site and some more explanations.
A synopsis found in that link:
system(): you want to execute a command and don't want to capture its output
exec: you don't want to return to the calling perl script
backticks: you want to capture the output of the command
open: you want to pipe the command (as input or output) to your script