如何检查shell脚本中是否存在命令?
发布时间:2020-12-15 16:46:43 所属栏目:安全 来源:网络整理
导读:我正在写我的第一个shell脚本。在我的脚本中,我想检查某个命令是否存在,如果不存在,请安装可执行文件。如何检查此命令是否存在? if #check that foobar command doesnt existthen #now install foobarfi 一般来说,这取决于你的shell,但如果你使用bash
我正在写我的第一个shell脚本。在我的脚本中,我想检查某个命令是否存在,如果不存在,请安装可执行文件。如何检查此命令是否存在?
if #check that foobar command doesnt exist then #now install foobar fi
一般来说,这取决于你的shell,但如果你使用bash,zsh,ksh或sh(由dash提供),以下应该工作:
if ! type "$foobar_command_name" > /dev/null; then # install foobar here fi 对于一个真正的安装脚本,你可能想要确保类型不会成功返回的情况下,当有一个别名foobar。在bash你可以做这样的事情: if ! foobar_loc="$(type -p "$foobar_command_name")" || [ -z "$foobar_loc" ]; then # install foobar here fi (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容