gpt4 book ai didi

linux - 检查 xCode 的安装总是返回 true

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:17 25 4
gpt4 key购买 nike

我正在尝试检查是否安装了 xCode。这:

function xCodeCheck(){
if xcode-select -p; then
return 1
else
return 0
fi
}
if xCodeCheck -eq 1; then
echo "it's here"
else
echo "it's not here"
fi

虽然总是返回 true。不管程序是否安装。如果不存在,如何返回 false?

最佳答案

您的代码的工作版本是这样的:

function xCodeCheck(){
if xcode-select -p; then
return 0
else
return 1
fi
}
if xCodeCheck; then
echo "it's here"
else
echo "it's not here"
fi

请注意,我反转了 0/1 返回值,因为 shell 中 0 的退出/返回状态是 true,其他所有内容都是 false

也就是说,整个包装功能毫无意义。

你可以很容易地写:

xCodeCheck() {
xcode-select -p
}

并让 xCodeCheck 直接返回 xcode-select 的返回值,而不是在 if 中捕获它并将其规范化为 01

也就是说,您可以在第一个测试中使用 xcode-select -p:

if xcode-select -p; then
echo "it's here"
else
echo "it's not here"
fi

如果你想保留手动返回和手动值检查你的原始代码需要写成:

function xCodeCheck(){
if xcode-select -p; then
return 1
else
return 0
fi
}

if xCodeCheck; [ $? -eq 1 ]; then
echo "it's here"
else
echo "it's not here"
fi

关于linux - 检查 xCode 的安装总是返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30087941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com