1. 程式人生 > >8.6 管道符和作業控制;8.7—8.8 shell變量(上下);8.9 環境變量配置文件

8.6 管道符和作業控制;8.7—8.8 shell變量(上下);8.9 環境變量配置文件

install tree 配置 顯示 linu 執行命令 方括號 nvi 自定義環境

擴展

bashrc和bash_profile的區別

http://ask.apelearn.com/question/7719

簡易審計系統:

http://www.68idc.cn/help/server/linux/2014042190951.html

關於PROMPT_COMMAND環境變量的含義

http://www.linuxnote.org/prompt_command-environment-variables.html

8.6 管道符和作業控制

管道符作用:前面命令輸出結果 管道符傳給 後面命令執行

1. ls命令(列出當前目錄下所有文件目錄),管道符(傳給後面命令) wc -l命令(統計出有多少數量)

[root@hao-02 ~]#

ls |wc -l

2. 停止 任務: Ctrl + z

恢復 任務: fg

3. 列出 所有任務jobs

[root@hao-02 ~]# jobs

技術分享圖片技術分享圖片

4. 恢復 多個停止任務: fg 停止任務ID號

[root@hao-02 ~]# fg 2

技術分享圖片技術分享圖片

5. 任務 恢復到前臺 fg 任務ID號

[root@hao-02 ~]# fg 3

6. 任務 丟到後臺運行bg 任務ID號

[root@hao-02 ~]# bg 3

技術分享圖片技術分享圖片

7. 一次把任務(執行命令) 丟到後臺運行任務執行命令 &

[root@hao-02 ~]# sleep 200 &

技術分享圖片技術分享圖片

8.7 shell變量(上)

本地變量:

中文顯示變量:LANG=zh_CN.UTF-8

英文顯示變量:LANG=en

環境變量名規則:字母數字下劃線 (首位,不能為數字)

1. 自定義環境變量:自定義變量名=自定義值[root@hao-02 ~]# a=1

2. 自定義環境變量,定義值特殊,要用''單引號括起來: 自定義變量名='特殊的定義值'

[root@hao-02 ~]# b='1 2'

3. 變量疊加變量值,變量名,要用""雙引號括起來:

自定義變量名="$變量名a"附加值"$變量名b"

[root@hao-02 ~]#

c="$a"D"$b"

技術分享圖片技術分享圖片

4. 查看變量名,對應的值: echo $ 變量名

[root@hao-02 ~]# echo $c

技術分享圖片技術分享圖片

5. 系統環境變量:env

6. 包含用戶自定義環境變量: set

8.8 shell變量(下)

全局變量:向下子shell生效

安裝pstree命令:yum install -y psmisc

本地變量,向下子shell不會同步生效!

1. 查看當前在哪個shell下pstree

[root@hao-02 ~]# pstree

技術分享圖片技術分享圖片

2. 自定義本地環境變量: [root@hao-02 ~]# hao=linux

查看變量名,對應的值: [root@hao-02 ~]# echo $hao

進入一個子shell: [root@hao-02 ~]# bash

再次查看變量名,對應的值: [root@hao-02 ~]# echo $hao

結果:進入下一個子shell,再查看hao變量值,沒有同步!!!

技術分享圖片技術分享圖片

全局變量,向下子shell同步生效!

1. 查看當前在哪個shell下pstree

[root@hao-02 ~]# pstree

技術分享圖片技術分享圖片

2. 自定義全局環境變量: [root@hao-02 ~]# export hao=linux

進入一個子shell: [root@hao-02 ~]# bash

再次查看變量名,對應的值: [root@hao-02 ~]# echo $hao

結果:進入下一個子shell,再查看hao變量值,已同步!!!

技術分享圖片技術分享圖片

8.9 環境變量配置文件

系統環境變量配置文件分兩個維度系統層次用戶層次

系統層次:etc下面的文件(一般不編輯)

/etc/profile (用戶登錄的時候會加載到,)

/etc/bashrc (用戶或系統執行shell腳本時候,會用到這個文件)

用戶層次:用戶夾目錄下的文件(可編輯)

~/.bashrc

~/.bash_profile

~/.bash_history

~/.bash_logout (用戶退出時候做的一些操作)

Linux環境變量之“PS1"

http://ask.apelearn.com/question/5364

1. 查看PS1環境變量值

[root@hao-01 ~]#echo $PS1

技術分享圖片技術分享圖片

2. 更改PS1變量值,顯示絕對路徑

[root@hao-01 ~]# PS1='[\u@\h \w]\$'

技術分享圖片技術分享圖片

3. 更改PS1變量值,變顏色

[root@hao-01 ~]# PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$'

技術分享圖片技術分享圖片

4. 更改PS1變量值,取消方括號,或者換成其他符號

[root@hao-01 ~]# PS1='{\u@\h \W}\$'

技術分享圖片技術分享圖片

1. 查看PS2環境變量值echo $PS2

技術分享圖片技術分享圖片

2. PS2的顯示格式:

[root@hao-01 mulu4]#for i in `seq 1 10`

技術分享圖片技術分享圖片

8.6 管道符和作業控制;8.7—8.8 shell變量(上下);8.9 環境變量配置文件