Perl command modification


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl command modification
# 1  
Old 10-25-2014
Perl command modification

below is a snippet of code from a larger perl code:


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:

Code:
my $uname = ( -e '/usr/bin/uname' ) ? '/usr/bin/uname' : '/bin/uname';
my $os = ( $( $uname 2>/dev/null)  );

but it didn't work.


OS:
Linux RedHat
# 2  
Old 10-25-2014
Try like this

Code:
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;

}

Code:
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

Code:
#!/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";
}

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 10-25-2014
Use system. Here's an example.
Code:
[user@host ~]$ perl -e 'system("date")'
Sun, Oct 26, 2014  7:38:44 AM
[user@host ~]$

This User Gave Thanks to balajesuri For This Post:
# 4  
Old 10-25-2014
Quote:
Originally Posted by SkySmart
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???
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
Code:
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:
  1. system(): you want to execute a command and don't want to capture its output
  2. exec: you don't want to return to the calling perl script
  3. backticks: you want to capture the output of the command
  4. open: you want to pipe the command (as input or output) to your script
This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modification of perl script to split a large file into chunks of 5000 chracters

I have a perl script which splits a large file into chunks.The script is given below use strict; use warnings; open (FH, "<monolingual.txt") or die "Could not open source file. $!"; my $i = 0; while (1) { my $chunk; print "process part $i\n"; open(OUT, ">part$i.log") or die "Could... (4 Replies)
Discussion started by: gimley
4 Replies

2. Shell Programming and Scripting

Help with command modification

Hello folks; I'm using the following command to get the highest number of requests per second in a log file and it works well. grep "2017-02-22" "LogFile.log" | cut -c1-20 | uniq -c | sort -n | tail -n1 Now i would like to also get the smallest requests per second and the amount of time... (5 Replies)
Discussion started by: Katkota
5 Replies

3. Shell Programming and Scripting

Modification to awk command

i have a php file that has this: php.code #!/usr/bin/php <?php phpinfo(); hlight_file(__FILE__); ?> I want my awk code grab whatever is inbetween and including the "<?php" and "?>". Then, it should scan all the entries between these two points. And if the entries between these... (10 Replies)
Discussion started by: SkySmart
10 Replies

4. Programming

Excel sheet modification using perl module

Hi , can any one tell me,"How to extract the same format from existing excel file to new excel file " using Spreadsheet::WriteExcel or Spreadsheet::ParseExcel module ??? Example_pgm: Below program is used to read existing excel file..In this program "my $cell = $_;" line is used to... (0 Replies)
Discussion started by: kavi.mogu
0 Replies

5. Shell Programming and Scripting

Excel sheet modification using perl module

Hi , Is there any possibility to read excel sheet in column by column order ?...Thanks in advance,........ :confused: (1 Reply)
Discussion started by: kavi.mogu
1 Replies

6. Shell Programming and Scripting

Excel sheet modification using perl module

I need to insert new column to already existing file ..can any one help me..?? (6 Replies)
Discussion started by: kavi.mogu
6 Replies

7. Shell Programming and Scripting

Excel sheet modification using perl module

Is there any possibility to move the content from one cell to another cell (Excel sheet) using perl module? (3 Replies)
Discussion started by: kavi.mogu
3 Replies

8. Solaris

Command for checking modification history on file

What is the command for checking modification history on file? ---------- Post updated at 01:20 PM ---------- Previous update was at 12:35 PM ---------- Let me rephrase this. On a regular Unix file can I at least check to see the time and date history modification of the file? (6 Replies)
Discussion started by: jastanle84
6 Replies

9. Shell Programming and Scripting

Need help with a slight modification to my PERL script

Hi all, So I have a script that reads a file called FILEA.txt and in that file there are several columns. The ones that are most important are the $name $start and $stop. So currently the script takes values between the start and stop (inside) by using a program called fastamd. But what I... (4 Replies)
Discussion started by: phil_heath
4 Replies

10. UNIX for Dummies Questions & Answers

command for modification date of a file

Good morning, I would like to find all files of a certain type and display their name as well as their modification date. In order to do this, I would do the following: find ./ -name *.csv | ???????? My question: what to put after the pipe instead of the question marks? Is there a basic... (5 Replies)
Discussion started by: scampsd
5 Replies
Login or Register to Ask a Question