为什么我的布尔测试在bash中切换?

cod*_*ann 2 linux bash shell ubuntu boolean-logic

我有这个bash功能,检查我是否在互联网上.它有助于我需要if internet-connected在bash脚本中进行快速测试的时刻.

由于它在上个月非常有用,我试图复制它的设计,有一个简单的ubuntu测试器,测试操作系统是否是Ubuntu.然后这发生了......

test.sh

internet-connected(){
    wget -q --spider http://google.com
    if [ $? -eq 1 ]
    then
        echo 'internet is connected'
        return 1
    else
        echo 'internet is not connected'
        return 0
    fi
}

echo "testing internet-connected"

if internet-connected
then
    echo 'connected'
else
    echo 'not connected'
fi

check-for-ubuntu(){
    tester=$(lsb_release -i | grep -e "Ubuntu" -c)
    if [ $tester -eq 1 ]
    then
        echo 'ubuntu detected'
        return 1
    else
        echo 'ubuntu not detected'
        return 0
    fi
}

echo ""
echo "testing check-for-ubuntu"

if check-for-ubuntu
then
    echo 'this is ubuntu'
else
    echo 'this is not ubuntu'
fi
Run Code Online (Sandbox Code Playgroud)

产量

testing internet-connected
internet is not connected
connected

testing check-for-ubuntu
ubuntu detected
this is not ubuntu
[Finished in 0.9s]
Run Code Online (Sandbox Code Playgroud)

我的问题

为什么逻辑在这两个函数中似乎是倒退的?

你们回答的确很好,谢谢.

ams*_*ams 5

Shell脚本不是C(或C++/Java/etc/etc.等).

  • 0表示成功(true).
  • 其他任何意味着错误(错误).

您的退货值是倒退的.