0

Firstly, yes I have read this:

Bash scripting, multiple conditions in while loop

The answer is probably in that post but I'm failing to see it so I hope someone can help. I want to test for two conditions in my while loop, essentially I want to combine these two tests which are currently nested, one's just a regex, and the second is to test if it is actually a valid date:

while ! [[ "$searchDate" =~ ^[0-9]{1,2}[a-zA-Z]{3}[0-9]{4}$ ]]; do
    read -p "Enter date in format DDmmmYYYY: " searchDate

    while ! (date -d $searchDate); do
        read -p "That date doesn't appear to be valid, try again: " searchDate
    done
done

I feel like I should be able to write something like:

while [[ ! "$searchDate" =~ ^[0-9]{1,2}[a-zA-Z]{3}[0-9]{4}$ ]] || ( ! date -d $searchDate); do
    read -p "There's something funky about that date, try again: " searchDate

but I can't get it to work, I'm not sure if it's my logic that's skewiff, or the way I'm trying to combine the tests (or both!)...

Community
  • 1
  • 1
Guy Goodrick
  • 53
  • 1
  • 7
  • `!` can only use used immediately after `if` or `while` you can't put it into a subshell by itself. – Barmar Apr 08 '15 at 17:32
  • 2
    @Barmar you can put `!` in front of any (single or multistage) pipeline, including in front of a simple command in a subshell as shown here. It's not specific to `if` or `while` in any way. – that other guy Apr 08 '15 at 17:38
  • That code in your comment doesn't assign to `searchDate`. – Etan Reisner Apr 08 '15 at 17:46

1 Answers1

2

You can do

while [[ ! "$searchDate" =~ ^[0-9]{1,2}[a-zA-Z]{3}[0-9]{4}$ ]] || ! date -d $searchDate; do
    read -p "Enter date in format DDmmmYYYY: " searchDate
done
anubhava
  • 761,203
  • 64
  • 569
  • 643