0

I have tested the regex here: http://regexr.com/3bchs

I cant get the array to only print the regex search terms.

files=(`ls $BACKUPDIR`)
daterange='(2015\-06\-)[0-9]+\s?'

for i in "${files[@]}"
do
        if [[ "$files[$i]" =~ $daterange ]];
         then
                 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
3
  • 1
    You can try to execute your script with bash -vx <script> to see how the values are evaluated. This might shed some lights on what goes wrong. Commented Jul 13, 2015 at 11:34
  • You forgot the {} in $files[$i]. Try ${files[$i]} Commented Jul 13, 2015 at 11:44
  • @anubhava I have added the input Commented Jul 13, 2015 at 11:44

1 Answer 1

2

By running bash -vx <script> I found out that the files it was compering was wrong. I needed to change $files[$i] to $i.

$files[$i] = 2015-06-06[2015-06-06]

I have further improved my answer thanks to Etan Reisner comment. By not parsing output from ls.

Reference: https://stackoverflow.com/a/11584156/3371795

#!/bin/bash

# Enable special handling to prevent expansion to a
# literal '/example/backups/*' when no matches are found. 
shopt -s nullglob

        # Variables
        YEAR=`date +%Y`
        MONTH=`date +%m`

        DIR=(/home/user/backups/backup.weekly/*)

        # REGEX - Get curent month
        DATE_RANGE='('$YEAR'\-'$MONTH'\-)[0-9]+\s?'


# Loop through dir
for i in "${DIR[@]}";
do
        # Compare dir name with date range
        if [[ "$i" =~ $DATE_RANGE ]];
        then
                # I have no idea what this is, works fine without it.
                [[ -d "$i" ]] 

                # Echo found dirs
                echo "$i"
        fi
done
Sign up to request clarification or add additional context in comments.

5 Comments

You shouldn't be parsing the output from ls. Use a glob in that array instead (which may mean needing to strip paths in the loop probably but basename and shell parameter expansion can do that for you).
I have updated my answer thanks @EtanReisner. Does anyone know what this is " [[ -d "$i" ]] ", I am unable to find anything on google.
[[ is a bash-specific version of [/test with slightly different semantics. -d is "is a directory". Notice in the original it is [[ -d "$folder" ]] && echo "$folder" so that's "if $folder is a directory then echo $folder". On its own [[ -d "$folder" ]] just returns true or false and so in your code it does nothing.
Thanks bro, In my scenario I dont see the use for it here ?
Written as you have it it has, as I said, no purpose since it just returns true or false and doesn't affect or print anything. In the original with the && it causes the echo to occur only when it returns true (i.e. the folder is a directory).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.