Escaping in the Bash @ Advanced Template (..)

I came across extglob extended template for bash. http://wiki.bash-hackers.org/syntax/pattern

I am studying this case:

Imagine the directory structure as follows:

/basedir/2017/02/26/uniquedirs1/files.log /basedir/2017/02/27/uniquedirs2/files.log /basedir/2017/02/28/uniquedirs3/files.log /basedir/2017/03/01/uniquedirs4/files.log /basedir/2017/03/02/uniquedirs5/files.log 

If I want to list the catalog files 2017/02/27 and 2017/02/28 , I can simply write:

 ls /basedir/2017/02/@("27"|"28")/*/files.log 

Awesome! \ O /

Now the question. How to define multiple directories in an extended bash template?
This does not work:

 ls /basedir/2017/@("02/28"|"03/01")/*/files.log ls /basedir/2017/@("02\/28"|"03\/01")/*/files.log 

Is there any way to define multiple directories for an "extended template"?

+5
source share
2 answers

Pathname generation applies only patterns to pathname components; they never match the / that separate the components. A parenthesis extension would be cleaner:

 ls /basedir/2017/{2/28,3/01}/*/files.log 

which will first expand to

 ls /basedir/2017/2/28/*/files.log /basedir/2017/3/01/*/files.log 

before generating the path name on the two resulting patterns.

+4
source

ok, the way I solved it dynamically is as follows:

 #!/bin/bash # User input: start_date="2017-02-27" day_range=2 # Logic: declare -a 'date_range=($( echo {'"$day_range"'..0} | xargs -I{} -d " " date -d "'"$start_date"' +{} day" --rfc-3339="date" ))' #this creates an array with dates: #2017-03-02 2017-03-01 2017-02-28 2017-02-27 eval ls "basedir/{$(IFS=\, ; echo "${date_range[*]//-//}")}/*/files.log" #this translates to: #ls basedir/{2017/03/01,2017/02/28,2017/02/27}/*/files.log 

This is more useful if you want to use it with tools like grep to retrieve data, if you cannot use file creation dates.

0
source

Source: https://habr.com/ru/post/1265295/


All Articles