[toc]
shell是一個指令解釋器,提供使用者和機器之間的互動(shell腳本是shell的一種表現)
支援特定文法,比如邏輯判斷、循環 (if,for,while)
每個使用者都可以有自己特定的shell
root:x:0:0:root:/root:/bin/bash
centos7預設shell為bash(bourne agin shell)
還有zsh、ksh等
history指令
<code>.bash_history</code> 存放指令記錄的檔案,如果終端非exit,logout正常退出,指令會記錄不全。
最大1000條,預設1000條,可修改
變量<code>histsize</code>, 檢視 <code>echo $histsize</code>如果顯示超過1000,是因為指令暫時存在記憶體中,還沒寫進檔案,<code>history-c</code>可清空記憶體裡記錄,但是不會清除<code>.bash_history</code>裡的記錄
<code>/etc/profile</code>中修改,要修改後立即生效重新進終端或執行 <code>source /etc/profile</code>
<code>histtimeformat="%y/%m/%d %h:%m:%s "</code> 增加此變量,可記錄對應指令的執行時間, 例: <code>1005 2019/11/12 10:29:47 w</code>
永久儲存 <code>chattr +a ~/.bash_history</code> 即使超過1000條也沒有權限删除
!! 上一條指令
!n n是數字,執行history記錄裡對應編号的指令
!word
tab鍵,敲一下,敲兩下
centos7支援參數補全,安裝<code>yum install -y bash-completion</code> 需要重新開機系統生效 <code>systemctl restart network</code>
alias别名給指令重新起個名字 <code>alias restartnet="systemctl restart network"</code> 取消alias:<code>unalias restartnet</code>
各使用者都有自己配置别名的檔案 <code>~/.bashrc</code>
ls /etc/profile.d/
自定義的alias放到<code>~/.bashrc</code>
ls *.txt
ls ?.txt
ls [0-9].txt
ls {1,2}.txt
cat 1.txt >2.txt
cat 1.txt >> 2.txt
ls aaa.txt 2>err //2>表示錯誤重定向
ls aaa.txt 2>>err //2>>表示錯誤追加重定向
&> 結合正确和錯誤的重定向
wc -l < 1.txt
command >1.txt 2>&1
cat 1.txt |wc -l ; cat 1.txt |grep 'aaa'
把前的結果交給後面的指令
ctrl z 暫停一個任務
jobs檢視背景的任務
bg[id]把任務調到背景
fg[id]把任務調到前台
指令後面加&直接丢到背景 <code>sleep 100 &</code>
path,home,pwd,logname 常風的一些系統變量
env指令,可以檢視系統常用變量,系統變量名通常是大寫,變量值可以是數值,也可以是字元串
set指令多了很多變量,并且包括使用者自定義的變量
自定義變量a=1
變量名規則:字母、數字下劃線,首位不能為數字,可以a1=4 a_1=4 但是不可以1a=4
變量值有特殊符号時需要用單引号括起來
<code>a='a b c'</code> 裡面帶空格要用單引号括起來
變量的累加,變量累加時要用雙引号
全局變量export b=2
unset變量,取消指派, <code>unset b</code> unset 變量名
系統層面的環境變量,etc下的配置檔案,全局生效
<code>/etc/profile</code> 使用者環境變量,互動,登入才執行,輸入使用者名,ip,port,密碼就會自動加載,然後profile又會自動的調用bashrc。
<code>/etc/bashrc</code> 使用者不用登入,執行shell就生效,隻要執行shell腳本,就會調用bashrc裡面的配置
使用者層面的環境變量,在各個使用者的家目錄下
<code>~/.bashrc</code>
<code>~/.bash_profile</code> . .bash_profile或者source .bash_profile
<code>~/.bash_history</code>
<code>~/.bash_logout</code> 定義使用者退出時需要做出的一些動作,比如:退出時删除指令曆史,就可把删除曆史指令的指令,放在.bash_logout裡面
<code>ps1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '</code>
<code>vim /etc/bashrc</code> 裡定義
<code>* 任意個任意字元</code>
<code>? 任意一個字元</code>
<code># 注釋字元</code>
<code>\ 脫義字元</code> 要使c='$a$b'的結果為$a$b除了加單引号,還可以使用脫意符c=\$a\$b。
<code>| 管道符</code>
<code>cut 分割</code>,-d 分隔符 -f 指定段号 -c 指定第幾個字元
示範:
<code>sort 排序</code>, -n 以數字排序 -r 反序 -t 分隔符 -kn1/-kn1,n2
<code>wc -l 統計行數</code> -m 統計字元數 -w 統計詞
示範
[root@localhost ~]# cat 1.txt
123
abc ,f23
[root@localhost ~]# wc -w 1.txt 統計多少個詞,以空格區分
3 1.txt
<code>uniq 去重</code>, -c統計行數
[root@localhost ~]# sort 1.txt |uniq 去重首先要排序,uniq通常要與sort一起使用
1
2
abc
[root@localhost ~]# sort 1.txt |uniq -c 先排序再去重再統計行數
2 1
2 123
1 2
1 abc
1 abc ,f23
<code>tee 和 &gt; 類似</code>,重定向的同時還在螢幕顯示
[root@localhost ~]# sort 1.txt |uniq -c |tee a.txt |tee的作用相當于>,但是它會把重定向的内容顯示在屏螢幕上
[root@localhost ~]# sort 1.txt |uniq -c |tee -a a.txt |tee -a 相當于>>
[root@localhost ~]# cat a.txt
<code>tr 替換字元</code>,tr 'a' 'b',大小寫替換tr '[a-z]' '[a-z]'
<code>split 切割</code>,-b大小(預設機關位元組),-l行數, -d添加數字字尾
$ 變量字首,!$組合,正則裡面表示行尾
;多條指令寫到一行,用分号分割
~ 使用者家目錄,後面正規表達式表示比對符
& 放到指令後面,會把指令丢到背景
<code>&gt; &gt;&gt; 2&gt; 2&gt;&gt; &&gt;</code>
<code>[ ]</code> 指定字元中的一個,[0-9],[a-za-z],[abc]
|| 和 && ,用于指令之間
[root@localhost test]# ls 2a.txt && wc -l 2.txt
ls: 無法通路2a.txt: 沒有那個檔案或目錄
[root@localhost test]# ls 2.txt && wc -l 2.txt
2.txt
77517 2.txt
前面執行不成功,後面就不會去執行,前面執行成功,後面才會去執行
[root@localhost test]# [ -d aminglinux ] || mkdir aminglinux
[root@localhost test]# ls
201911.aa 201911.ac 201911.ae 201911.ag 2.txt
201911.ab 201911.ad 201911.af 201911.ah aminglinux
[root@localhost test]# [ -d aminglinux ] && mkdir aminglinux
mkdir: 無法建立目錄"aminglinux": 檔案已存在