Linux centos7 shell 介紹、 命令歷史、命令補全和別名、通配符、輸入輸出重定向


一、shell介紹

shell腳本是日常Linux系統管理工作中必不可少的,不會shell,就不是一個合格管理員。

shell是系統跟計算機硬件交互使用的中間介質,一個系統工具。實際上在shell和計算機硬件之間還有一層——系統內核。如果吧計算機比作人的軀體,那系統內核就是人的大腦,至於shell,把它比做人的五官更貼切。

其實,用戶直接面對的不是計算機硬件而是shell,用戶把指令告訴shell,然后shell再傳給系統內核,接着內核再去支配計算機硬件去執行各種操作。

CentOS安裝的shell版本是bash(Bourne Again Shell),是sh(Bourne Shell)的增強版。Bourne Shell是最早流行起來的版本,創始人是Steven Bourne,為了紀念他將其命名為Bourne Shell 簡稱sh

二、命令歷史

我們執行過的命令都會記錄,預設可記錄1000條歷史命令,這些命令保存在用戶的家目錄的.bash_history文件中。

只有當用戶正常退出當前shell時,在當前shell中運行的命令才會保存至.bash_history

!是與命令歷史有關的一個特殊字符

!!:連續連個表示執行上一條指令

!n:這里的n是數字,表示執行命令史中的第n條指令

!字符串(字符串大於等於1):!pw表示執行歷史最近一次以pw開頭的命令

 

[root@davery ~]# ls /root/.bash_history
/root/.bash_history
[root@davery ~]#
[root@davery ~]# cat !$       可存1000條
cat /root/.bash_history
ping wwwbaiducom
vi /etc/sysconfig/network-scripts/ifcfg-ens33
systemctl restart network.service
ifconfig
route -n
ping www.baidu.com
if config
ifconfig
ping wwww.baudu.com
systemctl restsrt network.service
systemctl restart network.service
journactl -xe
systemctl status network.service
etc/profile
systemctl restart network.service
ssh 192.168.1.106

....

[root@davery ~]# echo $HISTSIZE  查看可保存多少
1000
[root@davery ~]#history -c 清除歷史命令,但不會刪除命令,依舊可以使用命令

[root@davery ~]# history   查看使用過的命令

[root@davery ~]# vi /etc/profile 查看文本

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000  最大存儲量

改為500條

HISTSIZE=5000

[root@davery ~]# echo $HISTSIZE
1000
[root@davery ~]# source /etc/profile       刷新一下
[root@davery ~]# echo $HISTSIZE
5000
[root@davery ~]#

 

[root@davery ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 指定格式

[root@davery ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S

[root@davery ~]# vi /etc/profile

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=5000
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "     粘貼變量

[root@davery ~]#

[root@davery ~]# vi /etc/profile
[root@davery ~]# source /etc/profile
[root@davery ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@davery ~]#

chattr +a ~ /.bash_history   永久保存

 

[root@davery ~]# chattr +a ~/.bash_history  

[root@davery ~]# init 0  正常關機才能保存

[root@davery ~]# pwd

 

/root
[root@davery ~]# !! 執行上一條命令
pwd
/root

[root@davery ~]# !555 執行第555條命令
fdisk -l

[root@davery ~]# !pw 執行最近一次以pw開頭的命令
pwd
/root
[root@davery ~]#

三、命令補全和別名

1.命令補全:Tab,敲一下,可以幫我們補全一個指令、一個路徑或者一個文件名;敲兩下,系統會把所有的命令或者文件都列出來

參數補全,安裝bash-completion

[root@davery ~]# yum install -y bash-completion
已加載插件:fastestmirror
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
正在解決依賴關系
--> 正在檢查事務
---> 軟件包 bash-completion.noarch.1.2.1-6.el7 將被 安裝
--> 解決依賴關系完成

依賴關系解決

=========================================================================================================================================
Package 架構 版本 源 大小
=========================================================================================================================================
正在安裝:
bash-completion noarch 1:2.1-6.el7 base 85 k

事務概要
=========================================================================================================================================
安裝 1 軟件包

總下載量:85 k
安裝大小:259 k
Downloading packages:
bash-completion-2.1-6.el7.noarch.rpm | 85 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安裝 : 1:bash-completion-2.1-6.el7.noarch 1/1
驗證中 : 1:bash-completion-2.1-6.el7.noarch 1/1

已安裝:
bash-completion.noarch 1:2.1-6.el7

完畢!
[root@davery ~]# reboot

[root@davery ~]# system
systemctl systemd-coredumpctl systemd-inhibit systemd-run
systemd-analyze systemd-delta systemd-loginctl systemd-stdio-bridge
systemd-ask-password systemd-detect-virt systemd-machine-id-setup systemd-sysv-convert
systemd-cat systemd-escape systemd-notify systemd-tmpfiles
systemd-cgls systemd-firstboot systemd-nspawn systemd-tty-ask-password-agent
systemd-cgtop systemd-hwdb systemd-path
[root@davery ~]# systemctl res
rescue reset-failed restart

2.別名

alias給命令重新起名字

用戶自己的配置別名文件 ~/.bashrc

或者在ls /etc/profile.d/

取消自定義別名

unalias 

自定義alias放到~/.bashrc

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

用戶自己的配置別名文件 ~/.bashrc

[root@davery ~]# vi .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@davery ~]# unalias restartsev 取消別名
[root@davery ~]#
[root@davery ~]# restartsev
-bash: restartsev: 未找到命令
[root@davery ~]#

四、通配符、、輸入輸出重定向

ls *.txt

ls ?.txt

ls [0-9].txt

ls {1,2}.txt

cat 1.txt > 2.txt     把1.txt指定輸入到2.txt

cat 1.txt >> 2.txt   把1.txt追加到2.txt

ls aaa.txt 2 >err

ls aaa.txt 2 >>err

wc -| < 1.txt

command >1.txt 2>&1

[root@davery ~]# ls *.txt

 

[root@davery ~]# ls *
0.txt.gz 4913 5036 anaconda-ks.cfg.01 anaconda-ks.cfg.1 davery davery~ user1

 

1.txt:

 

make:

 

uear1:
[root@davery ~]#

[root@davery ~]# touch 1.txt
[root@davery ~]# touch 2.txt
[root@davery ~]# touch 3.txt
[root@davery ~]# ls ?.txt
2.txt 3.txt

1.txt:
[root@davery ~]#

[root@davery ~]# ls [0-3].txt
2.txt 3.txt

1.txt:
[root@davery ~]# ls [1].txt
[root@davery ~]# ls [123].txt
2.txt 3.txt

1.txt:
[root@davery ~]# ls [13].txt
3.txt

1.txt:
[root@davery ~]# ls [23].txt
2.txt 3.txt
[root@davery ~]#

 

 

[root@davery ~]# ls [0-9a-zA-Z].txt
2.txt 3.txt

 

1.txt:
[root@davery ~]#

 

 

[root@davery ~]# ls {1,2,3,}.txt
ls: 無法訪問.txt: 沒有那個文件或目錄
2.txt 3.txt

 

1.txt:
[root@davery ~]#

 

輸入、輸出重定向

 

[root@davery ~]# cat 1.txt > 2.txt

[root@davery ~]# cat 1.txt >> 2.txt

 

[root@davery ~]# ls [12].txt aaa.txt &> 2.txt
[root@davery ~]# ls 2.txt
2.txt

[root@davery ~]# ls [12].txt aaa.txt &>> 2.txt

[root@davery ~]# wc -l < 1.txt

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM