2019獨角獸企業重金招聘Python工程師標準>>>
一 shell介紹
shell 是一個命令解釋器。本質上是用戶與計算機之間的交互。
用戶把指令告訴shell,然后shell再傳輸給系統內核,接著內核再去支配計算機硬件去執行各種操作。
每個用戶都可以有自己特定的shell。
CentOS7默認shell為bash(Bourne Again Shell)。
還有zsh,ksh等。
[root@localhost ~]# yum list|grep zsh
zsh.x86_64 5.0.2-28.el7 base
zsh-html.x86_64 5.0.2-28.el7 base
?
二 命令歷史
執行過的命令都會記錄。這些命令保存在用戶的家目錄的.bash_history文件中。
注意: 只有當用戶退出當前shell時,在當前shell中運行的命令才會保存在.bash_history文件中。
?
運行命令
[root@localhost ~]# history
顯示命令歷史,結果如下:
可以記錄1000條命令歷史( 變量HISTSIZE)。
[root@localhost ~]# echo $HISTSIZE
1000
在/etc/profile中修改。
與命令歷史有關的一個特殊字符!
常見應用:
- !! :執行上一條指令。
[root@localhost ~]# !!
ls -l /etc/profile //上一條指令
-rw-r--r--. 1 root root 1795 11月 6 2016 /etc/profile
- !n : 執行命令歷史中的第n條指令
[root@localhost ~]# !723
ls /usr/local/apache2/
bin cgi-bin error icons lib man modules
build conf htdocs include logs manual
- !word(字符串 大于等于1):執行最近一次以word開頭的命令。
[root@localhost ~]# !723
ls /usr/local/apache2/
bin cgi-bin error icons lib man modules
build conf htdocs include logs manual
[root@localhost ~]# !ls
ls /usr/local/apache2/
bin cgi-bin error icons lib man modules
build conf htdocs include logs manual
?
三 通配符
?* ?匹配0個或多個字符。
? 匹配1個字符。
實例如下:
1 匹配*
[root@localhost /]# cd tmp
[root@localhost tmp]# ls
1 3.txt
11 systemd-private-4c298191af864cd1a3f133883d308ceb-vmtoolsd.service-xr6HDH
12.tar test
13.tar.gz test.zip
1.txt yum_save_tx.2018-01-09.22-27.7kaeYH.yumtx
1.txt.zip zsh-5.0.2-28.el7.x86_64.rpm
2.txt
[root@localhost tmp]# ls *.txt
1.txt 2.txt 3.txt
2 匹配?
[root@localhost tmp]# ls ?.txt
1.txt 2.txt 3.txt
[root@localhost tmp]# ls ?.zip
ls: 無法訪問?.zip: 沒有那個文件或目錄
3 [0-9] ?表示范圍,或的關系。 ?{1,2}或的關系。
[root@localhost tmp]# ls [0-9].txt
1.txt 2.txt 3.txt
[root@localhost tmp]# ls {1,2}.txt
1.txt 2.txt
?
四 輸入/輸出重定向
< ?輸入重定向
> 輸出重定向 ?將命令的結果輸入到文件中。
2> 錯誤重定向
>> 追加重定向
?
實例如下:
[root@localhost tmp]# echo "123">1.txt //輸出重定向
[root@localhost tmp]# echo "123">>1.txt //追加輸出
[root@localhost tmp]# cat 1.txt
123
123[root@localhost tmp]# wc -l < 1.txt //輸入重定向
2
[root@localhost tmp]#
?