If/Else 语句的 Shell 比较超过 2 个值?

If/Else Statement's Shell Comparing more than 2 Values?

在 shell 中对 if/else 语句重新评分的快速问题,现在我正在尝试测试两个不同的值是否等于第三个

echo "CompareDateValues"
if [ "${TodaysDate}" = "${prevDate}" & "${currDate}" ]; then
    echo "Dates Are A Match : TodaysDate:${TodaysDate} = savedStateRunDates:${prevDate}"
else
    echo "Dates Are Not A Match : TodaysDate:${TodaysDate} = savedStateRunDates:${prevDate}"
    echo Exit
    exit 1
fi

正如您从上面的代码中看到的那样,我正在尝试测试 prevdate 和 currdate 是否与 Todaysdate 匹配,但我似乎可以让它正常工作任何帮助都会很棒

if test "$TodaysDate" = "$prevDate" && test "$TodaysDate" = "$currDate"; then ...

你必须使用两个条件:

if [ "$TodaysDate" = "$prevDate" ] && [ "$TodaysDate" = "$currDate" ] ; then

-a 运算符(不推荐)

if [ "$TodaysDate" = "$prevDate" -a "$TodaysDate" = "$currDate" ] ; then

或者,如果您处于 bash,切换到 [[ ... ]] 条件:

if [[ $TodaysDate = $prevDate && $TodaysDate = $currDate ]] ; then