日期:2014-05-16  浏览次数:20646 次

linux enable命令学习

shell命令用来启动或关闭shell内建命令。

通过type命令查看可以知道,enable命令本身也是一个shell内建命令。
sh-# type enable
enable is a shell builtin

使用enable命令可以查看到当前系统中有哪些shell内建命令,
也可以确认enable是一个shell builtin命令。
sh-# enable
enable .
enable :
enable [
enable alias
enable bind
enable break
enable builtin
enable caller
enable cd
enable command
enable compgen
enable complete
enable continue
enable declare
enable dirs
enable echo
enable enable
enable eval
enable exec
enable exit
enable export
enable false
enable fc
enable getopts
enable hash
enable help
enable history
enable kill
enable let
enable local
enable logout
enable popd
enable printf
enable pushd
enable pwd
enable read
enable readonly
enable return
enable set
enable shift
enable shopt
enable source
enable test
enable times
enable trap
enable true
enable type
enable typeset
enable ulimit
enable umask
enable unalias
enable unset
enable wait
sh-#

linux shell命令执行时,shell总是先在自己的shell builtin中查找该命令,如果找到则执行该命令;
如果找不到该命令,则会从环境变量$PATH指定的路径中依次去查找待执行的命令。
因为了解了这一点,所以看起来好像没有办法编写用户自己的命令来替代shell builtin命令。
幸运的是,有了enable命令我们就能做到了。

我们以shell内建命令hostory命令为例进行说明。

1. 可以看到history是一个shell builtin命令,并且也可以正常执行history命令。
sh-# type history
history is a shell builtin
sh-# history
    1  type enable
    2  type ooxx
    3  type ooxx
    4  enable -all
    5  enable -all
    6  enable all
    7  enable
    8  enable
    9  enable
   10  pwd
   11  enable -n pwd
   12  pwd
   13  type
   14  type pwd
   15  en
   16  history
  
2. 使用enable命令来关闭history命令。
sh-# enable -n history
sh-#

3. 通过实验验证,history命令真的被关闭了。
所以,用户可以根据需求编写自己的history命令了。
sh-# type history
sh: type: history: not found
sh-# history
sh: history: command not found
sh-#
sh-# enable
enable .
enable :
enable [
enable alias
enable bind
enable break
enable builtin
enable caller
enable cd
enable command
enable compgen
enable complete
enable continue
enable declare
enable dirs
enable echo
enable enable
enable eval
enable exec
enable exit
enable export
enable false
enable fc
enable getopts
enable hash
enable help
enable kill
enable let
enable local
enable logout
enable popd
enable printf
enable pushd
enable read
enable readonly
enable return
enable set
enable shift
enable shopt
enable source
enable test
enable times
enable trap
enable true
enable type
enable typeset
enable ulimit
enable umask
enable unalias
enable unset
enable wait

4. 被关闭的命令,仍然可以使用enable命令将其启动。
sh-# enable history
sh-#

5. 可以看到,history命令被启动起来了。
sh-# type history
history is a shell builtin
sh-# enable
enable .
enable :
enable [
enable alias
enable bind
enable break
enable builtin
enable caller
enable cd
enable command
enable compgen
enable complete
enable continue
enable declare
enable dirs
enable echo