UNIX 環境

Unix的一個重要概念是環境,被定義的環境變量。有些系統通過環境變量,還有一些由shell,或任何程序加載另一個程序。

變量是一個字符串,我們分配一個值。分配的值可以是一個數字,文本,文件名,移動設備,或任何其他類型的數據。

例如,首先我們設定一個變量測試,然後我們使用echo命令來訪問它的值:

$TEST="Unix Programming" $echo $TEST Unix Programming

需要注意的是環境變量設置,而無需使用$符號,但訪問它們時,我們使用$符號作爲前綴。這些變量保持它們的值,直到我們設計出來shell。

當你登錄到系統中,shell經過一個階段稱爲初始化設置各種環境。這通常是一個兩步的過程,涉及的的shell閱讀下列文件:

  • /etc/profile

  • profile

過程如下:

  1. shell進行檢查,看是否存在文件 /etc/profile文件。

  2. 如果它存在,當shell讀取。否則,此文件將被跳過。不顯示錯誤消息。

  3. shell檢查,看看是否該文件。配置文件存在於你的home目錄。主目錄是開始在登錄後的目錄

  4. 如果它存在,當shell讀取它,否則shell跳過。不顯示錯誤消息。

只要這兩個文件被讀取,shell將顯示一個提示:

$

這是提示這裏你可以輸入命令,以讓他們執行。

注 - 這裏詳述shell初始化過程適用於所有的Bourne型shell,但所使用的是bash和ksh一些額外的文件。

.profile 文件:

文件/etc/profile文件是由UNIX機器的系統管理員,包含shell初始化所需的信息系統上的所有用戶。

該文件 .profile是在你的控制之下。您可以添加儘可能多的 shell定製信息,只要想這個文件。最小信息集,需要配置包括:

  • 使用的終端的類型

  • 在其中定位命令的目錄的列表

  • 列表變量,影響終端的外觀和風格。

可以檢查 .profile,在home目錄。使用vi編輯器打開它,並檢查所有的變量設置環境。 

設置終端類型:

通常情況下,使用的終端自動配置通過登錄或getty程序。有時,自動配置過程中猜測終端不正確。

如果終端設置不正確,命令的輸出可能看起來很奇怪,或者可能無法正確與shell交互。

爲了確保,這是沒有的情況下,大多數用戶他們的終端設置到最低共同如下:

$TERM=vt100
$

設置路徑:

當輸入任何命令在命令提示符下,shell也有定位纔可以執行的命令。

PATH變量中指定位置的的shell看起來應該命令。一般它被設置如下:

$PATH=/bin:/usr/bin
$

這裏每一個由冒號分開的各個條目,目錄。如果要求的shell來執行命令並不能找到它在PATH變量中的任何目錄中,出現類似下面的消息:

$hello
hello: not found
$

有變量,如PS1和PS2在下一節討論。

PS1和PS2變量:

命令提示符下的shell顯示爲字符存儲在變量PS1。可以改變這個變量是你想要的任何東西。只要改變它,它會被用來由shell,從這一點上。

例如,如果發出命令:

$PS1='=>' => => =>

將成爲提示=>。要設置PS1的價值,因此,它顯示的工作目錄,發出以下命令:

=>PS1="[u@h w]$" [root@ip-72-167-112-17 /var/www/yiibai/unix]$ [root@ip-72-167-112-17 /var/www/yiibai/unix]$

此命令的結果是,提示顯示用戶的用戶名,機器名(hostname),工作目錄。

有相當多的PS1的值參數可以用來作爲轉義序列,試圖限制了,提示沒有太多的信息。

Escape Sequence

描述

Current time, expressed as HH:MM:SS.

d

Current date, expressed as Weekday Month Date

Newline.

s

Current shell environment.

W

Working directory.

w

Full path of the working directory.

u

Current user.s username.

h

Hostname of the current machine.

#

Command number of the current command. Increases with each new command entered.

$

If the effective UID is 0 (that is, if you are logged in as root), end the prompt with the # character; otherwise, use the $.

可以使自己的變化,每次登錄時,或者可以將它添加到您的配置文件中所做的更改會自動在PS1。

當你發出一個命令,是不完整的,shell將顯示輔助提示,等待完成該命令,然後再次按Enter鍵。

默認的次級提示>(大於號),但可以改變重新定義PS2 shell變量:

下面的例子使用默認的次級提示:

$ echo "this is a

test" this is a
test
$

下面的例子重新定義PS2定製提示:

$ PS2="secondary prompt->" $ echo "this is a
secondary prompt->test" this is a
test
$

環境變量:

以下是部分重要的環境變量列表。如上所述,這些變量將被設置和訪問:

變量

描述

DISPLAY

Contains the identifier for the display that X11 programs should use by default.

HOME

Indicates the home directory of the current user: the default argument for the cd built-in command.

IFS

Indicates the Internal Field Separator that is used by the parser for word splitting after expansion.

LANG

LANG expands to the default system locale; LC_ALL can be used to override this. For example, if its value is pt_BR, then the language is set to (Brazilian) Portuguese and the locale to Brazil.

LD_LIBRARY_PATH

On many Unix systems with a dynamic linker, contains a colon-separated list of directories that the dynamic linker should search for shared objects when building a process image after exec, before searching in any other directories.

PATH

Indicates search path for commands. It is a colon-separated list of directories in which the shell looks for commands.

PWD

Indicates the current working directory as set by the cd command.

RANDOM

Generates a random integer between 0 and 32,767 each time it is referenced.

SHLVL

Increments by one each time an instance of bash is started. This variable is useful for determining whether the built-in exit command ends the current session.

TERM

Refers to the display type

TZ

Refers to Time zone. It can take values like GMT, AST, etc.

UID

Expands to the numeric user ID of the current user, initialized at shell startup.

以下是幾個環境變量的樣本示例:

$ echo $HOME /root ]$ echo $DISPLAY

$ echo $TERM
xterm
$ echo $PATH /usr/local/bin:/bin:/usr/bin:/home/amrood/bin:/usr/local/bin
$