什么是shell?html
shell是系统跟计算机硬件交互时使用的中间介质,它只是系统的一个工具。(造成比喻:用户直接面对的不是计算机硬件面是shell,用户把指令告诉shell,而后shell再传输给系统内核,接着内核再去支配计算机硬件去执行各类操做)python
一、shell是一个命令解释器,提供用户和机器之间的交互 好比咱们登陆的终端可以让咱们运行命令查看结果,这就是一个shell。linux
二、支持特定语法,好比逻辑判断、循环、for等正则表达式
三、每一个用户均可以有本身特定的shellshell
四、CentOS7默认shell为bash(Bourne Agin Shell,Bourne是一个用户的名字,为了记念他,把他开发shell叫作Bourne shell,后来在这个基础上优化开发,造成一个新的shell,叫作bash shell,也就是在centos7下使用的这个bash shell)vim
五、除了bash shell还有zsh、ksh等,用起来跟bash差很少,但有些细节上差别,好比一些特性的差别。centos
查看系统是否有安装zsh、ksh,若是没有能够yum安装一下,示例以下bash
[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
一、history命令 #查看以前使用的命令。 二、.bash_history #存储使用过的命令文件,在root的家目录下 三、最大1000条 #存储使用命令的最大数,在/etc/profile文件里更改数值 四、变量HISTSIZE #查看命令 echo $HISTSIZE 五、/etc/profile中修改HISTSIZE最大保存数,source保存 六、HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " #永久生效把这条命令添加到/etc/profile里 七、永久保存 chattr +a ~/.bash_history 八、!! #执行上一条命令 九、!n #n表示数字,好比:!176, 这样它就会执行命令历史里的176这条命令 十、!echo #echo表示在命令历史里从下往上找以echo开头的第一条命令执行它。
示例以下less
一、.bash_history文件工具
#命令历史保存在root的家目录下.bash_history文件中 [root@aminglinux-01 ~]# ls /root/.bash_history /root/.bash_history
二、history命令
#查看以前历史使用的命令。 [root@aminglinux-01 ~]# history 1 ls -l /tmp/ 2 ls -l ...... 585 ls /root/.bash_history 586 cat /root/.bash_history 587 history
三、echo $HISTSIZE 显示存放最大多少条历史记录
#history存储使用命令的最大数1000条,变量HISTSIZE [root@aminglinux-01 ~]# echo $HISTSIZE 1000
四、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配置文件里面去,是先存储在内存里,只有当你退出终端的时候才会保存到配置文件里。
五、定义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
六、定义格式: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
七、给命令历史文件增长特殊权限
#为了让命令历史永久保存,不想让其它人去破坏它,能够给它加一个特殊的a权限,不能删除,能够追加。 [root@aminglinux-01 ~]# chattr +a ~/.bash_history [root@aminglinux-01 ~]# lsattr .bash_history -----a---------- .bash_history 注意:若是执行了这条命令后,你并无正常退出终端,就会致使保存的命令不全。
八、!! 连续两个,!表示执行上一条命令。
[root@xietaolinux3 ~]# lsattr .bash_history -----a---------- .bash_history [root@xietaolinux3 ~]# !! lsattr .bash_history -----a---------- .bash_history
九、!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
十、!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
一、tab键,敲一下,敲两下(按tab键能够帮咱们补全一个指令,一个路径或者一个文件名) 二、参数补全,安装bash-completion ,重启系统生效 适用于centos7 三、alias别名给命令从新起个名字 四、各用户都有本身配置别名的文件 ~/.bashrc(存放别名的路径地址) 五、ls /etc/profile.d/(存放别名的路径地址) 六、自定义的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也是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>以及追加剧定向命令>>。
一、 cat 1.txt >2.txt #>它会把命令产生的正确信息输出到指定文件里去,删除以前文件内容重写。 二、 cat 1.txt >> 2.txt #>>把前面命令输出内容重定向追加到后面命令里去,不删除旧的。 三、ls aaa.txt 2> err #它会把命令产生的错误信息输出到指定文件里去 四、ls aaa.txt 2>> err #错误信息输出追加剧定向 五、2>与2>>等于&> #&>结合了正确与错误 六、 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
#使用错误命令输出一条错误信息 [root@aminglinux-01 ~]# lsaaa -bash: lsaaa: 未找到命令 #保存一条信息到指定文件里去 [root@aminglinux-01 ~]# lsaaa 2> a.txt #查看结果 [root@aminglinux-01 ~]# cat a.txt -bash: lsaaa: 未找到命令
#追加一次错误信息到文件 [root@aminglinux-01 ~]# lsaaa 2>> a.txt #查看结果 [root@aminglinux-01 ~]# cat a.txt -bash: lsaaa: 未找到命令 -bash: lsaaa: 未找到命令
#&>结合正确错误信息重定向到一个文件里去 [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: 未找到命令