Linux 基礎之Shell--殼


一、什麼是Shell?

1.1. 接口(Interface)

操作系統爲用戶提供兩種接口:
CLI(Command Line Interface) 命令接口:用戶通過使用命令來規劃和控制作業的執行,或者對系統進行管理。
API(Application Programming Interface):應用程序接口:通過編程方式請求操作系統服務。

Shell 是命令行解釋器,其實一個用C 語言寫的程序,是介於用戶與Linux內核之間的“翻譯官”,即用戶和Linux 內核之間的接口程序,當從Shell或其他程序向Linux 發送命令時,內核會做出相應的響應。

Shell 作爲命令語言解釋器,它擁有自己內建的Shell命令集,以交互式地解釋和執行用戶發送的命令,將請求的命令加以“翻譯”並傳給Linux內核

Shell 作爲程序設計語言,擁有自己的語法規則。可以通過shell調用系統內核的大部分功能執行程序、創建文檔,並以並行的方式協調各個程序的運行。

1.2 Linux Shell 類別

shell Command
Bourne Shell /bin/sh
C Shell /bin/csh
Korn Shell /bin/ksh
Bourne again Shell /bin/bash
Tenex C Shell /bin/tcsh

查看當前系統支持的Shell:

[root@localhost ~]# cat /etc/shells #當前系統支持的Shell
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
/bin/tcsh
/bin/csh
[root@localhost ~]# echo $SHELL  # 當前的Shell
/bin/bash

1.3 Shell 功能

其主要負責解釋命令行提示符下輸入的命令。Shell 在分析命令時,將它分解成以空白符分開的符號,空白符:空格、換行符、。

1.3 Shell 執行規則

Shell 的處理命令的順序爲:

  1. 別名 alias
  2. 關鍵字 key
  3. 函數 function
  4. 內部命令
  5. 外部命令或外部腳本($PATH)
    環境變量 $PATH:命令可搜索路徑,一個能找到可執行程序的目錄列表
    如果用戶輸入的命令不是一個內部命令並在搜索路徑下沒有找到這個可執行文件,將會返回一個錯誤信息;反之,如果命令可以找到,拿了Shell的內部命令或應用程序將被分解成一系列的系統調用,進而傳遞給Linux 內核。
[root@localhost ~]# type history
history is a shell builtin   #內部命令
[root@localhost ~]# type man
man is /usr/bin/man   #別名
[root@localhost ~]# type ls
ls is aliased to `ls --color=auto'
[root@localhost ~]# type bash  #關鍵字
bash is /usr/bin/bash

二、Shell 命令行

Linux 系統常用的命令格式:
command [flags] [argument1] [argument2] …
命令 可選項 參數 …

命令、選項、參數之間必須由一個或多個空格隔開,如 ls -al;cat test.file
命令的參數指定命令運行的對象,
命令的選項指定命令對象進行特殊的操作
選項,以“-”開始,有長短格式如 -a == --all ,且短格式之間可以合併,長短格式之間不可以合併。

Linux 系統的查看幫助的方式:

  1. 命令 --help(或-h)
  2. man [1-9] <命令>/<配置文件>
  3. pinfo <命令>
  4. /usr/share/doc/ #大多數軟件說明文檔保存於此
Linux 命令行提示符:
bash 中 # 表示超級用戶
$ 表示普通用戶

bash 的實用功能 Tab 鍵:命令和文件名的自動補全功能

三、歷史命令 history

bash 通過歷史命令文件保留一定數目的已經在Shell裏使用過的命令,數目取決於環境變量 HISTSIZE(默認保存1000條)。

在bash Shell 中執行的命令並不會立即直接將命令寫入歷史命令文件,而是先存放到內存的緩衝區中,該緩衝區稱爲歷史命令列表,當bash 退出時將歷史命令列表寫入歷史命令文件。或是執行 history -w 命令,要求bash 立即將歷史命令列表寫入歷史命令文件。

  1. 登錄系統後,歷史命令列表會根據歷史命令文件來初始化。環境變量 HISTFILE可以指定歷史命令文件的名稱,默認是登錄用戶家目錄下的 .bash_history

history [-a|n|r|w] [filename]

選項 功能
-a 把當前的歷史命令列表(記錄)追加到歷史命令文件
-c 清空當前的歷史命令列表(記錄)
-n 把歷史命令文件的內容加入到當前歷史命令列表
-r 把歷史命令文件的內容更新(替換)當前歷史命令列表
-w 把當前的歷史命令列表(記錄)的內容寫入歷史命令文件,並覆蓋歷史命令文件的原來內容(慎重操作)
filename 把當前的歷史命令列表(記錄)寫入到filename的文件

四、命令的別名 alias

別名,就是給複雜的指令創建一個簡單的名字,當用戶使用這個別名時,系統會自動的找到並執行這個別名對應的真實指令。

1.臨時設置別名,僅本次登錄有效

可以通過 alias 別名= 命令
alias 查看當前的別名列表:

[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
定義一個臨時別名
[root@localhost ~]# alias showhome='ls -l /home'  #定義一個臨時別名
[root@localhost ~]# showhome  #執行別名
total 0
drwx------. 3 test test 78 Apr 11  2018  test
[root@localhost ~]# unalias showhome  #取消別名
[root@localhost ~]# showhome  #再次執行報錯
bash: showhome: command not found...
定義一個永久別名,每次登錄都生效

通過編輯 ~/.bashrc 文件定義永久生效的別名

[root@localhost ~]# vim ~/.bashrc  #編輯文件
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias showtmp='ls -l /tmp' #查看 /tmp 目錄

[root@localhost ~]# showtmp  #嘗試執行別名命令
bash: showtmp: command not found...
[root@localhost ~]# logout  #註銷
[root@localhost ~]# showtmp  #再次執行別名命令
total 1016
-rw-------. 1 root root 342778 Oct 31 21:23 cciCaQ59.out
-rw-------. 1 root root 342778 Oct 31 21:39 ccjyUrNg.out
-rw-------. 1 root root 342778 Oct 31 21:24 ccKOE45J.out

注: 定義別名時,等號兩邊不能有空格。等號右邊的命令一般會包含空格或特殊字符,必須使用引號

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章