# Perl script variable to read shell command

**URL:** <https://community.unix.com/t/perl-script-variable-to-read-shell-command/215609>\
**Category:** Shell Programming and Scripting\
**Created:** [October 8, 2008, 3:39am UTC](https://community.unix.com/t/perl-script-variable-to-read-shell-command/215609 "2008-10-08T03:39:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bulkbiz](https://community.unix.com/letter_avatar/bulkbiz/32/5_5575768a8748004e209b776fc1b2916d.png) [@bulkbiz](https://community.unix.com/u/bulkbiz)\
**Post date:** [October 8, 2008, 3:39am UTC](https://community.unix.com/t/perl-script-variable-to-read-shell-command/215609/1 "2008-10-08T03:39:18Z")

</div>

Solaris 10  
Korn shell ksh,

Hi there,

I have figured out to get yesterday's date which is using the below command:  
TZ=GMT+24; date +%d-%b-%Y to get the format of 30-Sep-2008 and  
TZ=GMT+24; date +%Y%m%d to get the format of 20080930.

I need this two format. In my perl script below I need the variable of $date and $folder to have the above format. Below method don't work. Please help.

--------------------------------------------------------------------------  
# FILENAME: collectLog-smtp.pl  
# Version : 1.o  
# Purpose : Collect log for the specify date  
# Changes : n/a  
#  
# Editable settings  
# Variable

my $date = `TZ=GMT+24 date +%d-%b-%Y`;  
my $folder = `TZ=GMT+24 date +%Y%m%d`;  
#my $date = "30-Sep-2008";  
#my $folder = "20080930";

# DO NOT modify the below settings  
my $row = "";  
my $input = "/jes/dist/sbas-poc/report/" . $folder . "/smtp/smtp.mail.tmp";  
my $output = "/jes/dist/sbas-poc/report/" . $folder . "/smtp/smtp.mail.log";

open(INFILE, "$input") or die "Cannot open input file";  
open(OUTFILE1, "\>$output") or die "Cannot open output file";

while (\<INFILE\>)  
{

# 14x argument  
# col[0] = date  
# col[1] = timestamp  
# col[2] = msg ID  
# col[3] = source channel  
# col[4] = either destination channel OR log status  
# col[5] = either log status OR mail size  
# col[6] = either mail size OR sender address  
# col[7] = either sender OR rfc;recipient address  
# col[8] = either rfc;recipient address OR recipient address

```
    $row=$_;
    @col = split /\\s\+/, $row;

    if\( $col[0] eq $date \)
    \{
            print OUTFILE1 "$row";
    \}

```

}  
close INFILE;  
close OUTFILE1;  
--------------------------------------------------------------------------

---

<div class="post-metadata">

**Author:** ![era](https://community.unix.com/letter_avatar/era/32/5_5575768a8748004e209b776fc1b2916d.png) [@era](https://community.unix.com/u/era)\
**Post date:** [October 8, 2008, 3:58am UTC](https://community.unix.com/t/perl-script-variable-to-read-shell-command/215609/2 "2008-10-08T03:58:29Z")

</div>

You need to chomp the output from backticks; it contains a trailing newline.

Beyond that, if it still doesn't work, it would be most helpful if you could say how it doesn't work.

(In the die message you should probably include the name of the file and the value of $!. -- this improves troubleshooting tremendously.)

---

<div class="post-metadata">

**Author:** ![KevinADC](https://community.unix.com/letter_avatar/kevinadc/32/5_5575768a8748004e209b776fc1b2916d.png) [@KevinADC](https://community.unix.com/u/KevinADC)\
**Post date:** [October 8, 2008, 3:08pm UTC](https://community.unix.com/t/perl-script-variable-to-read-shell-command/215609/3 "2008-10-08T15:08:45Z")

</div>

I totally agree with era. chomp() the scalars that store the output from the backticks and add the filename and $! to the die message. chomp() should resolve the problem though.

---

<div class="post-metadata">

**Author:** ![SFNYC](https://community.unix.com/letter_avatar/sfnyc/32/5_5575768a8748004e209b776fc1b2916d.png) [@SFNYC](https://community.unix.com/u/SFNYC)\
**Post date:** [October 8, 2008, 4:34pm UTC](https://community.unix.com/t/perl-script-variable-to-read-shell-command/215609/4 "2008-10-08T16:34:25Z")

</div>

Why do you need to call the date command to get a date?

```plaintext
my $date = `TZ=GMT+24 date +%d-%b-%Y`;
my $folder = `TZ=GMT+24 date +%Y%m%d`;

```

Since you are using Perl, use Perl's functions to get the date you want:

```plaintext
use POSIX qw(strftime);
my $date = strftime("%d-%b-%Y", localtime());
my $folder = strftime("%Y%m%d", localtime());

```

For getting yesterday's date you can follow these simple instructions:  
[perlfaq4](http://faq.perl.org/perlfaq4.html#How_do_I_find_yester)

---

<div class="post-metadata">

**Author:** ![bulkbiz](https://community.unix.com/letter_avatar/bulkbiz/32/5_5575768a8748004e209b776fc1b2916d.png) [@bulkbiz](https://community.unix.com/u/bulkbiz)\
**Post date:** [October 10, 2008, 2:05am UTC](https://community.unix.com/t/perl-script-variable-to-read-shell-command/215609/5 "2008-10-10T02:05:21Z")

</div>

Thanks for the reply, I am noob in programming actually.

I saw this code from web:  
Output is day.month.year (e.g. 31.12.2006):

perl -le '($D,$M,$Y)=(localtime(time-86400))[3,4,5];printf("%.2d.%.2d.%.4d",$D,++$M,$Y+=1900)'

May I know how can I apply to my code?(Is my colleague's code, I am learning to modify it. I need to assign the $date and $folder into yesterday's format)

Please help
