linux自定義和使用 shell 環境(一)

概述

  學習自定義 Linux bash shell 環境來滿足用戶需求。學習:

· 修改全局和用戶配置文件

· 在登錄或生成新 shell 時設置環境變量

· 爲常用命令序列創建 bash 函數

· 爲新用戶帳戶維護框架目錄

· 設置命令搜索路徑

Linux shell

      在終端上使用一個 Linux shell 程序,通過鍵入命令(輸入流)來與系統交互,並在同一個終端上查看輸出(輸出流)和錯誤消息(錯誤流)。有時,您需要在系統引導以前運行命令,以便允許終端建立連接,有時您需要定期運行命令,無論您是否登錄。shell 也可以爲您完成這些任務。標準輸入和輸出流不需要來自或定向到終端上的真實用戶。在本教程中,將進一步瞭解 shell 和自定義用戶的環境。具體地講,您將學習 bash (Bourne again) shell(原始 original Bourne shell 的增強版),並瞭解使 bash 更符合可移植操作系統接口 (Portable Operating System Interface, POSIX) 標準的更改。我會在此過程中介紹其他一些 shell 特性。

Shell 和環境

    shell 與操作系統提供了一個層。藉助 Linux(和 UNIX)shell,組合基本函數來構建複雜的操作。然後可以使用編程結構,構建能在 shell 中直接執行的函數或將函數保存爲 shell 腳本。shell 腳本是一個命令序列,它存儲在 shell 運行的文件中。

表 1. 常見 bash 環境變量

名稱

用途

USER

登錄用戶的名稱

UID

登錄用戶的用戶 ID 數字

HOME

用戶的主目錄

PWD

當前工作目錄

SHELL

shell 的名稱

PPID

父進程的 PID —啓動此進程的進程的進程 ID

shell 還會設置一些特殊參數,但這些參數無法修改。

表 2. 常見 bash 參數

名稱

用途

$

(運行的 bash shell [ 或其他 ] 進程)的進程 ID(或 PID)

?

上一個命令的退出代碼

0

shell 或 shell 腳本的名稱

使用變量

在名稱前面加上 $作爲前綴來使用變量的值,如1所示。

1. 使用變量值

 [ian@atticf22 ~]$ echo $UID 1000 

 [ian@atticf22 ~]$ echo $HOME /home/ian

設置變量值並讓它們可用

    通過鍵入一個名稱後不留空格,立即鍵入等號 (=),在 bash shell 中創建或設置 shell 變量。變量名是僅由字母數字字符和下劃線組成的單詞。名稱以字母字符或下劃線開頭。變量名是區分大小寫的,所以 var1和 VAR1是不同的變量。變量名(特別是導出變量)通常採用大寫形式,就像 表 1中的示例一樣,但這是一種約定,而不是一種要求。一些變量(比如 $$和 $?)實際上是 shell 參數而不是變量 —只能引用它們,不能向它們賦值。

Shell 變量僅對您創建它們時所在的進程可見,除非 導出它們,以便子進程可以看到和使用它們。子進程不能將變量導出到父進程。可以使用export命令導出變量。在 bash shell 中,可以一步完成分配和導出,但不是所有 shell 都支持這項功能。

    探索這些概念的一種好方法是使用另一個 shell 來創建子進程。可以使用 ps命令幫助跟蹤您所在的位置和正在運行的進程。

2. 設置 shell 變量並將它們導出到環境

ian@atticf22 ~]$ # Use the ps command to list current PID, parent PID and running command name

 [ian@atticf22 ~]$ ps -p $$ -o "pid ppid cmd"

  PID  PPID CMD 

 12761  9169 bash 

 [ian@atticf22 ~]$ # Start a child bash process

 [ian@atticf22 ~]$ bash

 [ian@atticf22 ~]$ # Assign two variables

 [ian@atticf22 ~]$ VAR1=v1 

 [ian@atticf22 ~]$ VAR2="Variable 2"

 [ian@atticf22 ~]$ # Export examples

 [ian@atticf22 ~]$ export VAR2 

 [ian@atticf22 ~]$ export VAR3="Assigned and exported in one step"

 [ian@atticf22 ~]$ # Use the $ character to reference the variables $ export VAR4=var4

 [ian@atticf22 ~]$ echo $VAR1 '/' $VAR2 '/' $VAR3

 v1 / Variable 2 / Assigned and exported in one step 

 [ian@atticf22 ~]$ # What is the value of the SHELL variable?

 [ian@atticf22 ~]$ echo $SHELL

 /bin/bash 

 [ian@atticf22 ~]$ # Now start ksh child and export VAR4

 [ian@atticf22 ~]$ ksh

 $ ps -p $$ -o "pid ppid cmd"

  PID  PPID CMD 

 26212 22923 ksh 

 $ export VAR4=var4

 $ # See what is visible

 $ echo $VAR1 '/' $VAR2 '/' $VAR3 '/' $VAR4 '/' $SHELL

 / Variable 2 / Assigned and exported in one step / var4 / /bin/bash 

 $# No VAR1 and shell is /bin/bash - is that right?

 $ exit 

 [ian@atticf22 ~]$ ps -p $$ -o "pid ppid cmd"

  PID  PPID CMD 

 22923 12761 bash 

 [ian@atticf22 ~]$ # See what is visible

 [ian@atticf22 ~]$ echo $VAR1 '/' $VAR2 '/' $VAR3 '/' $VAR4 '/' $SHELL

 v1 / Variable 2 / Assigned and exported in one step / / /bin/bash 

 [ian@atticf22 ~]$ # No VAR4 - our child cannot export back to us

 [ian@atticf22 ~]$ exit

 exit 

 [ian@atticf22 ~]$ ps -p $$ -o "pid ppid cmd"

  PID  PPID CMD 

 12761  9169 bash 

 [ian@atticf22 ~]$ # See what is visible

 [ian@atticf22 ~]$ echo $VAR1 '/' $VAR2 '/' $VAR3 '/' $VAR4 '/' $SHELL

 / / / / /bin/bash 

 [ian@atticf22 ~]$ # None of VAR1 through VAR4 is exported back to parent

    2中否注意到,ksh 沒有設置 SHELL變量?但在您登錄時,或者使用 su命令和創建 登錄 shell的選項來切換到另一個用戶時,通常會設置此變量。

可使用 echo命令查看 表 1和 表 2中的一些常見 bash 變量

3. 常見環境變量和 shell 變量

[ian@atticf22 ~]$ echo $USER $UID

 ian 1000 

 [ian@atticf22 ~]$ echo $SHELL $HOME $PWD

 /bin/bash /home/ian /home/ian 

 [ian@atticf22 ~]$ (exit 0);echo $?;(exit 4);echo $?

 0 

 4 

 [ian@atticf22 ~]$ echo $0

 bash 

 [ian@atticf22 ~]$ echo $$ $PPID

 12761 9169 

 [ian@atticf22 ~]$ # see what my process and its parent are running

 [ian@atticf22 ~]$ ps -p $$,$PPID -o "pid ppid cmd"

  PID  PPID CMD 

 9169 1 /usr/libexec/gnome-terminal-server 

 12761  9169 bash

    在 bash shell 中,還可以通過在命令前加上名稱 = 值對作爲前綴,將環境值設置爲在單個命令的持續時間內有效

4. 爲單個命令設置 bash 環境值 

 [ian@atticf22 ~]$ echo "$VAR5 / $VAR6"

 / 

 [ian@atticf22 ~]$ VAR5=5 VAR6="some value" bash

 [ian@atticf22 ~]$ echo "$VAR5 / $VAR6"

 5 / some value 

 [ian@atticf22 ~]$ exit

 [ian@atticf22 ~]$ echo "$VAR5 / $VAR6"

readonly和其他變量屬性                                                                 上一頁       回頁首  

一些 shell 參數無法修改。還可以將變量限制爲 readonlyinteger或 string。可以使用 declare命令設置變量屬性。使用 -p選項顯示變量和各種屬性。瞭解 declare命令:

 info bash "Shell Builtin Commands" "Bash Builtins"  --index-search declare 

 help declare

5. 變量屬性

 [ian@atticf22 ~]$ declare -r rov1="this is readonly"

 [ian@atticf22 ~]$ rov="Who says it's read only?"

 [ian@atticf22 ~]$ readonly rov2="another constant value"

 [ian@atticf22 ~]$ rov2=3

 bash: rov2: readonly variable 

 [ian@atticf22 ~]$ UID=99

 bash: UID: readonly variable 

 [ian@atticf22 ~]$ declare -pr

 declare -r BASHOPTS="checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote: 

 force_fignore:histappend:interactive_comments:progcomp:promptvars:sourcepath"

 declare -ir BASHPID 

 declare -r BASH_COMPLETION_COMPAT_DIR="/etc/bash_completion.d"

 declare -ar BASH_VERSINFO='([0]="4" [1]="3" [2]="42" [3]="1" [4]="release" [5]= 

"x86_64-redhat-linux-gnu")'

 declare -ir EUID="1000"

 declare -ir PPID="12761"

 declare -r SHELLOPTS="braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor"

 declare -ir UID="1000"

 declare -r rov1="this is readonly"

 declare -r rov2="another constant value"

 [ian@atticf22 ~]$ help declare

 declare: declare [-aAfFgilnrtux] [-p] [name[=value] ...] 

    Set variable values and attributes. 

    

    Declare variables and give them attributes.  If no NAMEs are given, 

    display the attributes and values of all variables. 

    

    Options: 

      -f   restrict action or display to function names and definitions 

      -F   restrict display to function names only (plus line number and 

       source file when debugging) 

      -g   create global variables when used in a shell function; otherwise 

       ignored 

      -p   display the attributes and value of each NAME 

    

    Options which set attributes: 

      -a   to make NAMEs indexed arrays (if supported) 

      -A   to make NAMEs associative arrays (if supported) 

      -i   to make NAMEs have the `integer' attribute 

      -l   to convert NAMEs to lower case on assignment 

      -n   make NAME a reference to the variable named by its value 

      -r   to make NAMEs readonly 

      -t   to make NAMEs have the `trace' attribute 

      -u   to convert NAMEs to upper case on assignment 

      -x   to make NAMEs export 

    

    Using `+' instead of `-' turns off the given attribute. 

    

    Variables with the integer attribute have arithmetic evaluation (see 

    the `let' command) performed when the variable is assigned a value. 

    

    When used in a function, `declare' makes NAMEs local, as with the `local'

    command.  The `-g' option suppresses this behavior. 

    

    Exit Status: 

    Returns success unless an invalid option is supplied or a variable 


取消設置變量                                                                           上一頁       回頁首

使用 unset命令從 bash shell 刪除一個變量。使用 -v選項確保刪除了變量定義。函數可擁有與變量相同的名稱,刪除一個函數定義,可使用 -f。如果沒有 -f或 -v

6. 取消設置 bash 變量

 ian@atticf22 ~]$ bash

 [ian@atticf22 ~]$ VAR1=v1

 [ian@atticf22 ~]$ declare -i VAR2

 [ian@atticf22 ~]$ VAR2=3+4

 [ian@atticf22 ~]$ echo $VAR1 $VAR2

 v1 7 

 [ian@atticf22 ~]$ unset VAR1

 [ian@atticf22 ~]$ echo $VAR1 $VAR2

 7 

 [ian@atticf22 ~]$ unset -v VAR2

 [ian@atticf22 ~]$ echo $VAR1 $VAR2

 [ian@atticf22 ~]$ exit

 exit

注意,變量被定義爲 integer,向它賦的值會計算爲算術表達式。

默認情況下,bash 將未設置的變量視爲擁有空值。爲什麼取消設置一個變量,而不是向它分配一個空值?在 bash 和其他許多 shell 中,如果應用未定義的變量,可能會產生一個錯誤。可使用 set -u命令生成未定義變量的錯誤,使用 set +u禁用該警告。

7. 錯誤和取消設置變量

 [ian@atticf22 ~]$ bash

 [ian@atticf22 ~]$ set -u

 [ian@atticf22 ~]$ VAR1=var1

 [ian@atticf22 ~]$ echo $VAR1

 var1 

 [ian@atticf22 ~]$ unset VAR1

 [ian@atticf22 ~]$ echo $VAR1

 bash: VAR1: unbound variable 

 [ian@atticf22 ~]$ VAR1=

 [ian@atticf22 ~]$ echo $VAR1

 [ian@atticf22 ~]$ unset VAR1

 [ian@atticf22 ~]$ echo $VAR1

 bash: VAR1: unbound variable 

 [ian@atticf22 ~]$ unset -v VAR1

 [ian@atticf22 ~]$ set +u

 [ian@atticf22 ~]$ echo $VAR1

 [ian@atticf22 ~]$ exit

 exit

取消設置一個不存在的變量不會出錯,甚至在指定了 set -u時也是如此。

上一頁       回頁首

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