如果命令是内置的,则检查 bash 和 csh
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7399713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking in bash and csh if a command is builtin
提问by Natan Yellin
How can I check in bash and csh if commands are builtin? Is there a method compatible with most shells?
如果命令是内置的,我如何检查 bash 和 csh?是否有与大多数 shell 兼容的方法?
采纳答案by Jon Lin
You can try using whichin csh or typein bash. If something is a built-in command, it will say so; otherwise, you get the location of the command in your PATH.
您可以尝试which在 csh 或typebash 中使用。如果某些东西是内置命令,它会这样说;否则,您将在 PATH 中获得命令的位置。
In csh:
在 csh 中:
# which echo
echo: shell built-in command.
# which parted
/sbin/parted
In bash:
在 bash 中:
# type echo
echo is a shell builtin
# type parted
parted is /sbin/parted
typemight also show something like this:
type也可能显示如下内容:
# type clear
clear is hashed (/usr/bin/clear)
...which means that it's not a built-in, but that bash has stored its location in a hashtable to speed up access to it; (a little bit)more in this post on Unix & Linux.
...这意味着它不是内置的,但 bash 已将其位置存储在哈希表中以加快对它的访问;(一点)在这篇关于 Unix 和 Linux 的帖子中更多。
回答by paxdiablo
In bash, you can use the typecommand with the -toption. Full details can be found in the bash-builtinsman page but the relevant bit is:
在 bash 中,您可以使用type带有-t选项的命令。可以在bash-builtins手册页中找到完整的详细信息,但相关位是:
type-t name
If the
-toption is used, type prints a string which is one ofalias,keyword,function,builtin, orfileif name is an alias, shell reserved word, function, builtin, or disk file, respectively. If the name is not found, then nothing is printed, and an exit status of false is returned.
输入-t 名称
如果使用该
-t选项,则 type 分别打印一个字符串,该字符串是alias,keyword,function, 之一builtin,或者file如果 name 是别名、shell 保留字、函数、内置文件或磁盘文件。如果未找到名称,则不打印任何内容,并返回 false 退出状态。
Hence you can use a check such as:
因此,您可以使用支票,例如:
if [[ "$(type -t read)" == "builtin" ]] ; then echo read ; fi
if [[ "$(type -t cd)" == "builtin" ]] ; then echo cd ; fi
if [[ "$(type -t ls)" == "builtin" ]] ; then echo ls ; fi
which would result in the output:
这将导致输出:
read
cd
回答by Mu Qiao
For bash, use type command
对于bash,使用type command
回答by wormsparty
For csh, you can use:
对于csh,您可以使用:
which command-name
which command-name
If it's built-in, it will tell so. Not sure if it works the same for bash. We careful with aliases, though. There may be options for that.
如果它是内置的,它会告诉你。不确定它对 bash 是否同样有效。不过,我们小心别名。可能有一些选择。
回答by Sildoreth
The other answers here are close, but they all fail if there is an alias or function with the same name as the command you're checking.
这里的其他答案很接近,但如果存在与您正在检查的命令同名的别名或函数,它们都会失败。
Here's my solution:
这是我的解决方案:
In tcsh
在 tcsh
Use the wherecommand, which gives all occurrences of the command name, including whether it's a built-in. Then grepto see if one of the lines says that it's a built-in.
使用where命令,它给出所有出现的命令名称,包括它是否是内置命令。然后grep看看其中一行是否说它是内置的。
alias isbuiltin 'test \!:1 != "builtin" && where \!:1 | egrep "built-?in" > /dev/null || echo \!:1" is not a built-in"'
In bash/zsh
在bash/zsh
Use type -a, which gives all occurrences of the command name, including whether it's a built-in. Then grepto see if one of the lines says that it's a built-in.
使用type -a,它给出所有出现的命令名称,包括它是否是内置的。然后grep看看其中一行是否说它是内置的。
isbuiltin() {
if [[ $# -ne 1 ]]; then
echo "Usage: isbuiltin() {
if [[ $# -ne 1 ]]; then
echo "Usage: $ isbuiltin command
command"
return 1
fi
cmd=
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
:
else
printf "$cmd is not a built-in\n" >&2
return 1
fi
}
command"
return 1
fi
cmd=
if ! type -a $cmd 2> /dev/null | egrep '\<built-?in\>' > /dev/null
then
printf "$cmd is not a built-in\n" >&2
return 1
fi
return 0
}
In ksh88/ksh93
在ksh88/ksh93
Open a sub-shell so that you can remove any aliases or command names of the same name. Then in the subshell, use whence -v. There's also some extra archaic syntax in this solution to support ksh88.
打开一个子shell,以便您可以删除同名的任何别名或命令名称。然后在子shell中,使用whence -v. 此解决方案中还有一些额外的古老语法来支持ksh88.
if isbuiltin $cmd 2> /dev/null
then
echo "$cmd is a built-in"
else
echo "$cmd is NOT a built-in"
fi
Using the Solution
使用解决方案
Once you applied the aforementioned solution in the shell of your choice, you can use it like this...
在您选择的 shell 中应用上述解决方案后,您可以像这样使用它......
At the command line:
在命令行:
##代码##If the command is a built-in, it prints nothing; otherwise, it prints a message to stderr.
如果命令是内置命令,则不打印任何内容;否则,它会向 stderr 打印一条消息。
Or you can use it like this in a script:
或者您可以在脚本中像这样使用它:
##代码##
