【发布时间】:2015-05-06 21:31:25
【问题描述】:
我正在尝试检查 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返回?
【问题讨论】:
-
空格很重要
if语句它测试将=true分配到xCodeCheck变量是否成功(在此之后运行echo "$xCodeCheck"以了解我的意思)。 Shell 函数不能仅返回数字值的字符串。要测试字符串,您需要使用[/test。通过shellcheck.net 运行您的代码,直到它返回没有错误。 -
@EtanReisner 我纠正了几个错误,但仍然在第 8 行得到
SC2034 xCodeCheck appears unused. Verify it or export it.。我不知道那是什么意思。 -
这是分配问题。
if xCodeCheck==true; then是测试中的变量赋值。与if res=$(some-command-that-might-fail); then相同。所以 shellcheck 告诉你你没有在任何地方使用xCodeCheck变量,所以赋值突出为“奇数”。你只想在这里if xcode-select -p; then。你不需要这个包装函数。 -
@EtanReisner double equal 是 bash 中的赋值吗?
-
@EtanReisner 你能看到我最近的更新吗?这样更好吗?