Sil*_*eth 7 scripting ksh function shell-builtin
如何检查命令是否是 的内置命令ksh?
在tcsh你可以使用where; 在zsh和bash您可以使用type -a; 在某些现代版本中,ksh您可以使用whence -av.
我想要做的是编写一个isbuiltin在任何版本ksh(包括ksh88和任何其他“旧”版本ksh)中工作的函数,其行为如下:
0如果所有给定的命令都是内置的,则返回(成功)1(失败)并将消息打印到 stderr。我已经有这样的工作功能zsh并bash使用上述命令。
这是我的目的ksh:
isbuiltin() {
if [[ "$#" -eq 0 ]]; then
echo "Usage: isbuiltin cmd" >&2
return 1
fi
for cmd in "$@"
do
if [[ $cmd = "builtin" ]]; then
#Handle the case of `builtin builtin`
echo "$cmd is not a built-in" >&2
return 1
fi
if ! whence -a "$cmd" 2> /dev/null | grep 'builtin' > /dev/null ; then
echo "$cmd is not a built-in" >&2
return 1
fi
done
}
Run Code Online (Sandbox Code Playgroud)
此功能适用于 ksh93。但是,似乎 ksh88 的版本whence不支持该-a选项,这是使其显示所有出现的选项。无法显示所有出现的情况,我只能使用whence -v,它确实告诉我命令是否是内置的,但前提是没有同名的别名或函数。
问题:还有什么可以代替whence -avin 的ksh88吗?
使用接受的答案(打开子shell),这是我更新的解决方案。将以下内容放在 .kshrc 中:
isbuiltin() {
if [[ "$#" -eq 0 ]]; then
printf "Usage: isbuiltin cmd\n" >&2
return 1
fi
for cmd in "$@"
do
if (
#Open a subshell so that aliases and functions can be safely removed,
# allowing `whence -v` to see the built-in command if there is one.
unalias "$cmd";
if [[ "$cmd" != '.' ]] && typeset -f | egrep "^(function *$cmd|$cmd\(\))" > /dev/null 2>&1
then
#Remove the function iff it exists.
#Since `unset` is a special built-in, the subshell dies if it fails
unset -f "$cmd";
fi
PATH='/no';
#NOTE: we can't use `whence -a` because it's not supported in older versions of ksh
whence -v "$cmd" 2>&1
) 2> /dev/null | grep -v 'not found' | grep 'builtin' > /dev/null 2>&1
then
#No-op. Needed to support some old versions of ksh
:
else
printf "$cmd is not a built-in\n" >&2
return 1
fi
done
return 0
}
Run Code Online (Sandbox Code Playgroud)
我已经在 Solaris、AIX 和 HP-UX 中使用 ksh88 对此进行了测试。它适用于我测试的所有情况。我还在 FreeBSD、Ubuntu、Fedora 和 Debian 中使用现代版本的 ksh 对此进行了测试。
如果您担心别名,请执行以下操作:
[[ $(unalias -- "$cmd"; type -- "$cmd") = *builtin ]]
Run Code Online (Sandbox Code Playgroud)
($(...)创建一个子shell环境,所以unalias只在那里有效)。
如果您还关心函数,也请command unset -f -- "$cmd"在type.
| 归档时间: |
|
| 查看次数: |
1582 次 |
| 最近记录: |