How to compare today's date against an array of dates [closed]

I want to create an array of dates and compare it to today's date if it matches then execute test.sh file otherwise exit the loop in bash script. I have this so far:

#!/bin/bash
now=$(date +%Y-%m-%d)
array=['2016-03-02','2015-01-02']
for i in "${array[@]}"
do
    if [ $now -eq $i ]; then
        bash test.sh
    else
        echo "error"
    fi
done

please write the correct way to do this..


  • Use declareand braces to populate array. Elements are separated with space, not comma.
  • The operator -eq only works with integers, which your dates aren't, use string matching.

    #!/bin/bash
    now=`date +%Y-%m-%d`
    declare -a array=('date1' 'date2')
    for i in "${array[@]}"; do
      if [[ $now == $i ]]; then
            bash test.sh
      else
            echo "error"
      fi
    done