linux命令的执行

作者: Aubin | 来源:发表于2017-07-15 11:28 被阅读32次

1. 命令的概念

  • 命令的执行过程

系统第一次执行外部命令时Hash缓存表为空,系统会先从PTAH路径下寻找命令,找到后会将路径加入到Hasa缓存中,当再次执行此命令时会直接从Hash的路径下执行,如果存在直接执行,如果不存在将继续从PATH下的路径继续查找,Hash表可以提高命令的调用速率。

  • 命令的优先级

alias -------------------------------------别名
  builtin------------------------------内部命令
    hash-------------------------缓存表
      $PATH---------------可执行程序或脚本(外部命令)

  • 内部命令与外部命令

内部命令是shell自带的
外部命令是安装系统时默认安装的,并且在文件系统下有对应的路径

  • 查看命令是内部命令还是外部命令type [commnd]
[root@centos6 ~]# type cat                  #判断cat命令,外部命令显示文件路径
cat is /bin/cat
[root@centos6 ~]# type cd                   #判断cd命令
cd is a shell builtin

2.命令的别名

命名别名只在当前进程中有效
如果想永久有效,要定义在配置文件中
  仅对当前用户:~/.bashrc
  对所有用户有效:/etc/bashrc

  • 查看进程中所有的别名 alias
[root@centos6 ~]#alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
......
  • 定义别名 alias NAME="VALUE"
[root@centos6 ~]#alias aubin=cat
[root@centos6 ~]#aubin test
hello world
  • 删除别名
[root@centos6 ~]#unalias aubin
[root@centos6 ~]#aubin test
-bash: aubin: command not found
  • 定义对当前用户永久生效的别名
[root@centos6 ~]#vim .bashrc 
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias aubin=cat                                # <<<-----此处定义别名
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
[root@centos6 ~]#. .bash                       #立即生效
  • 定义指定用户生效的别名
[root@centos6 ~]#cd ~ li
[root@centos6 li]#vim .bashrc                 #编辑用户目录下的.bashrc
  • 定义所有用户生效的别名
[root@centos6 ~]#vim /etc/bashrc
alias aubin=cat                                # <<<-----加入定义别名
[root@centos6 ~]#. /etc/bashrc                 #立即生效

3.内部命令

shell程序找到键入命令所对应的可执行程序或代码,由shell分析后提交给内核分配资源并将其运行起来。

  • 查看所有的内部命令
[root@centos6 ~]#help
[root@centos6 ~]#enable
enable .
enable :
enable [
enable alias
enable bg
enable bind
......
  • 内部命令的禁用与启用enable
[root@centos6 li]#enable -n cd                 #禁用内部命令
[root@centos6 li]#enable cd                    #启用内部命令
  • 禁用内部命令失效
[root@centos6 li]#enable -n pwd
[root@centos6 li]#enable -n                    #查看禁用的内部命令或如下图用help
enable -n pwd

也可以help查已经被禁用的命令【命令前的*代表命令已经用】

黄色方框 中命令前的*代表命令已禁用
禁用内部命令enable -n pwd后依然可以使用
[root@centos6 li]#pwd
/home/li

使用which查看命令的执行文件

[root@centos6 li]#which pwd
/bin/pwd

当内部命令禁用后,按照bash优先级继续搜索Hash表、$PATH。直到在$PATH中发现/bin/pwd的可执行文件则将其运行。

  • 查看禁用的内部命令
[root@centos6 li]#enable -n
enable -n cd
enable -n pwd

或者如上图所示使用help命令查看


4.HASH缓存表

用来显示和清除哈希表,执行命令的时候,系统将先查询哈希表。

  • 查看命令的缓存 hash
[root@centos6 ~]# hash
  hits  command
   3    /usr/bin/cal
   1    /usr/bin/yum
[root@centos6 ~]# 查看详细的Hash表
[root@centos6 ~]#hash -l
builtin hash -p /bin/dd dd
builtin hash -p /usr/bin/yum yum
  • 向Hash表中增加内容 hash -p path command
[root@centos6 ~]#将cat定义一个别名存在hash表
[root@centos6 ~]#hash -p /bin/cat aubin  
[root@centos6 ~]#aubin test
hello world
  • 打印Hash表中命令的路径 hash -t command
[root@centos6 ~]#hash -t aubin
/bin/cat
  • 删除Hash表中指定命令 hash -d command
[root@centos6 ~]#hash -d aubin
  • 删除Hash表中所有命令hash -r
[root@centos6 ~]# hash -r
  • 查看命令的路径 which
[root@centos6 ~]# which cat            #查看命令的路径,以第一个路径为准
/bin/cat
[root@centos6 ~]# which -a cat         #查看命令所有路径,一个命令可能有多个路径
/bin/cat
/usr/local/bin/cat

5.外部命令

外部命令就是一个可执行文件,当执行外部命令时,系统会去执行在文件目录下对应的可执行文件。

  • 列出命令的路径
[root@centos6 /]#which echo                    #列出命令的路径
/bin/echo
[root@centos6 /]#which cp                      #which列出文件路径会显示别名
alias cp='cp -i'
        /bin/cp
[root@centos6 /]#which --skip-alias cp         #列出文件路径而不显示别名
/bin/cp
  • 列出命令所有路径,多个bash有相同命令时,则命令有多个路径。
[root@centos6 /]#which -a echo              
/bin/echo
  • 列出命令与帮助手册的路径
[root@centos6 /]#whereis echo
echo: /bin/echo /usr/share/man/man1/echo.1.gz /usr/share/man/man1p/echo.1p.gz

相关文章

  • java远程执行shell命令失败

    需要通过java远程登录linux系统执行shell命令,执行的shell命令是 这条命令在linux下执行完全没...

  • Linux 命令及技巧

    Linux 命令拼接 顺序执行 前面执行成功后,后面不执行 前面执行成功后,后面才执行 Linux find 命令...

  • Linux相关使用介绍

    一、Linux内外部命令的判断方式以及命令的执行流程 1、Linux命令 Linux命令分为内部命令(shell自...

  • linux命令

    linux命令 1、可以同时执行多个命令

  • Linux命令之文件管理 (三十九)

    Linux tmpwatch命令 Linux tmpwatch命令用于删除暂存文件。 执行tmpwatch指令可删...

  • Python执行常见的linux命令

    0. 概述 使用os.system(代表着linux命令的字符串)就能执行linux命令 因为linux多条命令可...

  • Linux &&和&及|和||的用法介绍!

    在使用Linux命令时,我们往往可以一行执行多条命令,或者有条件的执行下一条命令,对于刚接触Linux命令时,特殊...

  • Linux 命令合集

    服务端常用的Linux命令Linux vim命令行快捷键Linux history命令显示执行时间 MacOS 释...

  • linux - 卸载python

    linux - 卸载python 命令执行即可

  • 命令执行顺序 && 管道

    命令的执行顺序 linux命令的执行一般是,在终端中输入一行命令,执行完成,再继续输入下一命令。 顺序执行多条命令...

网友评论

    本文标题:linux命令的执行

    本文链接:https://www.haomeiwen.com/subject/gyhnhxtx.html