Perl System command calls to variable | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Perl System command calls to variable

Shell Programming and Scripting


Tags
system call

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-10-2008
4scriptmoni 4scriptmoni is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 19 January 2009, 9:12 AM EST
Location: Spain
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Perl System command calls to variable

I am new to scripting in Perl so I have a dumb question.
I know I can call system commands using

system("date");

But I am not able to:
1. set its output to a variable
2. run in quiet mode(no output to the screen)

The examples i have


Code:
#!/usr/bin/perl
print `date +\%y\%m\%d.\%H\%M`;   # OK
x=`date +\%y\%m\%d.\%H\%M`;     #ERROR
print x;

I get something like
Can't modify constant item in scalar assignment
Sponsored Links
    #2  
Old 11-10-2008
sysgate's Avatar
sysgate sysgate is offline Forum Advisor  
Unix based
 
Join Date: Nov 2006
Last Activity: 20 November 2013, 8:29 AM EST
Location: Bulgaria
Posts: 1,419
Thanks: 0
Thanked 6 Times in 6 Posts
Well, you should specify that you want to print scalar, you're missing the " $ ", the following example works OK :


Code:
#!/usr/bin/perl

print `date +\%y\%m\%d.\%H\%M`;
print "\nthis is separator print statement\n";

$x = `date +\%y\%m\%d.\%H\%M`;
print "$x\n";
print "\nthis is the second separator print statement\n\n";

If you want to capture system command's output, you should not use system(), as it only returns the status of wait(). Use backticks instead as shown above.
Sponsored Links
    #3  
Old 11-10-2008
SFNYC SFNYC is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 12 February 2014, 10:30 AM EST
Location: New York City
Posts: 115
Thanks: 0
Thanked 2 Times in 2 Posts
To capture output:


Code:
$output = `program args`;   # collect output into one multiline string
@output = `program args`;   # collect output into array, one line per element

But since you just want a date, it would be more efficient to have Perl do it than to fork off a new process.


Code:
$ cat ./date_test.pl
#!/usr/bin/perl

use strict;
use warnings;
use POSIX qw(strftime);

my $x;

$x = strftime("%y%m%d.%H%M", localtime());

print "x = $x\n";

exit 0;

$ ./date_test.pl
x = 081110.1019

    #4  
Old 11-10-2008
4scriptmoni 4scriptmoni is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 19 January 2009, 9:12 AM EST
Location: Spain
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
works

thanks it worked.
Now i have little problem with output.
I performa backup but tar always give me a output:
"tar: Removing leading `/' from member names"
I tried using &>/dev/null || exit 0 but no help...


Code:
sub backup()
{
    #Backup a folder and move it
    my @f = (localtime)[3..5]; # grabs day/month/year values
    my $datex = $f[1] +1 ."-". $f[0]; 
    my $filename = "bkp-scripts_".$datex.".tgz";
    #print "$filename\n";
    #  &> /dev/null" redirects both stdout and stderr to /dev/null. Dosent work!
    `tar zcf "/home/backup/$filename" /scripts &>/dev/null || exit 0`;
}

Sponsored Links
    #5  
Old 11-10-2008
SFNYC SFNYC is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 12 February 2014, 10:30 AM EST
Location: New York City
Posts: 115
Thanks: 0
Thanked 2 Times in 2 Posts
Try it this way:

Code:
`tar zcf "/home/backup/$filename" /scripts 2>/dev/null || exit 0`;

Sponsored Links
    #6  
Old 11-10-2008
4scriptmoni 4scriptmoni is offline
Registered User
 
Join Date: Jul 2008
Last Activity: 19 January 2009, 9:12 AM EST
Location: Spain
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
yes! thanks so much
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Command to trace System Calls on HP UX helper HP-UX 3 04-23-2012 09:56 AM
About system calls. vishwasrao UNIX for Dummies Questions & Answers 2 04-14-2009 07:52 AM
System calls ? TECHRAMESH Solaris 1 05-16-2006 01:05 AM
System calls? PlunderBunny UNIX for Dummies Questions & Answers 2 03-26-2006 08:12 PM
System Calls clickonline1 UNIX for Dummies Questions & Answers 2 10-06-2001 05:30 PM



All times are GMT -4. The time now is 09:08 AM.