确定命令是否存在始终为 false
MacOS 将 zsh
作为默认 shell。
检查命令是否存在的代码:
if command -v omz
then
echo "exists";
else
echo "does not exists";
fi
运行它,它总是返回为不存在(使用安装/卸载进行测试)。
但是,如果我在 shell 中输入以下内容:
$ ./test.sh
does not exists
$ omz
[prints out help dialigue]
$ command -v omz
omz
我尝试了不同的测试方法,但总是相同的。
omz 随 Oh-my-zsh 一起提供,我用它来确定 Oh-my-zsh 是否安装在我的自动化脚本中。检查这个的正确方法是什么?
MacOS has zsh
as the default shell.
The code to check if a command exists:
if command -v omz
then
echo "exists";
else
echo "does not exists";
fi
Run it and it always comes back as does not exist (tested with installed/uninstalled).
However, if I type the following in a shell:
$ ./test.sh
does not exists
$ omz
[prints out help dialigue]
$ command -v omz
omz
I tried different ways of testing, always the same.
omz
ships with Oh-my-zsh and I'm using it to determine if Oh-my-zsh is installed in my automation scripts. What would be the correct way to check this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个 shell 函数。 Shell 函数不会导出。据我所知,在 ZSH 中不可能导出 shell 函数。
命令
失败,因为没有这样的命令。它并不总是假的。创建一个名为
omz
的可执行文件并将其放入您的$PATH
中 - 当然,command -v
将返回成功。或者,定义 omz 函数或获取提供 omz 定义的文件,然后再检查它是否存在。
你正在这样做。该函数在您的脚本中不存在,因此您已成功测试该函数不存在。测试工作。
如果你想专门测试它是否是一个函数,请使用 match
type
输出,例如case "$(type omz)" in *shell\ function*) echo it is; ;; *)回显它不是; ;; esac。
It's a shell function. Shell functions are not exported. As I read, it is not possible in ZSH to export a shell function.
command
fails, because there is no such command.It is not always false. Create an executable named
omz
and put it in your$PATH
- for sure,command -v
will return success then.Or alternatively, define
omz
function or source the file that providesomz
definition, before checking for it's existence.You are doing that. The function does not exist in your script, ergo you are successfully testing that the function does not exist. The test work.
If you want to specifically test if it is a function, use match
type
output, likecase "$(type omz)" in *shell\ function*) echo it is; ;; *) echo it is not; ;; esac
.这个怎么样?
zsh
的内置which
没有-s
(静默)选项,请使用/usr/bin/which
。how about this?
zsh
's build-inwhich
have not-s
(silent) option, use/usr/bin/which
.