How about like this:
start=$start_year$(printf %02d $start_month)
end=$end_year$(printf %02d $end_month)
ls *.log | sed -ne "/$start/,/$end/p"
Question
My script has to list all files between a given range of date within a file name (ex: 20130133.212001.log). The start date and end date will be given by user. I now have start_year, end_year, start_month, end_month....so on till end_second. My plan was to use ls syntax:
ls *{start_year..end_year}{start_month..end_month}{.....}.log
But the problem is if month or date has 01 bash is taking it as 1. Indeed my bash version is older than 4. I can't considering updating the bash version. Any other method that I use to list files in such range.
[EDIT]
$> cat tmp.sh
#! /bin/bash
start_year=2013
end_year=2013
start_month=01
end_month=01
start_date=01
end_date=12
start_month=$(printf %02d $start_month)
end_month=$(printf %02d $end_month)
start_date=$(printf %02d $start_date)
end_date=$(printf %02d $end_date)
ls {$start_year..$end_year}{$start_month..$end_month}{$start_date..$end_date}.log
$>
but output is:
$>./tmp.sh
ls: {2013..2013}{01..01}{01..12}.log: No such file or directory
while the current dir contains:
$> ls
20130101.log 20130103.log 20130105.log 20130107.log 20130109.log 20130111.log 20130201.log abcd.log
20130102.log 20130104.log 20130106.log 20130108.log 20130110.log 20130112.log 2013.log tmp.sh
$>
Solution
How about like this:
start=$start_year$(printf %02d $start_month)
end=$end_year$(printf %02d $end_month)
ls *.log | sed -ne "/$start/,/$end/p"
OTHER TIPS
You can utilize printf
facility in that case.
Consider this script:
for i in {1..2}; do for j in {01..12}; do printf "%02d%02d\n" $i $j; done; done
**UPDATE: For using variable:
start=1
end=2
for ((i=start; i<=end; i++)); do
for ((j=1; j<=12; j++)); do printf "%02d%02d\n" $i $j; done
done
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212