Pattern matching notation

Hello

I have two kinds of logs like

server.log
server.log.2013-07-27.001

i want to create a variable which look like this (with a pipe)

log_name=server.(log|log.$YYYY-MM-DD.[0-9][0-9][0-9])

But i tried many cases but it didn't work.
Is it possible ? If yes, can you help me.

Have you tried putting it in double quotes...

log_name="server.(log|log.$YYYY-MM-DD.[0-9][0-9][0-9])"

What are you planning to do with this variable?
What is the value of the variable YYYY when you try setting log_name?

If you have a variable YYYY that is set to the current year, don't you also need $MM and $DD so they can expand to the current month and day, respectively?

Do you want $YYYY to be expanded when log_name is set, or do you want $log_name to contain the literal string $YYYY ?

If you want $YYYY (and any other variables you may define) to be expanded when you set log_name, use:

log_name="server.(log|log.$YYYY-MM-DD.[0-9][0-9][0-9])"

as shamrock suggested.

If you want the literal string $YYYY use:

log_name='server.(log|log.$YYYY-MM-DD.[0-9][0-9][0-9])'

Thank you for your help.
I'm going to explain my aim

In my directory, it may be that there is this

ll
-rw-r--r--  1 xxxx xxxxx  4909630 Jul 30 15:09 server.log
-rw-r--r--  1 xxxx xxxxx  7753380 Jul 28 23:59 server.log.2013-07-28.001
-rw-r--r--  1 xxxx xxxxx  7445243 Jul 29 23:59 server.log.2013-07-29.001
-rw-r--r--  1 xxxx xxxxx 10493980 Jul 29 10:21 server.log.2013-07-29.002
-rw-r--r--  1 xxxx xxxxx 10491771 Jul 29 12:43 server.log.2013-07-29.003
-rw-r--r--  1 xxxx xxxxx 10489253 Jul 29 14:40 server.log.2013-07-29.004
-rw-r--r--  1 xxxx xxxxx 10491934 Jul 29 17:11 server.log.2013-07-29.005
-rw-r--r--  1 xxxx xxxxx 10491424 Jul 30 10:09 server.log.2013-07-30.002
-rw-r--r--  1 xxxx xxxxx 10491929 Jul 30 12:05 server.log.2013-07-30.003
-rw-r--r--  1 xxxx xxxxx 10489096 Jul 30 14:15 server.log.2013-07-30.004
 server.log.2013-07-30.004

i create this variables

AAAA_MM_JJ=2013-07-29
log_name="server.(log|log.$YYYY-MM-DD.[0-9][0-9][0-9])"

if i launch

ll $log_name

I would like to obtain :

-rw-r--r--  1 xxxx xxxxx  4909630 Jul 30 15:09 server.log
-rw-r--r--  1 xxxx xxxxx  7445243 Jul 29 23:59 server.log.2013-07-29.001
-rw-r--r--  1 xxxx xxxxx 10493980 Jul 29 10:21 server.log.2013-07-29.002
-rw-r--r--  1 xxxx xxxxx 10491771 Jul 29 12:43 server.log.2013-07-29.003
-rw-r--r--  1 xxxx xxxxx 10489253 Jul 29 14:40 server.log.2013-07-29.004
-rw-r--r--  1 xxxx xxxxx 10491934 Jul 29 17:11 server.log.2013-07-29.005

But it didn't work, i obtain that.

ll $log_name
/bin/ls: server.(log|log.2013-07-29.[0-9][0-9][0-9]): No such file or directory

So you want:

log_name="server.log server.log.$AAAA_MM_JJ.[0-9][0-9][0-9]"

in your script sometime after you have set the variable AAAA_MM_JJ.

1 Like

Thanks a lot :slight_smile: it works with a space.

ll $log_name

I' obtain now :

-rw-r--r--  1 xxxx xxxxx  4909630 Jul 30 15:09 server.log
-rw-r--r--  1 xxxx xxxxx  7445243 Jul 29 23:59 server.log.2013-07-29.001
-rw-r--r--  1 xxxx xxxxx 10493980 Jul 29 10:21 server.log.2013-07-29.002
-rw-r--r--  1 xxxx xxxxx 10491771 Jul 29 12:43 server.log.2013-07-29.003
-rw-r--r--  1 xxxx xxxxx 10489253 Jul 29 14:40 server.log.2013-07-29.004
-rw-r--r--  1 xxxx xxxxx 10491934 Jul 29 17:11 server.log.2013-07-29.005

Do you know why doesn't it work with the command FIND ?

find . -type f -iname "$log_name" 2> /dev/null

Of course I know why. And I think you do, too.
Note that the command you used with ll wasl

ll $log_name

(with no quotes). So ls was given two operands: server.log and server.log.2013-07-29.[0-9][0-9][0-9] .

With find, you added the quotes because a -iname primary takes a single pattern argument; not two. But, by putting quotes around the two patterns, you created a single pattern that will only match filenames that start with server.log server.log.2013-07-29. and end with three decimal digits (and you don't have any files in this directory with names starting with server.log followed by a space).

For this application you need a different approach:

log_name1="server.log"
log_name2="$log_name1.$$AAAA_MM_JJ.[0-9][0-9][0-9]"
find . -type f \( -iname "$log_name1" -o -iname "$log_name2" \) 2> /dev/null