shell介绍
什么是shell?
shell是系统跟计算机硬件交互时使用的中间介质,它只是系统的一个工具。(形成比喻:用户直接面对的不是计算机硬件面是shell,用户把指令告诉shell,然后shell再传输给系统内核,接着内核再去支配计算机硬件去执行各种操作)
1、shell是一个命令解释器,提供用户和机器之间的交互 比如我们登录的终端能够让我们运行命令查看结果,这就是一个shell。
2、支持特定语法,比如逻辑判断、循环、for等
3、每个用户都可以有自己特定的shell
4、CentOS7默认shell为bash(Bourne Agin Shell,Bourne是一个用户的名字,为了纪念他,把他开发shell叫做Bourne shell,后来在这个基础上优化开发,形成一个新的shell,叫做bash shell,也就是在centos7下使用的这个bash shell)
5、除了bash shell还有zsh、ksh等,用起来跟bash差不多,但有些细节上差异,比如一些特性的差异。
查看系统是否有安装zsh、ksh,如果没有可以yum安装一下,示例如下
[root@aminglinux-01 ~]# yum list |grep zsh
zsh.x86_64 5.0.2-25.el7 installed
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@aminglinux-01 ~]# yum list |grep ksh
ksh.x86_64 20120801-34.el7 base
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-hub.noarch 1.5.3-2.el7 epel
python-moksha-wsgi.noarch 1.2.2-2.el7 epel
命令历史:
1、history命令
#查看之前使用的命令。
2、.bash_history
#存储使用过的命令文件,在root的家目录下
3、最大1000条
#存储使用命令的最大数,在/etc/profile文件里更改数值
4、变量HISTSIZE
#查看命令 echo $HISTSIZE
5、/etc/profile中修改HISTSIZE最大保存数,source保存
6、HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
#永久生效把这条命令添加到/etc/profile里
7、永久保存 chattr +a ~/.bash_history
8、!!
#执行上一条命令
9、!n
#n表示数字,比如:!176, 这样它就会执行命令历史里的176这条命令
10、!echo
#echo表示在命令历史里从下往上找以echo开头的第一条命令执行它。
示例如下
1、.bash_history文件
#命令历史保存在root的家目录下.bash_history文件中
[root@aminglinux-01 ~]# ls /root/.bash_history
/root/.bash_history
2、history命令
#查看之前历史使用的命令。
[root@aminglinux-01 ~]# history
1 ls -l /tmp/
2 ls -l
......
585 ls /root/.bash_history
586 cat /root/.bash_history
587 history
3、echo $HISTSIZE 显示存放最大多少条历史记录
#history存储使用命令的最大数1000条,变量HISTSIZE
[root@aminglinux-01 ~]# echo $HISTSIZE
1000
4、history –c清空内存中的历史记录;不会清除.bash_history文件中的记录
#清空当前命令历史记录
[root@aminglinux-01 ~]# history -c
[root@aminglinux-01 ~]# history
1 history
#查看是否清空了配置文件命令,没有,只能清空当前命令历史的。
[root@aminglinux-01 ~]# cat .bash_history
ls -l /tmp/
ls -l
which rm
注意:当前使用的命令并不会直接保存到.bash_history配置文件里面去,是先存储在内存里,只有当你退出终端的时候才会保存到配置文件里。
5、定义HISTSIZE值,在配置文件/etc/profile中修改
#环境变量HISTSIZE在/etc/profile文件中搜索HISTSIZE更改命令历史变量数值
[root@aminglinux-01 ~]# vi /etc/profile
默认值是1000,改成5000如下:
HISTSIZE=5000
#保存退出后查看变更并没有生效
[root@aminglinux-01 ~]# echo $HISTSIZE
1000
#更改变量执行source命令
[root@aminglinux-01 ~]# source /etc/profile
#查看更改结果
[root@aminglinux-01 ~]# echo $HISTSIZE
5000
6、定义格式:HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
看history是只显示ID号与具体命令,是否可以把命令是什么时候运行的记录下来,只需要把变量重新赋值就可以了。
[root@aminglinux-01 ~]# history
1 history
2 cat .bash_history
3 vi /etc/profile
4 echo $HISTSIZE
5 source /etc/profile
6 echo $HISTSIZE
7 history
#更改命令历史格式,只在当前终端生效
[root@aminglinux-01 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@aminglinux-01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@aminglinux-01 ~]# history
1 2017/11/14 16:45:35 history
2 2017/11/14 16:46:43 cat .bash_history
3 2017/11/14 17:59:55 vi /etc/profile
4 2017/11/14 18:04:45 echo $HISTSIZE
5 2017/11/14 18:05:12 source /etc/profile
6 2017/11/14 18:05:14 echo $HISTSIZE
7 2017/11/14 18:06:30 history
8 2017/11/14 18:07:10 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
9 2017/11/14 18:07:34 echo $HISTTIMEFORMAT
10 2017/11/14 18:07:57 history
#如果想要保存如上history格式效果,就需要进入配置文件加上如下这条变量命令,保存退出。
[root@aminglinux-01 ~]# vim /etc/profile
添加到文件最后面:
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[root@aminglinux-01 ~]# source /etc/profile
#这样哪怕再开一个终端这也保存了。
[root@aminglinux-01 ~]# echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
7、给命令历史文件增加特殊权限
#为了让命令历史永久保存,不想让其它人去破坏它,可以给它加一个特殊的a权限,不能删除,可以追加。
[root@aminglinux-01 ~]# chattr +a ~/.bash_history
[root@aminglinux-01 ~]# lsattr .bash_history
-----a---------- .bash_history
注意:如果执行了这条命令后,你并没有正常退出终端,就会导致保存的命令不全。
8、!! 连续两个,!表示执行上一条命令。
[root@xietaolinux3 ~]# lsattr .bash_history
-----a---------- .bash_history
[root@xietaolinux3 ~]# !!
lsattr .bash_history
-----a---------- .bash_history
9、!n 这里的n是数字,表示执行命令历史中的第n条命令。
[root@xietaolinux3 ~]# history
1 2018/12/17 16:18:32 history
2 2018/12/17 16:30:00 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
3 2018/12/17 16:30:05 echo $HISTTIMEFORMAT
4 2018/12/17 16:30:11 history
5 2018/12/17 16:32:56 vim /etc/profile
6 2018/12/17 16:33:09 source /etc/profile
7 2018/12/17 16:35:10 chattr +a ~/.bash_history
8 2018/12/17 16:35:19 lsatty .bash_history
9 2018/12/17 16:35:25 lsattr .bash_history
10 2018/12/17 16:41:17 history
11 2018/12/17 16:41:33 lsatty .bash_history
12 2018/12/17 16:41:41 history
[root@xietaolinux3 ~]# !9
lsattr .bash_history
-----a---------- .bash_history
10、!echo 表示执行命令历史中最近一次以echo开头的命令。
[root@xietaolinux3 ~]# history
1 2018/12/17 16:18:32 history
2 2018/12/17 16:30:00 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
3 2018/12/17 16:30:05 echo $HISTTIMEFORMAT
4 2018/12/17 16:30:11 history
5 2018/12/17 16:32:56 vim /etc/profile
6 2018/12/17 16:33:09 source /etc/profile
7 2018/12/17 16:35:10 chattr +a ~/.bash_history
8 2018/12/17 16:35:19 lsatty .bash_history
9 2018/12/17 16:35:25 lsattr .bash_history
10 2018/12/17 16:41:17 history
11 2018/12/17 16:41:33 lsatty .bash_history
12 2018/12/17 16:41:41 history
13 2018/12/17 16:41:45 lsattr .bash_history
14 2018/12/17 16:42:52 history
[root@xietaolinux3 ~]# !echo
echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
命令补全和别名:
1、tab键,敲一下,敲两下(按tab键可以帮我们补全一个指令,一个路径或者一个文件名)
2、参数补全,安装bash-completion ,重启系统生效 适用于centos7
3、alias别名给命令重新起个名字
4、各用户都有自己配置别名的文件 ~/.bashrc(存放别名的路径地址)
5、ls /etc/profile.d/(存放别名的路径地址)
6、自定义的alias放到~/.bashrc,自定义后别名存放的地址
(永久保存alias别名,把alias信息存到/etc/bashrc里面或者家目录下面的.bashrc)
命令补全
Centos6中tab键只能补全本身,不支持参数补全;Centos7中tab键支持命令参数补全,需要安装一个包bash-completion 重启才能生效。
#使用tab测试补全这条命令参数
[root@aminglinux-01 ~]# systemctl restart network
#安装这个bash-completion库
[root@aminglinux-01 ~]# yum install -y bash-completion
#重启系统
[root@aminglinux-01 ~]# reboot
alias别名
alias也是bash所特有的功能之一
#直接执行alias命令会看到目前系统预设的所有别名
[root@aminglinux-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 rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
有的时候命令很长,打的时候不方便,效率低,这样就可以使用alias做别名,如下:
#alias别名给命令重新命名
[root@aminglinux-01 ~]# alias restartnet='systemctl restart network.service'
[root@xietaolinux3 ~]# 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'
#设置好的别名命令生效
[root@aminglinux-01 ~]# restartnet
#取消设置好的别名,使用命令unalias
[root@aminglinux-01 ~]# unalias restartnet
#别名命令失效
[root@aminglinux-01 ~]# restartnet
-bash: restartnet: 未找到命令
配置alias的文件有哪些呢?举例如下:
#用户家目录下.bashrc文件中只配置了几个alias
[root@xietaolinux3 ~]# 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
#其他的很多别名在/etc/profile.d/目录下,有很多.sh文件中定义
[root@xietaolinux3 ~]# ls /etc/profile.d/
256term.csh colorgrep.csh colorls.sh less.csh vim.sh
256term.sh colorgrep.sh lang.csh less.sh which2.csh
bash_completion.sh colorls.csh lang.sh vim.csh which2.sh
#如colorgrep.sh文件中的grep定义的alias
[root@xietaolinux3 ~]# cat /etc/profile.d/colorgrep.sh
# color-grep initialization
/usr/libexec/grepconf.sh -c || return
alias grep='grep --color=auto' 2>/dev/null
alias egrep='egrep --color=auto' 2>/dev/null
alias fgrep='fgrep --color=auto' 2>/dev/null
通配符
注意:命令行下的通配不区分大小写,但是如果是正则表达式就区分了。
• ls *.txt #表示查找.txt通配文件
• ls ?.txt #表示一个任意的字符
• ls [0-9].txt #列出满足条件范围内的文件
• ls {1,2}.txt #用花括号列出你需要的文件
*用来匹配零个或多个任意字符
[root@aminglinux-01 ~]# ls
111 1_heard.txt.bak 1.txt.bak 2.txt 456 aminglinux
123 1_sorft.txt.bak 234 3.txt aming2 anaconda-ks.cfg
[root@aminglinux-01 ~]# ls *.txt
2.txt 3.txt
[root@aminglinux-01 ~]# ls *txt
2.txt 3.txt
[root@aminglinux-01 ~]# ls *txt*
1_heard.txt.bak 1_sorft.txt.bak 1.txt.bak 2.txt 3.txt
[root@aminglinux-01 ~]# ls 1*
1_heard.txt.bak 1_sorft.txt.bak 1.txt.bak
111:
123:
?用来匹配一个字符
[root@aminglinux-01 ~]# ls ?.txt
2.txt 3.txt
[root@aminglinux-01 ~]# touch 1.txt
[root@aminglinux-01 ~]# ls ?.txt
1.txt 2.txt 3.txt
[root@aminglinux-01 ~]# touch a.txt
[root@aminglinux-01 ~]# touch bb.txt
#bb.txt因为是两个字符,所以没有匹配出来
[root@aminglinux-01 ~]# ls ?.txt
1.txt 2.txt 3.txt a.txt
[ ]里面可以写一个范围,只要是在这个范围内单个字符都列出来
[root@aminglinux-01 ~]# ls [0-3].txt
1.txt 2.txt 3.txt
[root@aminglinux-01 ~]# ls [12].txt
1.txt 2.txt
[root@aminglinux-01 ~]# ls [23].txt
2.txt 3.txt
[root@aminglinux-01 ~]# ls {1,2}.txt
1.txt 2.txt
[root@xietaolinux3 ~]# ls [a-z].txt
a.txt
[root@xietaolinux3 ~]# ls [0-9a-z].txt
1.txt 2.txt 3.txt a.txt b.txt
[root@xietaolinux3 ~]# ls [0-9A-Z].txt
1.txt 2.txt 3.txt b.txt
[root@xietaolinux3 ~]# ls [0-9a-zA-Z].txt
1.txt 2.txt 3.txt a.txt b.txt
{ }花括号里面要用“,”隔开,1.txt或者3.txt;
[root@xietaolinux3 ~]# ls {1,2,3,a}.txt
1.txt 2.txt 3.txt a.txt
输入输出重定向
输入重定向用于改变命令的输入,输出重定向用于改变命令的输出。输出重定向更为常用,它经常用于将命令的结果输入到文件中,而不是屏幕中。输入重定向的命令是<,输出重定向的命令是>。另外还有错误重定向2>以及追加重定向命令>>。
1、 cat 1.txt >2.txt
#>它会把命令产生的正确信息输出到指定文件里去,删除之前文件内容重写。
2、 cat 1.txt >> 2.txt
#>>把前面命令输出内容重定向追加到后面命令里去,不删除旧的。
3、ls aaa.txt 2> err
#它会把命令产生的错误信息输出到指定文件里去
4、ls aaa.txt 2>> err
#错误信息输出追加重定向
5、2>与2>>等于&>
#&>结合了正确与错误
6、 wc -l < 1.txt
#查看一个文件的行数 , 把1.txt内容输入到重定向命令里面去,得出结果,左边必须是一条命令,不支持文件到文件。
>输出重定向
[root@xietaolinux3 ~]# echo "aannccaa" > 1.txt
[root@xietaolinux3 ~]# cat 1.txt
aannccaa
>>追加重定向
[root@xietaolinux3 ~]# echo "aannccaa" >> 1.txt
[root@xietaolinux3 ~]# cat 1.txt
aannccaa
aannccaa
2> 错误重定向
#使用错误命令输出一条错误信息
[root@aminglinux-01 ~]# lsaaa
-bash: lsaaa: 未找到命令
#保存一条信息到指定文件里去
[root@aminglinux-01 ~]# lsaaa 2> a.txt
#查看结果
[root@aminglinux-01 ~]# cat a.txt
-bash: lsaaa: 未找到命令
2>>错误追加重定向
#追加一次错误信息到文件
[root@aminglinux-01 ~]# lsaaa 2>> a.txt
#查看结果
[root@aminglinux-01 ~]# cat a.txt
-bash: lsaaa: 未找到命令
-bash: lsaaa: 未找到命令
&>等于>+2>正确和错误重定向
#&>结合正确错误信息重定向到一个文件里去
[root@aminglinux-01 ~]# ls [12].txt aaa.txt &> a.txt
#查看结果
[root@aminglinux-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
&>>追加正确与错误信息重定向
[root@aminglinux-01 ~]# ls [12].txt aaa.txt &>> a.txt
[root@aminglinux-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
可以将正确输出到一个文件中,错误输入到另一个文件中,也可以同时输出到一个文件中(用&>)
#既有正确也有错误的输出,区分开来保存在指定的文件里,把正确与错误区分开来输出
[root@aminglinux-01 ~]# ls [12].txt aaa.txt > 1.txt 2>a.txt
#正确
[root@aminglinux-01 ~]# cat 1.txt
1.txt
2.txt
#错误
[root@aminglinux-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
< 输入重定向
#把1.txt内容输入到重定向命令里面去,得出结果
[root@localhost ~]# wc -l < 1.txt
2
#左边只能是命令,不能是文件,不支持这样的写法。
[root@xietaolinux3 ~]# 2.txt < 1.txt
-bash: 2.txt: 未找到命令