LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   linux command inside perl script (http://www.linuxquestions.org/questions/programming-9/linux-command-inside-perl-script-753437/)

von_vea 09-07-2009 08:10 PM

linux command inside perl script
 
Hi All,

I have a perl script named if3.pl pls. see below contents:

------------------------------------------------------
#!/usr/bin/perl -w

$today = `date | awk '{print \$1}'`;

if (($today eq 'Sat') or ($today eq 'Tue'))
{
$do = "Its saturday or tuesday";
}
else
{
$do = "Work!";
}

print "$do\n";
print "$today\n";
------------------------------------------------------

When I ran that script, these are the result.

------------------------------------------------------
$ perl if3.pl
Work!
Tue

------------------------------------------------------

I am expecting "Its saturday or tuesday" since the $today variable has "Tue".

But I have a hint that $today is not only having a value of "Tue" but as well as the extra line after Tue.

------------------------------------------------------
$ perl if3.pl
Work!
Tue
<THIS IS THE EXTRA LINE>
------------------------------------------------------

Any idea how to remote the extra line from $today variable ?


Many Thanks in advance!
- Von

gregorian 09-07-2009 08:30 PM

chomp($today=`date | awk '{print \$1}'`);

Sergei Steshenko 09-07-2009 11:15 PM

Why would one call 'awk' from Perl in the first place ?

PMP 09-08-2009 05:39 AM

agree with Sergei, you could have user
`date +%a`

estabroo 09-08-2009 07:25 AM

or even kept it in perl

$today = (split(" ", localtime()))[0];

Sergei Steshenko 09-08-2009 01:54 PM

If we are talking about Perl and date/time manipulation, a good place to start from is: http://datetime.perl.org/?Modules .


All times are GMT -5. The time now is 06:43 AM.