【2018.04.20學習筆記】【linux基礎知識8.1-8.5】

8.1 shell介紹

shell是一種命令解釋器,用於接收用戶指令與操做系統交互;php

他有特定的語法:例如邏輯判斷、循環語句等。mysql

每一個系統用戶都有本身的shell,CentOS默認的Shell是 -bash(bourne Agin Shell)linux

系統還有zsh、ksh等。sql

Connecting to 192.168.87.130:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

Last login: Fri Apr 20 00:00:10 2018 from 192.168.87.1
[root@lgs-01 ~]# shell
-bash: shell: 未找到命令

8.2 命令歷史

linux系統會記錄用戶輸入過的歷史命令。shell

查看命令的歷史記錄:history 命令,默認存儲1000條命令歷史apache

[root@lgs-01 ~]# history
 #節省部分顯示 
 976  rm -rf /var/cache/yum/x86_64/7/base/packages/zziplib-0.13.62-5.el7.x86_64.rpm 
  977  ls -l /var/cache/yum/x86_64/7/base/packages/
  978  yum reinstall -y zziplib --downloadonly
  979  ls -l /var/cache/yum/x86_64/7/base/packages/
  980  cd /usr/local/src/
  981  ls
  982  wget http://cn2.php.net/distributions/php-7.1.6.tar.bz2
  983  ls -l php-7.1.6.tar.bz2 
  984  tar jxvf php-7.1.6.tar.bz2 
  985  ls
  986  cd php-7.1.6
  987  ls
  988  ./configure --prefix=/usr/local/php7 --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php7/etc --with-pdo-mysql=/usr/local/mysql/ -with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
  989  echo $?
  990  make
  991  echo $?
  992  make install
  993  echo $?
  994  /usr/local/apache2.4/bin/apachectl -M
  995  ls -l /usr/local/apache2.4/modules/libphp7.so 
  996  init 0
  997  shell
  998  history

history的歷史是記錄在 /root/.bash_history裏:vim

[root@lgs-01 ~]# ls -l /root/.bash_history 
-rw-------. 1 root root 23650 4月  20 01:11 /root/.bash_history

清除命令歷史:history -cbash

默認記錄1000條命令歷史,能夠修改配置文件/etc/profile裏的HISTSIZE參數:php7

[root@lgs-01 ~]# echo $HISTSIZE 
1000
[root@lgs-01 ~]# 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
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    fi
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# 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=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge

加入變量HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S",能夠顯示命令執行的時間less

修改配置文件後,source /etc/profile 從新加載才生效

[root@lgs-01 ~]# 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.
#節省部份內容
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S"
[root@lgs-01 ~]# source /etc/profile
[root@lgs-01 ~]# history
  995  2018/04/20 13:33:01ls -l /usr/local/apache2.4/modules/libphp7.so 
  996  2018/04/20 13:33:01init 0
  997  2018/04/20 14:20:48shell
  998  2018/04/20 14:23:11history
  999  2018/04/20 14:25:22historyls -l /etc/.bash_history
 1000  2018/04/20 14:25:27ls -l /etc/.bash_history
 1001  2018/04/20 14:25:46ls -l /root/.bash_history 
 1002  2018/04/20 14:27:11cat /root/.bash_history 
 1003  2018/04/20 14:27:41vim /root/.bash_history 
 1004  2018/04/20 14:31:30echo $HISTSIZE 
 1005  2018/04/20 14:32:03cat /etc/profile
 1006  2018/04/20 14:35:16vim /etc/profile
 1007  2018/04/20 14:36:43source /etc/profile
 1008  2018/04/20 14:36:46ls
 1009  2018/04/20 14:36:55history

執行上一條命令:!!

[root@lgs-01 ~]# !!
ls
123.zip                                b                                          mesa-libGL-devel-17.0.1-6.20170307.el7.x86_64.rpm
1.txt.bak                              c                                          mesa-libGLU-9.0.0-4.el7.x86_64.rpm

執行命令歷史裏的第幾條命令:!n ,n是數字

1010  2018/04/20 14:39:18 cat /etc/profile
 1011  2018/04/20 14:40:04 source /etc/profile
 1012  2018/04/20 14:40:45 ls
 1013  2018/04/20 14:41:54 history
[root@lgs-01 ~]# !1011
source /etc/profile

執行最近以某字符內容開頭的命令:!echo

1004  2018/04/20 14:31:30 echo $HISTSIZE 
 1005  2018/04/20 14:32:03 cat /etc/profile
 1006  2018/04/20 14:35:16 vim /etc/profile
 1007  2018/04/20 14:36:43 source /etc/profile
 1008  2018/04/20 14:36:46 ls
 1009  2018/04/20 14:36:55 history
 1010  2018/04/20 14:39:18 cat /etc/profile
 1011  2018/04/20 14:40:04 source /etc/profile
 1012  2018/04/20 14:40:45 ls
 1013  2018/04/20 14:41:54 history
[root@lgs-01 ~]# !1011
source /etc/profile
[root@lgs-01 ~]# !echo
echo $HISTSIZE 
1000

8.3 命令補全和別名

linux系統提供了命令輸入的補全功能,提升用戶輸入命令的效率。

須要安裝:bash-completion

當輸入命令、文件或者目錄的開頭部分字符串的時候:按tab補全:

按一下tab:當開頭字符串只有一個命令、文件或者目錄匹配的時候,按一下tab便可補全

# [root@lgs-01 ~]# logo 此處按一下tab,便可補全命令 logout
[root@lgs-01 ~]# logout

按兩下tab:當開頭字符串不止一個命令、文件或者目錄匹配的時候,按兩下tab能夠列出全部該開頭字符串的命令、文件或者目錄,而後再繼續根據提示輸入後續字符便可。

[root@lgs-01 ~]# log
logger     login      loginctl   logname    logout     logrotate  logsave

當一條命令很長的時候,還能夠給命令起別名,提升輸入效率

用alias命令:例如ls命令,是有帶顏色顯示選項的ls命令取別名而來

[root@lgs-01 ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/l
[root@lgs-01 ~]# alias restartnet='systemctl restart network.service '
[root@lgs-01 ~]# res
reset       resize2fs   resizecons  resizepart  restartnet  restorecon  
[root@lgs-01 ~]# restartnet 
[root@lgs-01 ~]# which restartnet
alias restartnet='systemctl restart network.service '
	/usr/bin/systemctl

查看系統全部的別名:

[root@lgs-01 ~]# 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 restartnet='systemctl restart network.service '
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

在用戶家目錄下的/.bashrc 配置文件裏定義了部分系統的alias別名,剩下的在

/etc/profile.d/ 目錄下的腳本文件

[root@lgs-01 ~]# cat /root/.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
[root@lgs-01 ~]# ls /etc/profile.d/
256term.csh  colorgrep.csh  colorls.csh  gnome-ssh-askpass.csh  lang.csh  less.csh  vim.csh  which2.csh
256term.sh   colorgrep.sh   colorls.sh   gnome-ssh-askpass.sh   lang.sh   less.sh   vim.sh   which2.sh

8.4 命令的通配符

任意個字符或0個:*

[root@lgs-01 ~]# ls *.bak
1.txt.bak  2_hard.txt.bak  2.txt.bak

[root@lgs-01 ~]# ls 333*.tar
333.tar

單個字符:

[root@lgs-01 ~]# ls 33?.tar
333.tar

[ ] 中括號範圍內的單個字符

[root@lgs-01 ~]# ls *.txt
k.txt  zb.txt
[root@lgs-01 ~]# ls [a-zA-Z].txt
k.txt

[ ] 中括號裏選取某單個字符

[root@lgs-01 ~]# ls *.txt
k.txt  w.txt  zb.txt
[root@lgs-01 ~]# ls [lkw].txt
k.txt  w.txt

{ } 大括號裏的一個字符段,以逗號分隔

[root@lgs-01 ~]# ls *.txt
abcd.txt  ape.txt  k.txt  los.txt  w.txt  zb.txt
[root@lgs-01 ~]# ls {los,k,sss,ape}.txt
ls: 沒法訪問sss.txt: 沒有那個文件或目錄
ape.txt  k.txt  los.txt

8.5 輸入輸出重定向

輸出重定向,覆蓋原文件的內容: >

[root@lgs-01 ~]# echo 1234 >w.txt 
[root@lgs-01 ~]# cat w.txt 
1234
[root@lgs-01 ~]# echo 0008 >w.txt 
[root@lgs-01 ~]# cat w.txt 
0008

追加劇定向,只追加內容,不覆蓋:>>

[root@lgs-01 ~]# cat w.txt 
0008
[root@lgs-01 ~]# echo 99999 >>w.txt 
[root@lgs-01 ~]# cat w.txt 
0008
99999

錯誤信息輸出重定向: 2>

[root@lgs-01 ~]# laaaa
-bash: laaaa: 未找到命令
[root@lgs-01 ~]# laaaa 2>k.txt 
[root@lgs-01 ~]# cat k.txt 
-bash: laaaa: 未找到命令

錯誤信息追加劇定向:2>>

[root@lgs-01 ~]# laaaa 2>k.txt 
[root@lgs-01 ~]# cat k.txt 
-bash: laaaa: 未找到命令
[root@lgs-01 ~]# kkkkk 2>>k.txt 
[root@lgs-01 ~]# cat k.txt 
-bash: laaaa: 未找到命令
-bash: kkkkk: 未找到命令

正確與錯誤輸出重定向: &>

[root@lgs-01 ~]# ls *.txt 2>k.txt abcccc.txt &>k.txt 
[root@lgs-01 ~]# cat k.txt 
ls: 沒法訪問abcccc.txt: 沒有那個文件或目錄
abcd.txt
ape.txt
k.txt
los.txt
w.txt
zb.txt

也支持正確與錯誤追加劇定向: &>>

正確與錯誤輸出分開重定向:

[root@lgs-01 ~]# cat w.txt 
0008
99999
[root@lgs-01 ~]# cat k.txt 
111
[root@lgs-01 ~]# ls *.txt ppp.txt >w.txt 2>k.txt 
[root@lgs-01 ~]# cat w.txt 
abcd.txt
ape.txt
k.txt
los.txt
w.txt
zb.txt
[root@lgs-01 ~]# cat k.txt 
ls: 沒法訪問ppp.txt: 沒有那個文件或目錄

輸入重定向:< 把右邊的文件內容 輸入給 左邊的命令,左邊只能是命令

[root@lgs-01 ~]# cat w.txt 
abcd.txt
ape.txt
k.txt
los.txt
w.txt
zb.txt
[root@lgs-01 ~]# wc -l <w.txt 
6
相關文章
相關標籤/搜索