shell基礎(上)

shell介紹

1、什麼是shell

  1. shell是一個命令解釋器,提供用戶和機器之間的交互
  2. 支持特定的語法,好比邏輯判斷、循環
  3. 每一個用戶 均可以有本身特定的shell
  4. Centos7默認shell爲bash(Bourne Agin Shell)
  5. 還有zsh、ksh等

2、命令歷史

  • history 命令
  • .bash_history
  • 最大1000條 能夠在環境變量配置文件/et/profile中進行修改
  • 變量HISTSIZE
  • /etc/profile修改
  • HISTIMEFORMAT="%Y/%m/%d %H:%M:%S"
  • 永久保存chattr +a ~/.bash_history
  • 使用上下箭頭能夠調用之前的歷史命令
  • !! 重複執行上一條命令
  • !n 重複執行第n條命令
  • !word 重複執行最後哦一條以該字符串開頭的命令
  • history -c 清空歷史命令
# 歷史命令的存放位置 /root/.bash_history

將歷史命令保存條數修改成10000條python

[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]# vi /etc/profile
···
# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=10000  這邊能夠更改歷史命令的條數
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi
···

歷史命令的使用演示shell

[root@localhost ~]# history 
    1  vi /etc/profile
    2  history 
    3  ls
    4  cat data/
    5  history 
[root@localhost ~]# !! 執行上一條命令
history 
    1  vi /etc/profile
    2  history 
    3  ls
    4  cat data/
    5  history 
[root@localhost ~]# !5 執行第五條命令
history 
    1  vi /etc/profile
    2  history 
    3  ls
    4  cat data/
    5  history 
[root@localhost ~]# !his 執行以his字符串出現的命令,若是有相同的字符串,執行最後一次出現的命令。
history 
    1  vi /etc/profile
    2  history 
    3  ls
    4  cat data/
    5  history

3、命令補全和別名

命令補全 tab

在輸入命令或文件時,按「Tab」鍵就會自動進行補全 前提是必須有有這個命令和文件vim

[root@localhost ~]# user 輸入命令的前幾個字符串 而後按tab 鍵,按一次沒有補全,連續按兩次出現能夠補全的命令,而後輸入你須要輸入的命令字符「a」就能夠補全命令useradd命令了。
useradd     userdel     usermod     usernetctl  users
[root@localhost ~]# useradd

別名

  • alias 查看系統中全部命令的別名
  • alias 別名 =‘原命令’ 設定別名
  • 別名永久生效 修改配置文件 ~/.bashrc
  • 刪除別名 unalias 查看系統中的別名
[root@localhost ~]# alias 
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

設定別名例如:alias ls='ls --color=never' ls 不顯示顏色 臨時生效bash

[root@localhost ~]# alias ls='ls --color=never'
[root@localhost ~]# ls
1.repo	anaconda-ks.cfg  data
[root@localhost ~]# alias 
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=never'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

修改配置文件 ~/.bashrc 使別名永久生效,修改完配置文件後還須要執行一條命令source .bashrc 才當即生效,若是不執行,重啓系統後生效oop

······
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias vi='vim'
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
······
[root@localhost ~]# source .bashrc
刪除別名 unalias 別名
[root@localhost ~]# alias 
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]# unalias vi
[root@localhost ~]# !al
alias 
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

vi 設置的別名已經刪除,這只是臨時刪除,若是想永久刪除,須要修改配置文件 .bashrcspa

4、通配符

通配符 做用
匹配一個任意字符
* 匹配0個或多個任意字符,也就是能夠匹配任何內容
[ ] 匹配中括號中任意一個字符。例如:[abc]表明必定匹配一個字符,或者a,或者b,或者c。
[-] 匹配中括號中任意一個字符,-表明一個範圍。例如:[a-z]表明匹配a-z一個小寫字母。
[^] 邏輯非,表示匹配不是中擴括號內的一個字符。例如:[^0-9]表明匹配一個不是數字的字符。
[root@localhost 123]# ls
yuanke  yuanke1  yuanke2  yuanke3  yuanke34  yuankea  yuankeaming
[root@localhost 123]# ls yuanke······ 若是後面什麼都不跟只列出單獨的文件
yuanke
[root@localhost 123]# ls yuanke* ·····使用*表示通配,將yuanke後面帶有全部的字符的文件列出
yuanke  yuanke1  yuanke2  yuanke3  yuanke34  yuankea  yuankeaming
[root@localhost 123]# ls yuanke?······使用?表示匹配任意一個字符,yuanke後面帶有一個字符的列出
yuanke1  yuanke2  yuanke3  yuankea
[root@localhost 123]# ls yuanke[0-9]······使用[]表示yuanke後面0-9匹配的任意一個數字
yuanke1  yuanke2  yuanke3
[root@localhost 123]# ls yuanke[0-9][0-9]若是想匹配兩位數字就寫兩個範圍
yuanke34
[root@localhost 123]# ls yuanke[^0-9]······即是yuanke後面不帶有0-9數字的一個字符
yuankea
[root@localhost 123]# ls yuanke[^0-9]*······表示yuanke後面不帶0-9數字的全部文件
yuankea  yuankeaming

5、輸入輸出重定向

輸出重定向
類型 符號 做用
標準輸出重定向 命令>文件 以覆蓋的方式,把命令的正確輸出輸出到指定的文件或設備當中
標準輸出重定向 命令>>文件 以追加的方式,把命令的正確輸出輸出到指定的文件或設備當中
錯誤輸出重定向 錯誤命令 2>文件 以覆蓋的方式,把命令的錯誤輸出輸出到文件或設備當中
標準錯誤輸出 錯誤命令 2>>文件 以追加的方式,把命令的錯誤輸出輸出到指定的文件或設備當中

以覆蓋的方式,把命令的正確輸出輸出到指定的文件或設備當中.net

[root@localhost ~]# ifconfig > test.log 
[root@localhost ~]# cat test.log 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.5.129  netmask 255.255.255.0  broadcast 192.168.5.255
        inet6 fe80::8335:b418:bf20:3d39  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:33:21:53  txqueuelen 1000  (Ethernet)
        RX packets 631  bytes 48243 (47.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 304  bytes 33849 (33.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 68  bytes 5912 (5.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 68  bytes 5912 (5.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# ls > test.log 
[root@localhost ~]# cat test.log 
123
1.repo
anaconda-ks.cfg
data
test.log

以追加的方式,把命令的正確輸出輸出到指定的文件或設備當中命令行

[root@localhost ~]# ls >> test.log 
[root@localhost ~]# cat test.log 
123
1.repo
anaconda-ks.cfg
data
test.log
123
1.repo
anaconda-ks.cfg
data
test.log

正確輸出和錯誤輸出同時保存

符號 做用
命令 > 文件 2>&1 以覆蓋的方式,把正確輸出和錯誤輸出都保存到同一個文件當中
命令 >> 文件 2>&1 以追加的方式,把正確輸出和錯誤輸出都保存到同一個文件當中
命令 &>文件 以覆蓋的方式,把正確輸出和錯誤輸出都保存到同一個文件當中
命令 &>>文件 以追加的方式,把正確輸出和錯誤輸出都保存到同一個文件當中
命令 >>文件1 2>>文件2 把正確的輸出追加到文件1中,把錯誤的輸出追加到文件2中。
覆蓋的形式

一、命令 > 文件 2>&1 以覆蓋的方式,把正確輸出和錯誤輸出都保存到同一個文件當中3d

[root@localhost ~]# ls > test2.log 2>&1 ······將ls執行的命令結果輸出到test2.log 
[root@localhost ~]# ls ······ ls命令執行的結果
123  1.repo  anaconda-ks.cfg  data  test2.log  test.log
[root@localhost ~]# cat test2.log ······查看正確輸出到test2.log的結果
123
1.repo
anaconda-ks.cfg
data
test2.log
test.log
[root@localhost ~]# lsyuanke > test2.log 2>&1······執行完錯誤命令後並無顯示到當前頁面,而是將錯誤的命令結果輸出到test2.log
[root@localhost ~]# cat test2.log ······查看輸出命令結果
-bash: lsyuanke: 未找到命令···· ······錯誤的命令結果

二、命令 &>文件日誌

[root@localhost ~]# ls &>test.log 
[root@localhost ~]# cat test.log 
123
1.repo
anaconda-ks.cfg
data
test.log
[root@localhost ~]# lss &>test.log 
[root@localhost ~]# cat test.log 
-bash: lss: 未找到命令
追加的形式

一、命令 >> 文件 2>&1 | 以追加的方式,把正確輸出和錯誤輸出都保存到同一個文件當中

[root@localhost ~]# ls >> test.log 2>&1
[root@localhost ~]# cat test.log 
-bash: lss: 未找到命令
123
1.repo
anaconda-ks.cfg
data
test.log

二、命令 &>>文件

[root@localhost ~]# cattt &>>test.log 
[root@localhost ~]# cat test.log 
-bash: lss: 未找到命令
123
1.repo
anaconda-ks.cfg
data
test.log
-bash: lss: 未找到命令
-bash: cattt: 未找到命令

命令 >>文件1 2>>文件2 | 把正確的輸出追加到文件1中,把錯誤的輸出追加到文件2中。

[root@localhost ~]# ls >>right.log 2>>error.log
[root@localhost ~]# cat right.log   ······ ls是正確的命令 輸出到rigth.log 文件當中
123
1.repo
anaconda-ks.cfg
data
error.log
right.log
test.log
[root@localhost ~]# cat error.log ······  錯誤的日誌裏面並無記錄
[root@localhost ~]# lsss >>right.log 2>>error.log ······執行一條錯誤的命令 lssss
[root@localhost ~]# cat right.log ······正確的日誌裏面並無追加
123
1.repo
anaconda-ks.cfg
data
error.log
right.log
test.log
[root@localhost ~]# cat error.log ······錯誤的日誌文件裏面顯示了 錯誤輸出命令結果
-bash: lsss: 未找到命令

/dev/null 系統中預留的黑洞,沒有用的文件能夠放到這裏面

6、管道符

[root@localhost ~]# 命令1 | 命令2

# 命令1的正確輸出做爲命令2的操做對象
[root@localhost ~]# ls 
123  1.repo  anaconda-ks.cfg  data  error.log  right.log  test.log
[root@localhost ~]# ls -l | grep right.log 
-rw-r--r--  1 root root   61 5月  31 22:17 right.log
[root@localhost ~]# ls -l | wc -l
8

這裏須要注意的是命令2必須是可以正確執行命令1的輸出結果

經常使用的快捷鍵

快捷鍵 做用
ctrl+c 強制終止當前命令
ctrl+l 清屏
ctrl+a 光標移動到命令行首
ctrl+e 光標移動到命令行尾
ctrl+u 從光標所在位置刪除到行首
ctrl+z 把命令放入後臺
ctrl+r 在歷史命令中搜索

ctrl+c 強制終止當前命令

[root@localhost ~]# cat right.log ^C

ctrl+z|把命令放入後臺

[root@localhost ~]# vim error.log 
-bash: lsss: 未找到命令
"error.log" 1L, 29C                               1,1          所有

[1]+  已中止               vim error.log

使用命令fg能夠進入後臺任務 ctrl+r|在歷史命令中搜索

(reverse-i-search)`vim': vim error.log ······在歷史命令中搜索出現的字符命令,只能搜索出出如今歷史命令中的最後一條。
  • jobs查看後臺任務
[root@localhost ~]# jobs
[1]-  已中止               vim aa.txt
[2]+  已中止               vim bb.txt
  • bg[id]把任務調到後臺運行
  • fg[id]把任務調到前臺運行
[root@localhost ~]# sleep 1000
^Z
[1]+  已中止               sleep 1000
[root@localhost ~]# sleep 200
^Z
[2]+  已中止               sleep 200
[root@localhost ~]# jobs
[1]-  已中止               sleep 1000
[2]+  已中止               sleep 200
[root@localhost ~]# bg 1 將任務1調到後臺運行
[1]- sleep 1000 &
[root@localhost ~]# jobs
[1]-  運行中               sleep 1000 &
[2]+  已中止               sleep 200
[root@localhost ~]# fg 1 將任務1調到前臺運行
sleep 1000
  • 命令後面加&直接丟到後臺運行
[root@localhost ~]# sleep 1000 &
[1] 1303
[root@localhost ~]# jobs
[1]+  運行中               sleep 1000 &
相關文章
相關標籤/搜索