學(xué)習(xí)啦 > 學(xué)習(xí)電腦 > 操作系統(tǒng) > Linux教程 > linux shell命令分類

linux shell命令分類

時(shí)間: 佳洲1085 分享

linux shell命令分類

  在Linux下,命令有幾種類型,對于一個(gè)Linux新手來說,知道不同命令的意思才能夠高效和準(zhǔn)確的使用它們。下面由學(xué)習(xí)啦小編為大家整理了部分文件的linux shell命令分類,希望對大家有幫助!

  linux shell命令分類

  1. 程序可執(zhí)行文件(文件系統(tǒng) 中的命令)

  當(dāng)你執(zhí)行一條命令的時(shí)候,Linux 通過從左到右搜索存儲(chǔ)在 $PATH 環(huán)境變量中的目錄來找到這條命令的可執(zhí)行文件。你可以像下面這樣查看存儲(chǔ)在 $PATH 中的目錄:

  $ echo $PATH

  /home/aaronkilik/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

  在上面的命令中,目錄

  /home/aaronkilik/bin

  將會(huì)被首先搜索,緊跟著是

  /usr/local/sbin

  然后一直接著下去。在搜索過程中,搜索順序是至關(guān)重要的。比如在 /usr/bin 目錄里的文件系統(tǒng)中的命令 $ ll /bin/。

  示例輸出:

  total 16284

  drwxr-xr-x 2 root root 4096 Jul 31 16:30 ./

  drwxr-xr-x 23 root root 4096 Jul 31 16:29 ../

  -rwxr-xr-x 1 root root 6456 Apr 14 18:53 archdetect*

  -rwxr-xr-x 1 root root 1037440 May 17 16:15 bash*

  -rwxr-xr-x 1 root root 520992 Jan 20 2016 btrfs*

  -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-calc-size*

  lrwxrwxrwx 1 root root 5 Jul 31 16:19 btrfsck -> btrfs*

  -rwxr-xr-x 1 root root 278376 Jan 20 2016 btrfs-convert*

  -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-debug-tree*

  -rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-find-root*

  -rwxr-xr-x 1 root root 270136 Jan 20 2016 btrfs-image*

  -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfs-map-logical*

  -rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-select-super*

  -rwxr-xr-x 1 root root 253816 Jan 20 2016 btrfs-show-super*

  -rwxr-xr-x 1 root root 249464 Jan 20 2016 btrfstune*

  -rwxr-xr-x 1 root root 245368 Jan 20 2016 btrfs-zero-log*

  -rwxr-xr-x 1 root root 31288 May 20 2015 bunzip2*

  -rwxr-xr-x 1 root root 1964536 Aug 19 2015 busybox*

  -rwxr-xr-x 1 root root 31288 May 20 2015 bzcat*

  lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzcmp -> bzdiff*

  -rwxr-xr-x 1 root root 2140 May 20 2015 bzdiff*

  lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzegrep -> bzgrep*

  -rwxr-xr-x 1 root root 4877 May 20 2015 bzexe*

  lrwxrwxrwx 1 root root 6 Jul 31 16:19 bzfgrep -> bzgrep*

  -rwxr-xr-x 1 root root 3642 May 20 2015 bzgrep*

  2. Linux 別名

  這些是用戶定義的命令,它們是通過 shell 內(nèi)置命令 alias 創(chuàng)建的,其中包含其它一些帶有選項(xiàng)和參數(shù)的 shell 命令。這個(gè)意圖主要是使用新穎、簡短的名字來替代冗長的命令。創(chuàng)建一個(gè)別名的語法像下面這樣:

  $ alias newcommand='command -options'

  通過下面的命令,可以列舉系統(tǒng)中的所有別名:

  $ alias -p

  alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '/''s/^/s*[0-9]/+/s*//;s/[;&|]/s*alert$//'/'')"'

  alias egrep='egrep --color=auto'

  alias fgrep='fgrep --color=auto'

  alias grep='grep --color=auto'

  alias l='ls -CF'

  alias la='ls -A'

  alias ll='ls -alF'

  alias ls='ls --color=auto'

  要在 Linux 中創(chuàng)建一個(gè)新的別名,仔細(xì)閱讀下面的例子。

  $ alias update='sudo apt update'

  $ alias upgrade='sudo apt dist-upgrade'

  $ alias -p | grep 'up'

  然而,上面這些我們創(chuàng)建的別名只能暫時(shí)的工作,當(dāng)經(jīng)過下一次系統(tǒng)啟動(dòng)后它們不再工作。你可以像下面展示的這樣在 '.bashrc' 文件中設(shè)置永久別名。

  添加以后,運(yùn)行下面的命令來激活:

  $ source ~/.bashrc

  3. Linux Shell 保留字

  在 shell 程序設(shè)計(jì)中,

  if、then、fi、for、while、case、esac、else、until

  以及其他更多的字都是 shell 保留字。正如描述所暗示的,它們在 shell 中有特殊的含義。你可以通過使用下面展示的 type 命令來列出所有的 shell 關(guān)鍵字:

  $ type if then fi for while case esac else until

  if is a shell keyword

  then is a shell keyword

  fi is a shell keyword

  for is a shell keyword

  while is a shell keyword

  case is a shell keyword

  esac is a shell keyword

  else is a shell keyword

  until is a shell keyword

  4. Linux shell 函數(shù)

  一個(gè) shell 函數(shù)是一組在當(dāng)前 shell 內(nèi)一起執(zhí)行的命令。函數(shù)有利于在 shell 腳本中實(shí)現(xiàn)特殊任務(wù)。在 shell 腳本中寫 shell 函數(shù)的傳統(tǒng)形式是下面這樣:

  function_name() {

  command1

  command2

  ......

  }

  或者像這樣:

  function function_name {

  command1

  command2

  ......

  }

  讓我們看一看如何在一個(gè)名為 shell_functions.sh 的腳本中寫 shell 函數(shù)。

  #!/bin/bash

  #write a shell function to update and upgrade installed packages

  upgrade_system(){

  sudo apt update;

  sudo apt dist-upgrade;

  }

  #execute function

  upgrade_system

  取代通過命令行執(zhí)行兩條命令:

  sudo apt update

  和

  sudo apt dist-upgrade

  我們在腳本內(nèi)寫了一個(gè)像執(zhí)行一條單一命令一樣來執(zhí)行兩條命令的 shell 函數(shù) upgrade_system。保存文件,然后使腳本可執(zhí)行。最后像下面這樣運(yùn)行 shell 函數(shù):

  $ chmod +x shell_functions.sh

  $ ./shell_functions.sh

  5. Linux Shell 內(nèi)置命令

  這些是在 shell 中內(nèi)置的 Linux 命令,所以你無法在文件系統(tǒng)中找到它們。這些命令包括 pwd、cd、bg、alias、history、type、source、read、exit 等,你可以通過下面展示的 type 命令來列出或檢查 Linux 內(nèi)置命令:

  $ type pwd

  pwd is a shell builtin

  $ type cd

  cd is a shell builtin

  $ type bg

  bg is a shell builtin

  $ type alias

  alias is a shell builtin

  $ type history

  history is a shell builtin

  結(jié)論

  作為一個(gè) Linux 用戶,知道你所運(yùn)行的命令類型是很重要的。我相信,通過上面明確、簡單并且易于理解的解釋,包括一些相關(guān)的說明,你可能對 “Linux命令的不同種類”有了很好的理解。

3576363