BASH and wildcard expansion

Bill Oliver vendor at billoblog.com
Tue Aug 20 00:56:03 UTC 2013


On Tue, 20 Aug 2013, Suvayu Ali wrote:

> On Mon, Aug 19, 2013 at 05:18:40PM -0500, Bill Oliver wrote:
>>
>> # Filename format YYYY_MM_DD--HHMM.zip
>> filename="$log_year-$log_month-$log_day--*.zip"
>>
>> filelist=`ls $filename`
>
> Please don't do this.  A much better solution is:
>
> filelist=($log_year-$log_month-$log_day--*.zip)
>
> See: <http://mywiki.wooledge.org/ParsingLs>
>
>

Thanks for the comment -- I hadn't thought of that.  However, it doesn't
work and it doesn't make sense to me.  I thought that putting things in
parentheses simply put it in a shell.

In any case, here's my test case.  I created two files:

2013-August-19--a.zip 
and
2013-August-19--ba.zip

Then, using this script:

******************
#!/bin/sh
# Date variables
log_year=`date "+%Y"`
log_month=`date "+%B"`
log_day=`date "+%d"`

# Filename format YYYY_MM_DD--HHMM.zip
filename="$log_year-$log_month-$log_day--*.zip"

filelist=`ls $filename`

echo $filelist
******************


I get the following output:

$ !./j
./jnk.sh
2013-August-19--a.zip 2013-August-19--ba.zip

Using this script:

******************

#!/bin/sh
# Date variables
log_year=`date "+%Y"`
log_month=`date "+%B"`
log_day=`date "+%d"`

# Filename format YYYY_MM_DD--HHMM.zip
filelist=($log_year-$log_month-$log_day--*.zip)


******************

I get:

$ ./jnk2.sh
2013-August-19--a.zip

For some reason, the second file (013-August-19--ba.zip) is not returned...

did you mean

filelist=$(ls log_year-$log_month-$log_day--*.zip)

?  ... but that puts the evil "ls" back in.  The complaint against "ls"
is that it breaks on wacky file names.  That isn't an issue here.


billo


More information about the users mailing list