linux自定义和使用 shell 环境(一)
概述
? 學(xué)習(xí)自定義?Linux?bash?shell?環(huán)境來滿足用戶需求。學(xué)習(xí):
·?修改全局和用戶配置文件
·?在登錄或生成新?shell?時設(shè)置環(huán)境變量
·?為常用命令序列創(chuàng)建?bash?函數(shù)
·?為新用戶帳戶維護框架目錄
·?設(shè)置命令搜索路徑
Linux?shell
? ? ? 在終端上使用一個?Linux?shell?程序,通過鍵入命令(輸入流)來與系統(tǒng)交互,并在同一個終端上查看輸出(輸出流)和錯誤消息(錯誤流)。有時,您需要在系統(tǒng)引導(dǎo)以前運行命令,以便允許終端建立連接,有時您需要定期運行命令,無論您是否登錄。shell?也可以為您完成這些任務(wù)。標(biāo)準(zhǔn)輸入和輸出流不需要來自或定向到終端上的真實用戶。在本教程中,將進一步了解?shell?和自定義用戶的環(huán)境。具體地講,您將學(xué)習(xí)?bash?(Bourne?again)?shell(原始?original?Bourne?shell?的增強版),并了解使?bash?更符合可移植操作系統(tǒng)接口?(Portable?Operating?System?Interface,?POSIX)?標(biāo)準(zhǔn)的更改。我會在此過程中介紹其他一些?shell?特性。
Shell?和環(huán)境
? ? shell 與操作系統(tǒng)提供了一個層。借助?Linux(和?UNIX)shell,組合基本函數(shù)來構(gòu)建復(fù)雜的操作。然后可以使用編程結(jié)構(gòu),構(gòu)建能在?shell?中直接執(zhí)行的函數(shù)或?qū)⒑瘮?shù)保存為?shell?腳本。shell?腳本是一個命令序列,它存儲在?shell 運行的文件中。
表?1.?常見?bash?環(huán)境變量
名稱 | 用途 |
USER | 登錄用戶的名稱 |
UID | 登錄用戶的用戶?ID?數(shù)字 |
HOME | 用戶的主目錄 |
PWD | 當(dāng)前工作目錄 |
SHELL | shell?的名稱 |
PPID | 父進程的?PID?—啟動此進程的進程的進程?ID |
shell?還會設(shè)置一些特殊參數(shù),但這些參數(shù)無法修改。
表?2.?常見?bash?參數(shù)
名稱 | 用途 |
$ | (運行的?bash?shell?[?或其他?]?進程)的進程?ID(或?PID) |
? | 上一個命令的退出代碼 |
0 | shell?或?shell?腳本的名稱 |
使用變量
在名稱前面加上?$作為前綴來使用變量的值,如1所示。
1.?使用變量值
?[ian@atticf22?~]$?echo?$UID?1000?
?[ian@atticf22?~]$?echo?$HOME?/home/ian
設(shè)置變量值并讓它們可用
? ? 通過鍵入一個名稱后不留空格,立即鍵入等號?(=),在?bash?shell?中創(chuàng)建或設(shè)置?shell?變量。變量名是僅由字母數(shù)字字符和下劃線組成的單詞。名稱以字母字符或下劃線開頭。變量名是區(qū)分大小寫的,所以?var1和?VAR1是不同的變量。變量名(特別是導(dǎo)出變量)通常采用大寫形式,就像?表?1中的示例一樣,但這是一種約定,而不是一種要求。一些變量(比如?$$和?$?)實際上是?shell?參數(shù)而不是變量?—只能引用它們,不能向它們賦值。
Shell?變量僅對您創(chuàng)建它們時所在的進程可見,除非?導(dǎo)出它們,以便子進程可以看到和使用它們。子進程不能將變量導(dǎo)出到父進程。可以使用export命令導(dǎo)出變量。在?bash?shell?中,可以一步完成分配和導(dǎo)出,但不是所有?shell?都支持這項功能。
? ? 探索這些概念的一種好方法是使用另一個?shell?來創(chuàng)建子進程。可以使用?ps命令幫助跟蹤您所在的位置和正在運行的進程。
2.?設(shè)置?shell?變量并將它們導(dǎo)出到環(huán)境
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?沒有設(shè)置?SHELL變量?但在您登錄時,或者使用?su命令和創(chuàng)建?登錄?shell的選項來切換到另一個用戶時,通常會設(shè)置此變量。
可使用?echo命令查看?表?1和?表?2中的一些常見?bash?變量
3.?常見環(huán)境變量和?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?中,還可以通過在命令前加上名稱?=?值對作為前綴,將環(huán)境值設(shè)置為在單個命令的持續(xù)時間內(nèi)有效
4.?為單個命令設(shè)置?bash?環(huán)境值?
?[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?參數(shù)無法修改。還可以將變量限制為?readonly、integer或?string。可以使用?declare命令設(shè)置變量屬性。使用?-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?
取消設(shè)置變量 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??上一頁? ? ? ?回頁首
使用?unset命令從?bash?shell?刪除一個變量。使用?-v選項確保刪除了變量定義。函數(shù)可擁有與變量相同的名稱,刪除一個函數(shù)定義,可使用?-f。如果沒有?-f或?-v。
6.?取消設(shè)置?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,向它賦的值會計算為算術(shù)表達式。
默認情況下,bash?將未設(shè)置的變量視為擁有空值。為什么取消設(shè)置一個變量,而不是向它分配一個空值?在?bash?和其他許多?shell?中,如果應(yīng)用未定義的變量,可能會產(chǎn)生一個錯誤。可使用?set?-u命令生成未定義變量的錯誤,使用?set?+u禁用該警告。
7.?錯誤和取消設(shè)置變量
?[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
取消設(shè)置一個不存在的變量不會出錯,甚至在指定了?set?-u時也是如此。
上一頁? ? ? ?回頁首
轉(zhuǎn)載于:https://blog.51cto.com/risingair/1868669
總結(jié)
以上是生活随笔為你收集整理的linux自定义和使用 shell 环境(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 动画框架详解,第 1 部
- 下一篇: 抽象类与接口 day-11.2