?
Solved

Problems with backticks

Posted on 1999-12-27
5
Medium Priority
?
212 Views
Last Modified: 2010-03-05
Hi,

I have developed a perl script to dynamically build an html page from a supplied text file.

On my RedHat Linux box it works perfectly yet on my ISPs FreeBSD box I have a problem. All I want to do is return the day of the week for a given date (not always today's date)

On RedHat Linux:

$dw = "25/12/1999";
$DofW = `date --date "$dw" +%w`;

... works perfectly and $DofW contains 6 indicating a Saturday. (It took me a while to figure out I needed double quotes around $dw)

On my ISPs box, I find from the man page for date, that the above date command doesn't work the same. I need to call it like this:

$DofW = `date -v25d -v12m -v1999y \"+%u\"`;

The problems occur when I replace the 25, 12 and 1999 with variables.  

I /split/ the required date into @Date then assign......

$Day = $Date[0];
$Month = $Date[1];
$Year = $Date[2];

# I then try the following 4 lines.....

$TD = "date -v".$Day."d -v".$Month."m -v".$Year."y \"+%u\"";
$DofW1 = `$TD`;
$DofW2 = `"$TD"`;
$DofW3 = `date -v25d -v12m -v1999y \"+%u\"`;

# Printing out the 4 values into html I get:

$TD : date -v 25d -v12m -v1999y:
$DofW1 ::
$DofW2 ::
$DofW3 :6 :

(The colons ':' are mine to indicate boundaries)

So why does my manual $DofW3 work (Returning 6 for a Saturday) yet the other two don't?
0
Comment
Question by:Upland
  • 2
  • 2
5 Comments
 

Author Comment

by:Upland
ID: 2308259
Edited text of question.
0
 
LVL 85

Accepted Solution

by:
ozo earned 200 total points
ID: 2308404
#it looks like $Day had an extra space
use Time::Local;
$dw = "25/12/1999";
@Date=split"/",$dw;
$DofW = (gmtime timegm 0,0,0,$Date[0],$Date[1]-1,$Date[2]-1900)[6];
#should be portable to any operating system
0
 
LVL 5

Expert Comment

by:ecw
ID: 2308409
Maybe $Day has a leading space, it has in the output -
  $TD : date -v 25d -v12m -v1999y:
don't know which Unix you're using, my date(1) hasn't got -v option, so I can't test it.
0
 

Author Comment

by:Upland
ID: 2308471
Ozo,

Well it works perfectly - but even after checking the man page for gmtime I cannot say I fully understand it.... such as timegm??

The space in $Day which you picked up on was not relevent - I inserted one in my manual assigment and it still worked.

Many thanks... still curious about the backticks though!

Upland
0
 
LVL 85

Expert Comment

by:ozo
ID: 2308714
timegm is explained in
perldoc Time::Local

$DofW1 = `$TD`;
should have been the same as
$DofW3 = `date -v25d -v12m -v1999y \"+%u\"`;
although I don't know what happened to the "+%u" in $TD
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question