Bash shell 在 while 条件下编写多个测试脚本

Bash shell script multiple tests in while condition

首先,是的,我读过这个:

Bash scripting, multiple conditions in while loop

答案可能在 post 中,但我没有看到它,所以我希望有人能提供帮助。我想在我的 while 循环中测试两个条件,本质上我想结合这两个当前嵌套的测试,一个只是一个正则表达式,第二个是测试它是否实际上是一个有效日期:

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

我觉得我应该可以这样写:

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

但我无法让它工作,我不确定是我的逻辑有偏差,还是我尝试组合测试的方式(或两者!)...

你可以做到

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