赞
踩
在Linux中,command -v 可以判断一个命令是否支持
demo:
if command -v docker >/dev/null 2>&1;then
echo "yes"
else
echo "no"
fi
如果系统不支持docker,则为no。
2>&1:
在Linux系统中0 1 2是一个文件描述符
从上表看的出来,我们平时使用的
echo “hello” > t.log
其实也可以写成
echo “hello” 1> t.log
2>&1 就是把标准错误输出标准输出中。
判断 shellcheck 命令是否存在,不存在则安装shellcheck :
#!/bin/bash
# fast fail.
set -eo pipefail
SHELLCHECK_VERSION=0.7.1
INSTALL_DIR="${HOME}/bin/"
mkdir -p "$INSTALL_DIR" || true
function install_shellcheck {
if ! command -v shellcheck &> /dev/null; then
MACHINE=$(uname -m);
TMPFILE=$(mktemp)
rm "$TMPFILE"
mkdir -p "$TMPFILE"/
curl -L -o "$TMPFILE"/out.xz "https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/shellcheck-v${SHELLCHECK_VERSION}.$(uname -s | tr '[:upper:]' '[:lower:]').${MACHINE}.tar.xz"
tar -xf "$TMPFILE"/out.xz -C "$TMPFILE"/
cp "${TMPFILE}/shellcheck-v${SHELLCHECK_VERSION}/shellcheck" "${INSTALL_DIR}/shellcheck"
rm -rf "$TMPFILE"
chmod +x "${INSTALL_DIR}"/shellcheck
fi
}
install_shellcheck
command命令在shell脚本里面,如果发现有个函数和我们需要执行的命令同名,我们可以用command用来强制执行后面的命令,而不是同名函数,然后我们也可以在shell脚本里面判断莫个命令是否存在,我们平时一般用which命令也行。
[root@dev workspace]# command --help
command: command [-pVv] command [arg ...]
Execute a simple command or display information about commands.
Runs COMMAND with ARGS suppressing shell function lookup, or display
information about the specified COMMANDs. Can be used to invoke commands
on disk when a function with the same name exists.
Options:
-p use a default value for PATH that is guaranteed to find all of
the standard utilities
-v print a description of COMMAND similar to the `type' builtin
-V print a more verbose description of each COMMAND
Exit Status:
Returns exit status of COMMAND, or failure if COMMAND is not found.
[root@dev workspace]#
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。