Shell基礎知識

Shell介紹

Shell是一個命令解釋器,提供用戶和機器之間的交互,支持特定語法,好比邏輯判斷、循環。每一個用戶均可以有本身特定的Shell。CentOS 7默認shell爲bash(Bourne Again Shell,/bin/bash),它是sh(Bourne Shell)的加強版本,此外還有zsh、ksh等。html

[root@centos-01 ~]# yum list |grep zsh
autojump-zsh.noarch                       22.3.0-3.el7                 epel     
zsh.x86_64                                5.0.2-28.el7                 base     
zsh-html.x86_64                           5.0.2-28.el7                 base     
zsh-lovers.noarch                         0.9.0-1.el7                  epel     
[root@centos-01 ~]# yum list |grep ksh
ksh.x86_64                                20120801-35.el7_4            updates  
mksh.x86_64                               46-5.el7                     base     
python-XStatic-Rickshaw.noarch            1.5.0.0-4.el7                epel     
python-moksha-common.noarch               1.2.3-2.el7                  epel     
python-moksha-wsgi.noarch                 1.2.2-2.el7                  epel     
python2-moksha-hub.noarch                 1.5.5-2.el7                  epel

歷史命令

http://www.javashuo.com/article/p-vkmrthvl-bu.htmlpython

命令補全和別名

https://blog.csdn.net/Noob_f/article/details/80384966正則表達式

通配符

  • *匹配零個或多個字符
[root@centos-01 ~]# ls
anaconda-ks.cfg
[root@centos-01 ~]# cd /tmp/
[root@centos-01 tmp]# ls
1.txt
2.txt.gz
3.txt.bz2
4.txt.xz
mytest
systemd-private-59dabe73e76f4c919c49080f12695d35-chronyd.service-ocfQwu
systemd-private-59dabe73e76f4c919c49080f12695d35-vgauthd.service-NyBxv0
systemd-private-59dabe73e76f4c919c49080f12695d35-vmtoolsd.service-pdvc96
vim-enhanced-7.4.160-2.el7.x86_64.rpm
yum_save_tx.2018-05-05.19-44.not9LG.yumtx
yum_save_tx.2018-05-05.21-20.Iue83X.yumtx
[root@centos-01 tmp]# ls *.txt
1.txt
[root@centos-01 tmp]# ls *txt*
1.txt  2.txt.gz  3.txt.bz2  4.txt.xz
[root@centos-01 tmp]# ls 1*
1.txt
  • ?匹配一個字符
[root@centos-01 tmp]# ls ?.txt
1.txt
[root@centos-01 tmp]# touch abc.txt
[root@centos-01 tmp]# ls
1.txt      systemd-private-59dabe73e76f4c919c49080f12695d35-chronyd.service-ocfQwu
2.txt.gz   systemd-private-59dabe73e76f4c919c49080f12695d35-vgauthd.service-NyBxv0
3.txt.bz2  systemd-private-59dabe73e76f4c919c49080f12695d35-vmtoolsd.service-pdvc96
4.txt.xz   vim-enhanced-7.4.160-2.el7.x86_64.rpm
abc.txt    yum_save_tx.2018-05-05.19-44.not9LG.yumtx
mytest     yum_save_tx.2018-05-05.21-20.Iue83X.yumtx
[root@centos-01 tmp]# ls ?.txt
1.txt
[root@centos-01 tmp]# cd mytest/
[root@centos-01 mytest]# ls
1.txt  2.txt.zip  4.txt  yourtest      yourtest.tar.bz2  yourtest.tar.xz
2.txt  3.txt      test   yourtest.tar  yourtest.tar.gz   yourtest.zip
[root@centos-01 mytest]# ls [0-3].txt
1.txt  2.txt  3.txt
[root@centos-01 mytest]# ls [2-3].txt
2.txt  3.txt
[root@centos-01 mytest]# ls [13].txt
1.txt  3.txt
[root@centos-01 mytest]# touch abc.txt
[root@centos-01 mytest]# ls [0-9a-zA-Z].txt
1.txt  2.txt  3.txt  4.txt
[root@centos-01 mytest]# touch d.txt
[root@centos-01 mytest]# ls [0-9a-zA-Z].txt
1.txt  2.txt  3.txt  4.txt  d.txt
[root@centos-01 mytest]# ls {1,2}.txt
1.txt  2.txt

輸入輸出重定向

覆蓋輸出>、追加輸出>>、錯誤重定向2>、錯誤追加劇定向2>>shell

[root@centos-01 ~]# cd /tmp/mytest/
[root@centos-01 mytest]# ls
1.txt  2.txt.zip  4.txt    d.txt  yourtest      yourtest.tar.bz2  yourtest.tar.xz
2.txt  3.txt      abc.txt  test   yourtest.tar  yourtest.tar.gz   yourtest.zip
[root@centos-01 mytest]# ls abc.txt 
abc.txt
[root@centos-01 mytest]# ls abc.txt > a.txt
[root@centos-01 mytest]# cat a.txt 
abc.txt
[root@centos-01 mytest]# lss abc.txt 2> a.txt
[root@centos-01 mytest]# cat a.txt 
-bash: lss: 未找到命令

>+2>==&>vim

[root@centos-01 mytest]# ls [12].txt ab.txt
ls: 沒法訪問ab.txt: 沒有那個文件或目錄
1.txt  2.txt
[root@centos-01 mytest]# ls [12].txt ab.txt > a.txt 
ls: 沒法訪問ab.txt: 沒有那個文件或目錄
[root@centos-01 mytest]# cat a.txt 
1.txt
2.txt
[root@centos-01 mytest]# ls [12].txt ab.txt 2> a.txt 
1.txt  2.txt
[root@centos-01 mytest]# cat a.txt 
ls: 沒法訪問ab.txt: 沒有那個文件或目錄
[root@centos-01 mytest]# ls [12].txt ab.txt > 1.txt 2>a.txt
[root@centos-01 mytest]# cat 1.txt 
1.txt
2.txt
[root@centos-01 mytest]# cat a.txt 
ls: 沒法訪問ab.txt: 沒有那個文件或目錄
[root@centos-01 mytest]# ls [12].txt ab.txt &> a.txt 
[root@centos-01 mytest]# cat a.txt 
ls: 沒法訪問ab.txt: 沒有那個文件或目錄
1.txt
2.txt

<輸入重定向centos

[root@centos-01 mytest]# wc -l < 1.txt 
2

管道符

管道符|,把前面的命令運行結果丟給後面的命令ruby

[root@centos-01 ~]# cat /tmp/mytest/1.txt | wc -l
2
[root@centos-01 ~]# ls /tmp/mytest/
1.txt  2.txt.zip  4.txt    a.txt  test      yourtest.tar      yourtest.tar.gz  yourtest.zip
2.txt  3.txt      abc.txt  d.txt  yourtest  yourtest.tar.bz2  yourtest.tar.xz
[root@centos-01 ~]# ls /tmp/mytest/ |wc -l
15

做業控制

當運行一個進程時,可使它暫停(按Ctrl+z),而後使用fg命令恢復它,利用bg命令使它到後臺運行,也可使它終止(按Ctrl+c)。jobs命令能夠看到在被暫停或者在後臺運行的任務。bash

bg [id]把任務調到後臺,fg [id]把任務調到前臺,命令後面加&直接丟到後臺。spa

[root@centos-01 ~]# vim /tmp/mytest/1.txt 

[1]+  已中止               vim /tmp/mytest/1.txt
[root@centos-01 ~]# fg
vim /tmp/mytest/1.txt

[1]+  已中止               vim /tmp/mytest/1.txt
[root@centos-01 ~]# vim /tmp/mytest/2.txt

[2]+  已中止               vim /tmp/mytest/2.txt
[root@centos-01 ~]# jobs
[1]-  已中止               vim /tmp/mytest/1.txt
[2]+  已中止               vim /tmp/mytest/2.txt
[root@centos-01 ~]# fg 2
vim /tmp/mytest/2.txt

[2]+  已中止               vim /tmp/mytest/2.txt
[root@centos-01 ~]# bg 2
[2]+ vim /tmp/mytest/2.txt &
[root@centos-01 ~]# jobs
[1]-  已中止               vim /tmp/mytest/1.txt
[2]+  已中止               vim /tmp/mytest/2.txt
[root@centos-01 ~]# vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 726156   2076 144068    0    0    79     5   81  111  0  1 98  2  0
 0  0      0 726156   2076 144100    0    0     0     0   47   43  0  1 99  0  0
^Z
[3]+  已中止               vmstat 1
[root@centos-01 ~]# bg 3
[3]+ vmstat 1 &
[root@centos-01 ~]# 2 0 0 726156 2076 144100 0 0 0 1 274 260 0 0 100 0 0
 0  0      0 726156   2076 144100    0    0     0     0   44   44  0  1 99  0  0
 0  0      0 726156   2076 144100    0    0     0     0   41   35  0  0 100  0  0
 0  0      0 726156   2076 144100    0    0     0     0   40   35  0  0 100  0  0
fg 0  0      0 726032   2076 144100    0    0     0     0   52   46  0  0 100  0  0
 3
vmstat 1
 0  0      0 726032   2076 144100    0    0     0     0   60   60  0  0 100  0  0
 0  0      0 726032   2076 144100    0    0     0     0   34   32  0  0 100  0  0
^Z
[3]+  已中止               vmstat 1
[root@centos-01 ~]# jobs
[1]   已中止               vim /tmp/mytest/1.txt
[2]-  已中止               vim /tmp/mytest/2.txt
[3]+  已中止               vmstat 1

打開新的終端,使用jobs命令並不會顯示在後臺運行或者被暫停的任務,要想停掉它的話,則須要先知道其pid,而後使用kill命令殺死那個進程。kill命令語法很簡單,直接在後面加pid便可。若是遇到殺不死的進程時,能夠在kill後面加一個選項:kill -9 [pid].net

[root@centos-01 ~]# ps aux |grep vim
root       1084  0.0  0.5 151548  5080 pts/0    T    513   0:00 vim /tmp/mytest/1.txt
root       1093  0.0  0.6 153236  6792 pts/0    T    513   0:00 vim /tmp/mytest/2.txt
root       1119  0.0  0.0 112676   980 pts/0    R+   00:19   0:00 grep --color=auto vim
[root@centos-01 ~]# kill 1084
[root@centos-01 ~]# kill 1093
[root@centos-01 ~]# jobs
[1]   已中止               vim /tmp/mytest/1.txt
[2]-  已中止               vim /tmp/mytest/2.txt
[3]+  已中止               vmstat 1
[root@centos-01 ~]# kill -9 1093
[root@centos-01 ~]# jobs
[1]   已中止               vim /tmp/mytest/1.txt
[2]-  已殺死               vim /tmp/mytest/2.txt
[3]+  已中止               vmstat 1
[root@centos-01 ~]# kill -9 1084
[root@centos-01 ~]# jobs
[1]-  已殺死               vim /tmp/mytest/1.txt
[3]+  已中止               vmstat 1
[root@centos-01 ~]# ps aux |grep vmstat
root       1095  0.0  0.1 148316  1352 pts/0    T    513   0:00 vmstat 1
root       1123  0.0  0.0 112676   980 pts/0    R+   00:21   0:00 grep --color=auto vmstat
[root@centos-01 ~]# kill 1095
[root@centos-01 ~]# jobs
[3]+  已中止               vmstat 1
[root@centos-01 ~]# kill -9 1095
[root@centos-01 ~]# jobs
[3]+  已殺死               vmstat 1
[root@centos-01 ~]# jobs
[root@centos-01 ~]#

Shell變量

https://blog.csdn.net/Noob_f/article/details/80385002

環境變量配置文件

  • /etc/profile:這個文件預設了幾個重要的變量,例如PATH、USER、LOGNAME、MAIL、INPUTRC、HOSTNAME、HISTSIZE等。
  • /etc/bashrc:這個文件主要預設umask以及PS1。這個PS1就是在敲命令時,前面那串字符了,例如個人Linux系統PS1就是‘[root@centos-01 ~]#’。
[root@centos-01 ~]# echo $PS1
[\u@\h \W]\$ [root@centos-01 ~]#

‘\u’是用戶,‘\h’是主機名,‘\W’是當前目錄(改成‘w’則是絕對路徑);‘$’就是‘#’ ,若是是普通用戶則顯示爲 ‘$’。

除了兩個系統級別的配置文件外,每一個用戶的家目錄下還有幾個這樣的隱藏文件:

  • .bash_profile:定義了用戶的我的化路徑與環境變量的文件名稱。每一個用戶均可使用該文件輸入專用於本身使用的shell信息。當用戶登陸時,該文件僅僅執行一次。
  • .bashrc:該文件包含專用於當前用戶shell的bash信息。當登陸時以及每次打開新的shell時,該該文件被讀取。例如能夠將用戶自定義的alias或者自定義變量寫到這個文件中。
  • .bash_history:記錄命令歷史。
  • .bash_logout:當退出shell時,會執行該文件。能夠把一些清理的工做放到這個文件中。

Shell中的特殊符號

  • *:任意個任意字符
  • ?:任意一個字符
  • #:註釋字符
  • \:脫義字符
[root@centos-01 ~]# a=1
[root@centos-01 ~]# b=2
[root@centos-01 ~]# c=$a$b
[root@centos-01 ~]# echo $c
12
[root@centos-01 ~]# c='$a$b'
[root@centos-01 ~]# echo $c
$a$b
[root@centos-01 ~]# c=\$a\$b
[root@centos-01 ~]# echo $c
$a$b
  • |:管道符

和管道符有關的命令

https://blog.csdn.net/Noob_f/article/details/80385303


  • $:變量前綴。除了用於變量前面的標識符外,還能夠和‘!’結合起來使用。‘!$’表示上條命中中最後一個變量。假如上條命令是ls test.txt,最後是test.txt,那麼在當前命令下輸入!$則表明test.txt。
  • ;:在一行中運行兩個或兩個以上的命令,須要在命令之間加一個」;」。
  • ~:用戶的家目錄,若是是root則是/root,普通用戶則是/home/username。正則表達式中表示匹配符。
  • &:把一條命令放到後臺執行。
  • >:覆蓋重定向
  • >>:追加劇定向
  • 2>:錯誤覆蓋重定向
  • 2>>:錯誤追加劇定向
  • &>:正確信息和錯誤信息都輸出
  • []:中括號,中間爲字符組合,表明中間字符中的任意一個。

&&||

  1. command1 ; command2
  2. command1 && command2
  3. command1 || command2

使用」;」時,無論command1是否執行成功都會執行command2;

使用「&&」時,只有command1執行成功後,command2纔會執行,不然command2不執行;

使用「||」時,command1執行成功後command2不執行,不然去執行command2,總之command1和command2總有一條命令會執行。

相關文章
相關標籤/搜索