天天看點

第九課預習任務1.shell介紹2.指令曆史3.指令補全和别名4.通配符5.管道符和作業控制6 shell變量7.環境變量配置檔案

第九課預習任務

1.shell介紹

2.指令曆史

3.指令補全和别名

4.通配符

5.管道符和作業控制

6 shell變量

7.環境變量配置檔案

1.shell介紹

1.1shell腳本在日常linux系統管理中用得非常好,也是我們運維工程師一個必備技能。現在很多公司招聘這個也是這最基本的要求,這裡隻是介紹一下一些基礎知識。

1.2shell是系統跟計算機硬體互動時全用的中間媒體,它隻是一個系統工具。它是一個指令解釋器,并且支援特定的文法,每個使用者都有自已特定的shell,centos預設的shell是bash.

2.指令曆史

2.1我們執行過的指令都有記錄下來,預設可以記錄1000條曆史指令。這些指令都會儲存在使用者家目錄的.bash_history檔案中。

//這裡面記錄了root的曆史指令
[[email protected] ~]# ls -a
.  ..  anaconda-ks.cfg  .bash_history  .bash_logout  .bash_profile  .bashrc  .cshrc  .tcshrc
[[email protected] ~]# cat .bash_history
cd 
ls
cd ..
ls
cd /home
ls
ls -l
touch 11.txt
ls -l
chmod 750 11.txt
ls -l
useradd user1
chown user1 11.txtq
chown user1 11.txt
ls -l
useradd user2
passwd user2
passwd user1
su user2
chmod u+s 11.txt
cd ..
pwd
chmod u+s /home/11.txt
           

2.2我們可以在這個配置檔案中修改這個參數/etc/history

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000 //在這裡設定
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
           

2.3 我們可以用"history -c "清空曆史指令,并不會去修改配置檔案,我們可以看到以前的指令都沒有了,隻有剛剛執行的二條。2.4

[[email protected] ~]# history -c
[[email protected] ~]# history
    1  histroy
    2  history
           

2.4 這裡有一個非常實用的變量,HISTTIMEFORMAT="%y:%m:%d  %h:%m:%s",如果想用永久生效,我們需要在

/etc/profile 配置檔案中去寫入這個變量

[[email protected] ~]# HISTTIMEFORMAT="%Y:%m:%d %H:%M:%S"
[[email protected] ~]# history
    1  2018:08:10 06:54:38histroy
    2  2018:08:10 06:54:45history
    3  2018:08:10 07:00:18HISTORYTIMEFORMAT="%y:%m:%d ?%h:%m:%s"
    4  2018:08:10 07:00:29echo HISTORYTIMEFORMAT
    5  2018:08:10 07:00:40echo $HISTORYTIMEFORMAT
    6  2018:08:10 07:00:55history
    7  2018:08:10 07:02:18HISTORYTIMEFORMAT="%Y:%m:%d %H:%M:%S"
    8  2018:08:10 07:02:20history
    9  2018:08:10 07:05:26HISTTIMEFORMAT="%Y:%m:%d %H:%M:%S"
   10  2018:08:10 07:05:27history
           

2.5如果我們不想自已的曆史指令一直儲存下來,我們可以“chattr +a ~/.bash_history”

2.6"!!"表示執行上一條指令

[[email protected] ~]# ls
anaconda-ks.cfg
[[email protected] ~]# !!
ls
anaconda-ks.cfg
           

2.7 “!n”這裡的n是數字,表示執行曆史的第n條指令

[[email protected] ~]# !11
ls
anaconda-ks.cfg
           

2.8 “!字元串”表示執行指令最近一次的指令

[[email protected] /]# ls home
11.txt  user  user1  user2
[[email protected] /]# !ls
ls home
11.txt  user  user1  user2
           

3.指令補全和别名

3.1 “tab”補全,這個指令非常實用。日常工作中使用度也非常高。敲一下和敲兩下也有點不同,敲一下會幫我們補全一個指令、一個路徑或者一個檔案名,敲二下會把系統所有的指令或者檔案名都列出來。

[[email protected] /]# ch
chacl       chardetect  chattr      chcpu       chgrp       chmem       chown       chronyc     chroot      chsh        
chage       chat        chcon       chfn        chkconfig   chmod       chpasswd    chronyd     chrt        chvt      
           

3.2 别名alias,它是bash功能之一。我們可以通過alisa把一個常用的并且很長的指令另取名為一個簡單好記的指令。如果取消隻要用unalias指令解除别名功能。

3.2.1檢視系統中存在的别名

[[email protected] ~]# 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'
           

3.2.2我們也可以自定義指令的别名,格式:alias[指令的别名]=['具體的指令']我們這裡把常用的‘cd’指令重名一下為‘c’

[[email protected]lhost ~]# alias c='cd' //自定義别名
[[email protected] ~]# alias //系統裡面就有了這個别名
alias c='cd'
[email protected] /]# c home //成功了
[[email protected] home]# 
           
[[email protected] ~]# cat .bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
           

4.通配符

4.1在bash,我們可以使用*來比對零個或多個字元,用?比對一個字元。

[[email protected] tmp]# ls *.txt //可以比對一個或多個字元
11.txt  1.txt  223.txt  2.txt
           
[[email protected] tmp]# ls ?.txt//隻能比對一個字元
1.txt  2.txt
           

4.2 還有“[]” “{}”這兩個是集合的比對

[[email protected] tmp]# ls [1-3].txt
1.txt  2.txt
           
[[email protected] tmp]# ls {1,3}.txt //比對範圍中的一個
ls: cannot access 3.txt: No such file or directory
1.txt
           

4.3 輸入\輸出重定向

4.3.1 輸入重定向用于改變指令的輸入,指令是<

[[email protected] tmp]# echo "11111" <1.txt
11111
           

4.3.2輸出重定向用于改變指令的輸出,指令是>,它經常用于将指令的結果輸入到檔案中。追加重定向,指令是>>

[[email protected] tmp]# echo "2222">1.txt
[[email protected] tmp]# cat 1.txt
2222
[[email protected] tmp]# echo "3333">>1.txt //不會删除原先存在資料
[[email protected] tmp]# cat 1.txt
2222
3333
           

4.3.3 錯誤重定向,指令為2>,追加錯誤重定向2>>

[[email protected] tmp]# ls 3.txt 2>1.txt //将前面的錯誤資訊重定向到後面的文檔中
[[email protected] tmp]# cat 1.txt
ls: cannot access 3.txt: No such file or directory
[[email protected] tmp]# cat 1.txt
ls: cannot access 3.txt: No such file or directory
[[email protected] tmp]# ls 33.txt 2>>1.txt
[[email protected] tmp]# cat 1.txt
ls: cannot access 3.txt: No such file or directory
ls: cannot access 33.txt: No such file or directory
           

5.管道符和作業控制

5.1 管道符“|”,它用于将前一個指令的輸出作為後一個指令的輸入

[[email protected] tmp]# ps aux | grep 'httpd'//檢視程序中是否有httpd這個程序
root      95863  0.0  0.0 112704   956 pts/0    S+   07:52   0:00 grep --color=auto httpd
           

5.2 作業控制

5.2.1 當運作程序時,你可以使它暫停(ctrl+z),然後使用fg指令恢複它,或是利用bg指令使它到背景運作。還可以使它終止(ctrl+c)

[[email protected] tmp]# vi 1.txt

[1]+  Stopped                 vi 1.txt //ctrl+z我們可以看到這個vi指令已經終止
[[email protected] tmp]# fg //将指令調回來
vi 1.txt
ls: cannot access 3.txt: No such file or directory
ls: cannot access 33.txt: No such file or directory
                                                     
[[email protected] tmp]# bg //将該指令放到背景運作
[1]+ vi 1.txt &
           

5.2.2 我們可以使用‘jobs’檢視被暫停或者在背景運作的任務

[[email protected] tmp]# jobs
[1]-  Stopped                 vi 1.txt
[2]+  Stopped                 vi 2.txt
           

5.2.3 使用&把任務放到背景運作

[[email protected] tmp]# vmstat &
[3] 95882
[[email protected] tmp]# 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 181160    268 681904    0    0    10    30   68   78  1  1 98  0  0
bg
[2]+ vi 2.txt &
[3]   Done                    vmstat
           

6 shell變量

6.1 shell預設的變量都是大寫的。變量就是使用一個簡單的字元串來替代某些具有特殊意義的設定以及資料。常用的有:PATH,HOME,PWD,LOGNAME.

6.2我們可以使用env檢視系統預設的全部系統變量

[[email protected] tmp]# env
XDG_SESSION_ID=9
HOSTNAME=localhost.localdomain //表示主機的名稱
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash //表示目前使用者的shell類型
HISTSIZE=000 //表示曆史記錄數
SSH_CLIENT=192.168.1.100 50164 22
SELINUX_USE_CURRENT_RANGE=
OLDPWD=/
SSH_TTY=/dev/pts/0
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
MAIL=/var/spool/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/tmp
LANG=en_US.UTF-8
SELINUX_LEVEL_REQUESTED=
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
SSH_CONNECTION=192.168.1.100 50164 192.168.1.150 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
XDG_RUNTIME_DIR=/run/user/0
_=/usr/bin/env
           

6.3還可以使用‘set’列出系統中的預設的環境變量

[[email protected] tmp]# set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:histappend:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.2.46(2)-release'
COLUMNS=136
DIRSTACK=()
EUID=0
GROUPS=()
           

6.4 前面都是系統變量,其實我們還可以自定義變量,定義變量的規則是

第九課預習任務1.shell介紹2.指令曆史3.指令補全和别名4.通配符5.管道符和作業控制6 shell變量7.環境變量配置檔案

6.5 當變量内容帶有特殊字元時,需要加上單引号

[[email protected] yum.repos.d]# a='knight lai' //這裡我們加了一個特殊字元空格
[[email protected] yum.repos.d]# echo $a
knight lai
           

6.6變量的累加,需要加雙引号

[[email protected] yum.repos.d]# c='a'b
[[email protected] yum.repos.d]# echo $c
ab
[[email protected] yum.repos.d]# c='$a'b
[[email protected] yum.repos.d]# echo $c
$ab
[[email protected] yum.repos.d]# c="$a"b //看到這裡才可以正确累加
[[email protected] yum.repos.d]# echo $c
knightb
           

6.7 全局變量 export就是聲明一下這個變量,讓該shell的子shell也知道這個變量。

[[email protected]st yum.repos.d]# export a=knight //聲明全局變量
[[email protected] yum.repos.d]# echo $a
knight
[[email protected] yum.repos.d]# bash //打開他的子shell發現也可以生效
[[email protected] yum.repos.d]# echo $a
knight
           

6.8 取消變量 unset a

[[email protected] yum.repos.d]# unset a
[[email protected] yum.repos.d]# echo $a

           

7.環境變量配置檔案

7.1 /etc/profile:使用者環境變量,互動, 登入才執行。

這個檔案預設了幾個重要的變量如:PATH,USER,LOGNAME,MAIL,INPUTRC,HOSTNAM等 

[[email protected] ~]# cat /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}
           

7.2/etc/bashrc:這個檔案主要預設umask以及PS1.使用者不用登入,執行shell就執行

[[email protected] ~]# cat /etc/bashrc
# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

# are we an interactive shell?
if [ "$PS1" ]; then
  if [ -z "$PROMPT_COMMAND" ]; then
    case $TERM in
    xterm*|vte*)
      if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
      elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
          PROMPT_COMMAND="__vte_prompt_command"
      else
          PROMPT_COMMAND='printf "\033]0;%[email protected]%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
      fi
      ;;
           

7.3.bash_profile:這個檔案定義了使用者的個人化路徑與環境變量的檔案名稱。

[[email protected] ~]# cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
           

7.4.bashrc:該檔案屬于自已的shell的bash資訊,當登入或每次打開新的shell時,該檔案會被讀取。

[[email protected] ~]# cat .bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
           

7.5.bash_history:該檔案用于記錄指令曆史

7.6.bash_logout:當退出shell時,會執行該檔案。可以将一些清理的工作放到這個檔案夾中。

7.7 PS1:我們可以在這個配置檔案下找到這個/etc/profile

[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\[email protected]\h \W]\\$ "
其中\u 指使用者\h指主機名 \w指目前目錄,\$指定符(如果是普通使用者則顯示為¥)
           

繼續閱讀