BASH - find regex in array, print found array items -
i have tested regex here: http://regexr.com/3bchs
i cant array print regex search terms.
files=(`ls $backupdir`) daterange='(2015\-06\-)[0-9]+\s?' in "${files[@]}" if [[ "$files[$i]" =~ $daterange ]]; echo $i fi done input: 2015-06-06 2015-06-13 2015-06-20 2015-06-27 2015-07-04 2015-07-11
output:
2015-06-06 2015-06-13 2015-06-20 2015-06-27 2015-07-04 2015-07-11
by running bash -vx <script> found out files compering wrong. needed change $files[$i] $i.
$files[$i] = 2015-06-06[2015-06-06]
i have further improved answer etan reisner comment. not parsing output ls.
reference: https://stackoverflow.com/a/11584156/3371795
#!/bin/bash # enable special handling prevent expansion # literal '/example/backups/*' when no matches found. shopt -s nullglob # variables year=`date +%y` month=`date +%m` dir=(/home/user/backups/backup.weekly/*) # regex - curent month date_range='('$year'\-'$month'\-)[0-9]+\s?' # loop through dir in "${dir[@]}"; # compare dir name date range if [[ "$i" =~ $date_range ]]; # have no idea is, works fine without it. [[ -d "$i" ]] # echo found dirs echo "$i" fi done
Comments
Post a Comment