標籤:linux shell基礎
1. shell 特性:
使用 history 命令查看命令曆史:
[[email protected] ~]# history
使用者的 history 記錄位於使用者家目錄下 .bash_history 檔案中:
[[email protected] ~]# vim ~/.bash_history
有關 history 的相關快捷操作:
[[email protected] ~]# !! //上一條命令
[[email protected] ~]# !$ //上一條命令的最後一個參數
[[email protected] ~]# !n //執行 history 中的第 n 條記錄
[[email protected] ~]# !字元 //執行 history 中以指定字元開頭的最近一條命令
萬用字元: * 匹配 0 個或多個字元, ? 匹配一個字元。
作業控制: ctrl+ z 將進程放入後台, jobs 查看全部後台進程, fg %n(數字)將後台進程號為 n 的進程放回前台, bg %n 將前台進程號為 n 的進程放入後台, kill %n 將後台進程號為 n 的進程殺死,& 放在命令後表示將進程放入後台執行:
[[email protected] ~]# sleep 3000 &
2.變數:
系統變數都是大寫。
通過 env 可以列出目前使用者的所有環境變數以及使用者自訂的全域變數:
[[email protected] ~]# env
set 命令可以把所有變數列出來,包括 env 列出的所有變數和當前shell 的自訂局部變數(使用 export 命令可以聲明全域變數:export myvar=”test” ,不使用 export 命令聲明的則是當前shell 的局部變數 myvar=”test”):
[[email protected] ~]# set
假如現在當前 shell 定義了一個全域變數export myvar=”test”,現在想取消這個全域變數,使用 unset 命令:
[[email protected] ~]# unset myvar
不加任何參數的 export 命令表示聲明所有環境變數以及使用者自訂變數:
[[email protected] ~]# export
3.系統內容變數和個人環境變數設定檔:
/etc/profile :PATH USER LOGNAME MAIL INPUTRC HOSTNAME HISTSIZE umask 等環境變數在該設定檔中設定。
/etc/bashrc : $PS1 umask 等環境變數在該設定檔中設定。
~/.bash_profile : 使用者私人的環境變數在此設定檔中設定。
~/.bashrc : 使用者私人的環境變數在此設定檔中設定。
簡單來說,profile 和 bashrc 的聯絡和區別如下:
(1)在使用者登入時,profile 設定檔(包括 /etc/profile 和 ~/.bash_profile)執行(source /etc/profile;source~/.bash_profile),而 profile 設定檔的執行會導致bashrc 設定檔(包括 /etc/bashrc 和 ~/.bashrc)執行(source /etc/bashrc;source~/.bashrc)。
(2)使用者在登入狀態下開啟一個新 shell 時,bashrc 設定檔執行,而 profile 設定檔不執行。
(3)如果要修改環境變數,最好在 profile 設定檔中進行修改。
更多不同可以參考如下文章:
http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html
4.shell 中的特殊符號:
* 匹配符號,匹配 0 個或多個任一字元。
? 匹配符號,匹配 1 個任一字元
# 注釋符號,其後面一整行的內容為注釋資訊
\ 脫意符號,將特殊字元還原為一般字元
| 管道符號
[] 代表所包含字元中的任意一個:
[[email protected]~]# ls [ad]bc.log
abc.log dbc.log
5.常用命令:
5.1 cut:
切分顯示。-d 指定分隔字元,和-f 配合使用, -f 指定所要顯示哪些列:
[[email protected] ~]# cut -d ‘:’ -f 1,3 /etc/passwd
[[email protected] ~]# cut -d ‘:’ -f 1-3 /etc/passwd
-c 指定每行從開始顯示的字元數:
[[email protected] ~]# cut -c 1,3 /etc/passwd
[[email protected] ~]# cut -c 1-3 /etc/passwd
5.2 sort:
排序。-t 指定分隔字元,和 -k 配合使用, -k 指定按分隔後的哪一些進行排序。預設情況下是根據 ASCII 碼值由小到大排序,使用 -n 參數可以按數字大小排序,-r 參數逆序,即由大到小排序:
[[email protected] ~]# sort -t ‘:’ -k 3 -nr /etc/passwd
5.3 uniq:
去除重複行。-c 參數統計同樣內容的行的數量:
[[email protected] ~]# … | uniq -c
5.4 tee:
類似輸出重新導向,但比輸出重新導向多出一個功能,即可以在標準輸出上顯示。可以簡單理解為 cat + > 。
[[email protected] ~]# cat /etc/passwd | tee /tmp/passwd.bak
5.5 wc:
統計檔案行數、字元數、詞數:
[[email protected] ~]# wc /etc/passwd
-l 參數只統計行數,-w 參數只統計詞數, -m 參數只統計字元數(會比實際字元數多出一部分,因為每行行尾其實都有一個$ 符號結尾):
[[email protected] ~]# wc -l /etc/passwd
5.6 tr:
tr 用於替換,實際工作中使用並不多,可以參考之前的一篇部落格,地址如下:
http://xitongjiagoushi.blog.51cto.com/9975742/1621301
5.7 split:
用於將一個大檔案按要求分割為多個小檔案,先人工產生一個比較大的檔案:
[[email protected] ~]# for i in `seq 1 10000` cat /etc/passwd >> bigfile.log done;
-l 參數指定以多少行為一個子檔案進行拆分,-b 參數指定以多大的檔案大小為一個子檔案進行拆分:
[[email protected] ~]# split -l 1000 bigfile.log
[[email protected] ~]# split -b 1M bigfile.log
參考資料:
http://www.aminglinux.com/bbs/thread-7717-1-1.html
本文出自 “細桶假狗屎” 部落格,請務必保留此出處http://xitongjiagoushi.blog.51cto.com/9975742/1632780
學習筆記(十一)——Linux Shell 基礎知識