Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

defining part of a string (YYMMDD date format) with shell script

Status
Not open for further replies.

starlite79

Technical User
Aug 15, 2008
89
US
Hi,

I am writing a kshell script to wget a bunch of files that have a nameYYMMDDname.data format. I attempted to define a string for the YYMMDD part as follows using strftime

Code:
# Define a string for the YYMMDD part of the filename.

s=strftime('%y%m%d')
Next I defined the filename as such:

Code:
file="name"$s"data.data"
But the script fails at the strftime part (unexpected '( 'error). Do I need to include C library(ies) somehow? Is there another way to define this? I may just have to say wget *.data, but I was hoping for a more elegant approach.
 
What about this (in ksh) ?
s=$(date +%y%m%d)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi,

That does work for today's date. I have a range of dates though. For example, one of the directories starts at 080516 and ends at yesterday's date. The other directory I want to wget files from starts at 080101 and ends at today's date. I have not found a way to define a range of dates. Any suggestions?
 
I made a little progress.

Now my code looks like this:

Code:
date1=$(date +%y%m%d --date='May 16, 2008')
date2=$(date +%y%m%d --date='1 day ago')

n1=$date1
n2=$date2

for ((s = n1; s <= n2; s++))

filename="string"$s"h00.anotherstring"

However, my for construct does not work. Any help to get the string s to be defined as the range 080516 to yesterday's yymmdd format?
 
Something like this perhaps:

Code:
date1=$(date +%m/%d/%Y --date='May 16, 2008')
date2=$(date +%m/%d/%Y --date='1 day ago')

while [[ "$date1" != "$date2" ]]
do
        dates="$dates $(date +%y%m%d --date $date1)"
        date1=$(date +%m/%d/%Y --date "$date1 + 1 day")
done

for s in $dates
do
        filename="string${s}h00.anotherstring"
        echo $filename
done

Unfortunately the --date option only seems to understand the illogical mm/dd/yyyy date format.

Annihilannic.
 
if you are using a ksh93 (not ksh88 or pdksh!) you can use the printf "%T" option to outpu a range sof dates and date formats. i.e.
Code:
$ printf "%(%Y-%m-%d)T\n" now
2008-09-22
$ printf "%(%Y-%m-%d)T\n" yesterday
2008-09-21
$ printf "%(%Y-%m-%d)T\n" "last monday"
2008-09-15
$ printf "%(%Y-%m-%d)T\n" "first monday may 2008"
2008-05-05
$
 
Hi,

Thanks for the help. The logic seems to work, but something is not quite right with my wget command.

First, I changed the order of the dates and date1 variables in the while loop:

Code:
while [[ "$date1" != "$date2" ]]
do
#   dates="$dates $(date +%y%m%d --date $date1)"
   date1=$(date +%m/%d/%Y --date "$date1 + 1 day")
   dates="$dates $(date +%y%m%d --date $date1)"
done

The echoing of the filenames in the for loop works fine.
But when I use wget, it only downloads the last day's file.

I am using wget this way:

Code:
${EXECPATH}wget -N --tries=10 [URL unfurl="true"]http://path/to/url/$filename[/URL]

Do I need filename in braces like this: ${filename}?
 
LOL... I didn't have the wget in the for loop. Once I did that, it worked as expected.

 
Anyone know an equivalent command to
Code:
date1=$(date +%m/%d/%Y --date='May 16, 2008')
for HP UX? HP UX doesn't understand the --date option and just spits out today's formatted date.

And yes, I have a good reason for wanting the script to also work on HP UX (At least for a while).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top