Linux Bash Shell學習(五):特殊檔案、別名、選項和參數

來源:互聯網
上載者:User

  本文也即《Learning the bash Shell》3rd Edition的第三章Customizing Your Environment之讀書筆記,但我們將不限於此。設定一個好的使用者環境,對我們開發是非常關鍵的。在這一章節中,從四個方面進行介紹:特別的檔案,別名,選項,參數。

特殊檔案

  在使用者目錄下有幾個特殊的檔案,可以用ls -a

來查看這些隱藏的系統檔案。.bash_profile只重要的檔案,經常用於設定使用者環境。這個檔案將在使用者login的時候執行。全域(所有使用者使用的)為/etc/profile。通長我們將新的配置附加在該檔案的末尾,修改後需要重新登入方才生效,或者使用source命令來執行這些特殊檔案,即source .bash_profile

  現在的linux一般都是使用.bash_profile,如果沒有系統會去找.bash_login,次去找.profile。另外還有.bashrc檔案,這實在調用subbash的時候系統執行的,也即是我們敲入bash

來開啟新的bash環境,這在實際中是很少用的。.bash_logout顧名思義,是在logout是調用。

別名

  在環境檔案中,會設定一些別名,當然也可以在正常的bash命令列使用。通過別名,我們可以用我們便於記憶或者經常使用的其他動作系統的命令來替代linux的命令,也可以將很長一串命令用比較簡單的單詞來替代。格式如下:

alias

name

=command

  請注意在“=”的前後是不允許留有空格的。例如用search來代替尋找命令grep,alias search=grep,如果command中是含有空格的,需要用引號來表示,例如alias ls=’ls –color’。當然我們需要注意不要將萬用字元這些特殊符號用於name。我們可以在command中放入一個已經alias的別名,這是允許的。bash是禁止loop的,如果碰到,它會停止進一步翻譯,例如上面的例子ls作為ls -color,而這兩個ls在邏輯上可以產生loop,但bash不會允許這種情況的發生。雖然這樣,我們要防止在多個alias上自己給自己設定環回設定,簡潔、清晰,是一個程式員的準則。

  別名只能用於命令列的開始,例如alias mywork=’cd /home/wei/project/mywork’,我們嵌入mywork,就可以直接進入指定的命令,但是我們不能alias mywork=/home/wei/project/mywork,然後cd mywork,別名是不會去解析不作為第一位的命令的。這種情況可以用export的方式來處理。

alias:例如當前alias的的列表

alias name

:例如給name的真正含義

unalias name

:取消該name的別名捆綁

選項

  我們可以通過set –o optionname

來開啟某個環境選項,用set +o optionname

來關閉它,注意是-表示on,+表示off,這是應為-比較容易按,而+需要按兩個鍵。可以用set -o來查看當前環境的開關情況,例如為了禁止CTRL-D引發logout,我們可以開啟ignoreeof,及set –o ignoreeof。我覺得有很多這樣的環境設定參數,但是其實很少使用,需要的時候可以通過man set來確定一下就可以。另外可能比較常用的是noclobber,禁止通過>的重新導向方式重寫一個已有的檔案。在bash 2.0,提供shopt來設定,也提供了一些來設定和unset的方式,但是我不認為需要去記憶這些,在實際應用上是很少使用的。這裡就不記錄。

參數設定

  相對環境的設定,參數的設定更有有意思,set或者shopt是實際上很少使用的,而使用者的環境變數設定,例如JAVA是lib或者bin的指定是通過.bash_profile來設定的。參數設定也就是varname

=value


,通常varname都是大寫,例如JAVADIR,我們查看一個varname,常用的方式就是echo,例如我們已經設定了TT=’test for it’,可以通過echo $TT或者echo “$TT”來查看。如果沒有,就給出空行。

單引號和雙引號

  我們來看看下面的例子:varname=alice;echo "$varname"和echo $varname,都顯示alice,而echo ‘$varname’則顯示$varname,這說明在雙引號下是解析參數的,而單引號則表示一串字元不解析裡面的內容。

在看一個例子varname=’four space    here’,如果echo "$varname",則顯示four space    here,如果echo $varname,等同於echo four space    here,顯示four space here。雙引號內,表示一個整體,作為一個word出現,如果我們echo "four space    here"也一樣在here之前有多個空格。

  在參數設定中分清單引號和雙引號是必須的。

內建參數

  Linux系統包含一些內建參數,我們可以通過修改這些參數來改變更配置置。在我們自訂的參數名中要避免與之重名。

下面是history相關的部分參數:

  • HISTCMD記錄單前是第N個命令,可以echo來查看一下。
  • HISTFILESIZE表示記錄在檔案中曆史命令的數目,也即檔案的最大行數;
  • HISTSIZE表示記錄在記憶體中最大的曆史命令的數目。
  • HISTIGNORE:表示不記錄某些命令,例如HISTIGNORE=l*:&,表示不記錄l開頭的命令,以及不記錄重複的命令(&表示重複命令)
  • HISTTIMEFORMAT:如果為null,則不顯示時間,我們可以為之設定時間格式,例如HISTTIMEFORMAT=”%y/%m/%d %T “,請注意這裡使用的是雙引號。

時間格式

Format

Replaced by

%a

The locale's abbreviated weekday name

%A

The locale's full weekday name

%b

The locale's abbreviated month name

%B

The locale's full month name

%c

The locale's appropriate date and time representation

%C

The century number (the year divided by 100 and truncated to an integer) as a decimal number [00-99]

%d

The day of the month as a decimal number [01-31]

%D

The date in American format; the same value as %m/%d/%y.

%e

The day of the month as a decimal number [1-31]; a single digit is preceded by a space

%h

The same as %b

%H

The hour (24-hour clock) as a decimal number [00-23]

%I

The hour (12-hour clock) as a decimal number [01-12]

%j

The day of the year as a decimal number [001-366]

%m

The month as a decimal number [01-12]

%M

The minute as a decimal number [00-59]

%n

A newline character

%p

The locale's equivalent of either a.m. or p.m

%r

The time in a.m. and p.m. notation; in the POSIX locale this is equivalent to %I:%M:%S %p

%R

The time in 24-hour notation (%H:%M)

%S

The second as a decimal number [00-61]

%t

A tab character

%T

The time (%H:%M:%S)

%u

The weekday as a decimal number [1-7], with 1 representing Monday

%U

The week number of the year (Sunday as the first day of the week) as a decimal number [00-53]

%V

The week number of the year (Monday as the first day of the week) as
a decimal number [01-53]; if the week containing 1 January has four or
more days in the new year, then it is considered week 1—otherwise, it
is the last week of the previous year, and the next week is week 1

%w

The weekday as a decimal number [0-6], with 0 representing Sunday

%W

The week number of the year (Monday as the first day of the week) as
a decimal number [00-53]; all days in a new year preceding the first
Monday are considered to be in week 0

%x

The locale's appropriate date representation

%X

The locale's appropriate time representation

%y

The year without century as a decimal number [00-99]

%Y

The year with century as a decimal number

%Z

The timezone name or abbreviation, or by nothing if no timezone information exists

%%

%

  書中還介紹了Mail相關的部分參數,但是實際上我們已經很少mail,而是使用想雷鳥,189郵箱這類郵件用戶端或者web mail,所以相關的參數也不再此摘錄。

  這裡介紹了一個通過“/”來見長命令分段的方式,例子如下:其實如果雙引號只要左引號,無右引號,也必須等右引號方才結束,下面的例子也可以不用雙引號

$echo “hello ,/

>my friend”

hello ,my friend

提示符

  在linux中,提示符通常為$,已經root使用者的#,這個和具體的linux版本有關係,我們也可以設定我們的提示符,例如加上使用者名稱,時間等等。在Linux中與4個提示符,PS1,PS2,PS3和PS4。PS1是預設的提示符。例如我們需要改為使用者名稱$的方式,可以重新設定PS1,即PS1="/u$",如果我們還希望加上時間,設為PS1="/u-/A$ "。

  PS2是命令尚未輸完,第二上的換行繼續的提示符,通常為>。例如我們上面介紹的分段方式。PS3和PS4用於編程和debug。在以後的章節中介紹。下表為提示符的設定內容。

Command

Meaning

/a

The ASCII bell character (007)

/A

The current time in 24-hour HH:MM format

/d

The date in "Weekday Month Day" format

/D {format

}

The format is passed to strftime(3) and the result is inserted
into the prompt string; an empty format results in a locale-specific time
representation; the braces are required

/e

The ASCII escape character (033)

/H

The hostname

/h

The hostname up to the first "."

/j

The number of jobs currently managed by the shell

/l

The basename of the shell's terminal device name

/n

A carriage return and line feed

/r

A carriage return

/s

The name of the shell

/T

The current time in 12-hour HH:MM:SS format

/t

The current time in HH:MM:SS format

/@

The current time in 12-hour a.m./p.m. format

/u

The username of the current user

/v

The version of bash

(e.g.,
2.00)

/V

The release of bash

; the version
and patchlevel (e.g., 2.00.0)

/w

The current working directory

/W

The basename of the current working directory

/#

The command number of the current command

/!

The history number of the current command

/$

If the effective UID is 0, print a #, otherwise print a
$

/nnn

Character code in octal

//

Print a backslash

/[

Begin a sequence of non-printing characters, such as terminal
control sequences

/]

End a sequence of non-printing
characters

  一般而言我們不會去修改他們,除非有特殊需求,所以沒有必要去記憶這些系統參數名,需要的時候再查。

待續……

 

相關連結:
我的Linux操作相關文章

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.