跳轉到內容

Fedora 和 Red Hat 系統管理/Shell 基礎

來自華夏公益教科書,開放書籍,開放世界

從你連線到 SSH 會話,登入到文字控制檯,或從圖形環境開啟終端的那一刻起,你就在與 shell 程式互動。簡單來說,shell 提供了你的命令列。它接收你輸入的命令,並在需要時執行程式來服務這些命令。

Bash 是 GNU/Linux 系統中最流行的 shell,它的基礎是源於最初的 UNIX shell,即 Borne Shell。雖然 Borne Shell 在它那個時代是革命性的,但按照今天的標準來說,它相當笨拙。像命令列編輯、命令歷史和作業控制這樣的概念是在後來其他 shell 中演變出來的,這些 shell 試圖取代最初的 Borne Shell,例如 C shellKorn Shell。雖然這些特性極大地增強了 shell 的可用性,但它們也付出了破壞與 Borne Shell 語法相容性的代價。當 GNU 專案設計他們的 shell 時,他們選擇實現這些新想法,但仍然保持與最初的 Borne Shell 的相容性。他們將他們的 shell 稱為 Borne Again Shell,或者簡稱為 Bash

shell 的基本思想與最初的 shell 並沒有太大區別,但命令歷史、命令列編輯以及 shell 指令碼中使用的特性則更加特殊。

從 shell 執行命令

[編輯 | 編輯原始碼]

shell 中的命令採用基本形式

COMMAND [-OPTIONS]... [ARG]...

(通常將變數放在大寫字母中,並將可選元素放在方括號中。省略號表示前面的內容可以重複)。

COMMAND 可以是以下四種之一

  • 程式 - shell 將在你的 PATH 環境變數中搜索名為 COMMAND 的程式。大多數程式都在 /bin 或 /usr/bin 中。命令的文件通常可以在手冊頁中找到(使用 `man COMMAND' 訪問)。
  • shell 內建命令 - 內建命令是由 shell 直接處理的命令,例如 cd、echo 和 alias。內建命令的文件可以在 bash 的手冊頁中找到,或者使用 help 內建命令:`help COMMAND'。
  • 別名 - 別名在命令執行之前進行處理,並被替換為另一個命令,可能新增選項或引數。
  • 函式 - 函式呼叫一系列命令,在 shell 指令碼中比在典型的命令列中更常使用。

內建命令示例

[編輯 | 編輯原始碼]

一些最簡單的命令是內建命令。例如,echo 只需列印傳遞給它的引數

[jtk@athena ~]$ echo Hello, World!
Hello, World!

pwd 內建命令列印工作目錄;cd 更改工作目錄。(我們將在討論檔案系統基礎知識時定義“工作目錄”)。

[jtk@athena ~]$ pwd
/home/jtk
[jtk@athena ~]$ cd /usr/share/doc/bash-3.1/
[jtk@athena bash-3.1]$ pwd
/usr/share/doc/bash-3.1
[jtk@athena bash-3.1]$ cd
[jtk@athena ~]$ pwd
/home/jtk

help 內建命令提供內建命令的文件。沒有引數,它會列出所有內建命令。使用引數,help 可以提供有關特定內建命令的詳細資訊

[jtk@athena ~]$ help
GNU bash, version 3.1.7(1)-release (i386-redhat-linux-gnu)
These shell commands are defined internally.  Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
Use `man -k' or `info' to find out more about commands not in this list.

A star (*) next to a name means that the command is disabled.

 JOB_SPEC [&]                       (( expression ))
 . filename [arguments]             :
 [ arg... ]                         (( expression ))
 alias [-p] [name[=value] ... ]     bg [job_spec ...]
 bind [-lpvsPVS] [-m keymap] [-f fi break [n]
 builtin [shell-builtin [arg ...]]  caller [EXPR]
 case WORD in [PATTERN [| PATTERN]. cd [-L|-P] [dir]
 command [-pVv] command [arg ...]   compgen [-abcdefgjksuv] [-o option
 complete [-abcdefgjksuv] [-pr] [-o continue [n]
 declare [-afFirtx] [-p] [name[=val dirs [-clpv] [+N] [-N]
 disown [-h] [-ar] [jobspec ...]    echo [-neE] [arg ...]
 enable [-pnds] [-a] [-f filename]  eval [arg ...]
 exec [-cl] [-a name] file [redirec exit [n]
 export [-nf] [name[=value] ...] or false
 fc [-e ename] [-nlr] [first] [last fg [job_spec]
 for NAME [in WORDS ... ;] do COMMA for (( exp1; exp2; exp3 )); do COM
 function NAME { COMMANDS ; } or NA getopts optstring name [arg]
 hash [-lr] [-p pathname] [-dt] [na help [-s] [pattern ...]
 history [-c] [-d offset] [n] or hi if COMMANDS; then COMMANDS; [ elif
 jobs [-lnprs] [jobspec ...] or job kill [-s sigspec | -n signum | -si
 let arg [arg ...]                  local name[=value] ...
 logout                             popd [+N | -N] [-n]
 printf [-v var] format [arguments] pushd [dir | +N | -N] [-n]
 pwd [-LP]                          read [-ers] [-u fd] [-t timeout] [
 readonly [-af] [name[=value] ...]  return [n]
 select NAME [in WORDS ... ;] do CO set [--abefhkmnptuvxBCHP] [-o opti
 shift [n]                          shopt [-pqsu] [-o long-option] opt
 source filename [arguments]        suspend [-f]
 test [expr]                        time [-p] PIPELINE
 times                              trap [-lp] [arg signal_spec ...]
 true                               type [-afptP] name [name ...]
 typeset [-afFirtx] [-p] name[=valu ulimit [-SHacdfilmnpqstuvx] [limit
 umask [-p] [-S] [mode]             unalias [-a] name [name ...]
 unset [-f] [-v] [name ...]         until COMMANDS; do COMMANDS; done
 variables - Some variable names an wait [n]
 while COMMANDS; do COMMANDS; done  { COMMANDS ; }
[jtk@athena ~]$ help help
help: help [-s] [pattern ...]
     Display helpful information about builtin commands.  If PATTERN is
    specified, gives detailed help on all commands matching PATTERN,
    otherwise a list of the builtins is printed.  The -s option
    restricts the output for each builtin command matching PATTERN to
    a short usage synopsis.
[jtk@athena ~]$ help cd
cd: cd [-L|-P] [dir]
     Change the current directory to DIR.  The variable $HOME is the
    default DIR.  The variable CDPATH defines the search path for
    the directory containing DIR.  Alternative directory names in CDPATH
    are separated by a colon (:).  A null directory name is the same as
    the current directory, i.e. `.'.  If DIR begins with a slash (/),
    then CDPATH is not used.  If the directory is not found, and the
    shell option `cdable_vars' is set, then try the word as a variable
    name.  If that variable has a value, then cd to the value of that
    variable.  The -P option says to use the physical directory structure
    instead of following symbolic links; the -L option forces symbolic links

程式命令示例

[編輯 | 編輯原始碼]

你使用的大多數命令都是程式。也就是說,它們是與 bash shell 在單獨程序中執行的單獨程式。這些程式可以在系統上的許多位置找到。搜尋的目錄在 PATH 環境變數中指定,但我們稍後會詳細介紹。大多數程式都位於 /bin 或 /usr/bin 目錄中,我們現在將重點關注這些目錄。

如果我想獲取目錄中檔案的列表,我會使用 ls 命令。當你輸入“ls”時實際執行的程式位於 /bin/ls。如果不帶引數呼叫,ls 將列出當前目錄中的檔案。可以使用引數來告訴它列出哪個目錄。可以使用選項來告訴它如何列出檔案

[jtk@athena ~]$ ls
bin   Desktop   google-earth  svn
[jtk@athena ~]$ ls -l
drwxrwxr-x 2 jtk jtk      4096 Dec  1 12:32 bin
drwxr-xr-x 8 jtk jtk      4096 Jan 11 13:51 Desktop
drwxr-xr-x 9 jtk jtk      4096 Dec 17 22:21 google-earth
-rw-r--r-- 1 jtk jtk  16791707 Dec  5 16:43 livejournal.tar.bz2
drwxrwxr-x 2 jtk jtk      4096 Dec  8 13:23 svn
[jtk@athena ~]$ ls -lt
drwxrwxr-x 2 jtk jtk      4096 Dec  1 12:32 bin
-rw-r--r-- 1 jtk jtk  16791707 Dec  5 16:43 livejournal.tar.bz2
drwxrwxr-x 2 jtk jtk      4096 Dec  8 13:23 svn
drwxr-xr-x 9 jtk jtk      4096 Dec 17 22:21 google-earth
drwxr-xr-x 8 jtk jtk      4096 Jan 11 13:51 Desktop
[jtk@athena ~]$ ls /home/
db2inst1  jtk  lost+found
[jtk@athena ~]$ ls -l /home/
total 24
drwxr-xr-x  7 db2inst1 db2iadm1  4096 Jul 17  2006 db2inst1
drwx------ 11 jtk      jtk       4096 Feb  2 11:47 jtk
drwx------  2 root     root     16384 Apr 19  2006 lost+found

請注意選項字串“-lt”。“l”指定“長”列表,而“t”指定條目應按修改時間排序。這些是單字元選項,可以單獨指定為“-l -t”,但通常像這樣分組在一起。

通常將選項放在引數之前,例如在命令“ls -l /home”中。對於 GNU 工具,這是可選的,但有些工具會在相同引數之後出現任何選項時報錯。(對於 shell 或作業系統來說,選項和引數之間沒有區別。當核心執行命令時,它們都是引數,程式負責解釋它們)。

以 root 使用者身份執行命令示例

[編輯 | 編輯原始碼]

如果你是一個非 root 使用者,並且需要安裝或執行需要管理員憑據的命令,只需執行以下操作

[jtk@athena ~]$ su root
Enter root password
Run the command you want (as root).

就這麼簡單。

如果你是一個使用者,並且想要列出機器的 IP 地址,可以使用 sudo 命令,現在讓我們假設你被允許使用 sudo 命令

# sudo /sbin/ifconfig 

類似地,你可以使用 sudo 執行任何超級使用者命令

華夏公益教科書