在bash脚本中使用if和boolean函数:if当函数返回true时,condition的计算结果为false

use*_*196 5 bash function

is_dir_empty(){

    for file in "$1"
    do
        if [ "$file" != "$1" ]; then
            return 0
        fi
    done
    echo "return 1"
    return 1
}

file="/home/tmp/*.sh"

if is_dir_empty "$file"; then
    echo "empty"
else echo "not empty"
fi

它输出

return 1
not empty

所以is_dir_empty返回1,但如果条件评估为假某事......为什么?

Mic*_*Day 9

因为shell脚本遵循Unix惯例,期望实用程序为成功返回零而对于失败则返回非零,因此布尔条件被反转.