8.1 shell 介绍8.2 命令历史8.3 命令补全与别名8.4 通配符8.5 输入输出重定向

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了8.1 shell 介绍8.2 命令历史8.3 命令补全与别名8.4 通配符8.5 输入输出重定向相关的知识,希望对你有一定的参考价值。

8.1 sehll 介绍

什么是shell

shell 是一个命令解释器,提供用户和机器之间交互

支持特定的语法,比如逻辑判断,循环.

每个用户都可以有自己特定的shell.

centos7 默认的shell 为bash( Bourne Agin shell )

还有zsh ,ksh等

8.2 命令历史

/root/.bash_history ;命令历史放置文件

[[email protected] ~]# ls /root/.bash_history/root/.bash_history

[[email protected] ~]# cat !$cat /root/.bash_history

history ;查看系统存储的命令,最多可以存储1000条

[[email protected] ~]# history | tail
  995  make install  996  echo $?  997  ls /usr/local/apache2  998  init 0
  999  ls /root/.bash_history 1000  cat $! 1001  ls /root/.bash_history 1002  cat /root/.bash_history 1003  history 1004  history | tail
[[email protected] ~]#

可以看到有1004 条,是因为还没有写入到文件中,暂时存在于内存中

echo $HISTSIZE ; 查看变量的值

[[email protected] ~]# echo $HISTSIZE1000[[email protected] ~]#

history -c ; 清空内存中的历史命令,并不会清空 /root/.bash_history 中保存的命令

[[email protected] ~]# history -c[[email protected] ~]# history
    8  history
    [[email protected] ~]# cat .bash_history | tailcd ..lscd httpd-2.2.32 lsmakeecho $?make installecho $?ls /usr/local/apache2
init 0[[email protected] ~]#

当退出当前终端时,内存中保存的命令会保存到 /root/.bash_history 文件中去

vim /etc/profile ; 在配置文件中修改 HISTTORY 变量的值

[[email protected] ~]# vim /etc/profileHOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=2000if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth

source /etc/profile ; 重新加载文件之后,HISTSIZE 变量的值在会改变

[[email protected] ~]# echo $HISTSIZE1000[[email protected] ~]# source /etc/profile[[email protected] ~]# echo $HISTSIZE2000[[email protected] ~]#

HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " ; 设置命令历史显示格式

[[email protected] ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " [[email protected] ~]# echo $HISTTIMEFORMAT%Y/%m/%d %H:%M:%S
[[email protected] ~]# history
    8  2017/06/25 12:27:07 history    9  2017/06/25 12:31:05 cat .bash_history   10  2017/06/25 12:31:14 cat .bash_history | tail   11  2017/06/25 12:35:08 cat /etc/profile   12  2017/06/25 13:10:00 vim /etc/profile   13  2017/06/25 13:14:38 echo $HISTORY   14  2017/06/25 13:15:34 echo $HISTTORY   15  2017/06/25 13:16:10 echo $HISTSIZE   16  2017/06/25 13:16:47 source /etc/profile   17  2017/06/25 13:17:13 echo $HISTSIZE   18  2017/06/25 13:19:55 history   19  2017/06/25 13:25:16 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 
   20  2017/06/25 13:25:37 echo $HISTTIMEFORMAT   21  2017/06/25 13:25:49 history
[[email protected] ~]#

只在当前终端生效,如果要在别的中终端生效,需要将变量加入到/etc/profile中

[[email protected] ~]# vim /etc/profileHISTSIZE=2000HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "[[email protected] ~]# source /etc/profile

chattr +a /root/.bash_history ; 给文件设置 a 权限,防止被删除

[[email protected] ~]# chattr +a /root/.bash_history[[email protected] ~]# lsattr /root/.bash_history-----a---------- /root/.bash_history
[[email protected] ~]#

!! 表示执行上一条命令,也就是命令历史中的最后一条命令

[[email protected] ~]# ls111.txt  1.txt  1.txt.1  2.txt  3.txt  anaconda-ks.cfg  httpd-2.2.32.tar.gz
[[email protected] ~]# !!ls111.txt  1.txt  1.txt.1  2.txt  3.txt  anaconda-ks.cfg  httpd-2.2.32.tar.gz

[[email protected] ~]# w
 13:43:44 up  5:12,  2 users,  load average: 0.00, 0.01, 0.05USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.86.1     08:32    8.00s  0.21s  0.06s w
root     pts/1    192.168.86.1     13:33    9:04   0.03s  0.03s -bash
[[email protected] ~]# !!w 13:43:46 up  5:12,  2 users,  load average: 0.00, 0.01, 0.05USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.86.1     08:32    2.00s  0.16s  0.01s w
root     pts/1    192.168.86.1     13:33    9:06   0.03s  0.03s -bash
[[email protected] ~]#

!n ;表示执行命令历史里的第n条命令,n 表示第几条命令

[[email protected] ~]# history
    8  2017/06/25 12:27:07 history    9  2017/06/25 12:31:05 cat .bash_history   10  2017/06/25 12:31:14 cat .bash_history | tail   11  2017/06/25 12:35:08 cat /etc/profile   12  2017/06/25 13:10:00 vim /etc/profile   13  2017/06/25 13:14:38 echo $HISTORY   14  2017/06/25 13:15:34 echo $HISTTORY   15  2017/06/25 13:16:10 echo $HISTSIZE   16  2017/06/25 13:16:47 source /etc/profile   17  2017/06/25 13:17:13 echo $HISTSIZE   18  2017/06/25 13:19:55 history   19  2017/06/25 13:25:16 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 
   20  2017/06/25 13:25:37 echo $HISTTIMEFORMAT   21  2017/06/25 13:25:49 history   22  2017/06/25 13:31:12 vim /etc/profile   23  2017/06/25 13:33:21 source /etc/profile   24  2017/06/25 13:39:08 chattr +a /root/.bash_history   25  2017/06/25 13:39:38 lsattr /root/.bash_history   26  2017/06/25 13:43:18 ls   27  2017/06/25 13:43:40 W   28  2017/06/25 13:43:43 w   29  2017/06/25 13:45:46 history
[[email protected] ~]# !17echo $HISTSIZE2000[[email protected] ~]#

!+字符串 ;表示执行以字符串开头的,history中最近的一条命令

[[email protected] ~]# history | tail -n 7
   29  2017/06/25 13:45:46 history   30  2017/06/25 13:46:00 echo $HISTSIZE   31  2017/06/25 13:50:11 w   32  2017/06/25 13:53:03 history | tail   33  2017/06/25 13:53:18 history | tail -n 3
   34  2017/06/25 13:53:32 history | tail -n3   35  2017/06/25 13:53:47 history | tail -n 7[[email protected] ~]# [[email protected] ~]# !echoecho $HISTSIZE2000[[email protected] ~]# !ww 13:50:11 up  5:19,  2 users,  load average: 0.00, 0.01, 0.05USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.86.1     08:32    3.00s  0.17s  0.01s w
root     pts/1    192.168.86.1     13:33   15:31   0.03s  0.03s -bash
[[email protected] ~]#

8.3 命令补全与别名

tab 键命令补全

  • 敲一下 补全命令或路径

  • 敲两下 列出当前目录下的所有文件

yum install -y bash-completion ;安装包之后tab键可以补全命令的参数

  • 安装完之后需要重新启动系统,才能生效

[[email protected] ~]# rpm -q bash-completionbash-completion-2.1-6.el7.noarch
[[email protected] ~]# systemctl restart network

alias restartnet=‘systemctl restart network.service‘ ; 设置别名

[[email protected] ~]# alias restartnet=‘systemctl restart network.service‘[[email protected] ~]# restartnet[[email protected] ~]#

alias ; 列出系统所有的别名

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

/root/.bashrc /etc/profile.d ; 定义alias 的文件

  • /root/.bashrc

[[email protected] ~]# cat /root/.bashrc# .bashrc# User specific aliases and functionsalias rm=‘rm -i‘alias cp=‘cp -i‘alias mv=‘mv -i‘# Source global definitionsif [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
[[email protected] ~]#
  • /etc/profile.d 下的脚本文件中

[[email protected] ~]# ls /etc/profile.d256term.csh         colorgrep.csh  colorls.sh  less.csh  vim.sh256term.sh          colorgrep.sh   lang.csh    less.sh   which2.csh
bash_completion.sh  colorls.csh    lang.sh     vim.csh   which2.sh

unalias restartnet ; 取消自定义的别名

[[email protected] ~]# unalias restartnet[[email protected] ~]# restartnet-bash: restartnet: 未找到命令
[[email protected] ~]#

8.4 通配符

* 任意个任意字符

[[email protected] ~]# ls *.txt111.txt  1.txt  2.txt  3.txt

[[email protected] ~]# ls *txt*111.txt  1.txt  1.txt.1  2.txt  3.txt

[[email protected] ~]# ls 1*111.txt  1.txt  1.txt.1[[email protected] ~]#

? ;表示一个任意字符

[[email protected] ~]# ls ?.txt1.txt  2.txt  3.txt
[[email protected] ~]#

ls [1-9].txt ; 列出包含数字的文件 [0-9],[123],[23],[a-z],[A-Z],[0-9a-zA-Z]

[[email protected] ~]# ls [0-9].txt1.txt  2.txt  3.txt
[[email protected] ~]# le [23].txt-bash: le: 未找到命令
[[email protected] ~]# ls [23].txt2.txt  3.txt
[[email protected] ~]#

ls {1,2,3}.txt ; 列出包含花括号当中的文件

[[email protected] ~]# ls {1,2}.txt1.txt  2.txt


[[email protected] ~]# ls {1,2,3}.txt1.txt  2.txt  3.txt
[[email protected] ~]#

8.5 输入输出重定向

cat 1.txt > 2.txt ;重定向,将前一个命令的结果重定向到后面的文件中

[[email protected] ~]# cat  2.txt1234567890[[email protected] ~]# cat 1.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin

[[email protected] ~]# cat 1.txt > 2.txt[[email protected] ~]# cat 2.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin
[[email protected] ~]#

cat 1.txt >> 2.txt ; 追加重定向,将前面命令的输出追加到后面的文件中

[[email protected] ~]# cat 1.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin

[[email protected] ~]# cat 2.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin

[[email protected] ~]# cat 1.txt >> 2.txt[[email protected] ~]# cat 2.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologinchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin
[[email protected] ~]#

lsaa 2> a.txt ; 错误重定向

[[email protected] ~]# touch a.txt[[email protected] ~]# lsaaa -bash: lsaaa: 未找到命令
[[email protected] ~]# lsaaa 2> a.txt[[email protected] ~]# cat a.txt-bash: lsaaa: 未找到命令
[[email protected] ~]#

lsaa 2>> a.txt ; 追加错误重定向

[[email protected] ~]# cat a.txt-bash: lsaaa: 未找到命令
[[email protected] ~]# lsaaa 2>> a.txt[[email protected] ~]# lsaaa 2>> a.txt[[email protected] ~]# cat a.txt-bash: lsaaa: 未找到命令
-bash: lsaaa: 未找到命令
-bash: lsaaa: 未找到命令
[[email protected] ~]#

ls [23].txt aaa.txt &> a.txt ; &> 相当于 2> + >

[[email protected] ~]# ls [23].txt  aaa.txt  &> a.txt[[email protected] ~]# cat a.txtls: 无法访问aaa.txt: 没有那个文件或目录
2.txt3.txt[[email protected] ~]#

ls [23].txt aaa.txt &>> a.txt ; &>> 相当于 2>> + >>

[[email protected] ~]# ls [23].txt  aaa.txt  &>> a.txt[[email protected] ~]# cat a.txtls: 无法访问aaa.txt: 没有那个文件或目录
2.txt3.txtls: 无法访问aaa.txt: 没有那个文件或目录
2.txt3.txt[[email protected] ~]#

ls [23].txt aaa.txt > 1.txt 2> a.txt ; 将正确输出和错误输出到不同的文件中

[[email protected] ~]# ls [23].txt  aaa.txt  > 1.txt  2> a.txt[[email protected] ~]# cat 1.txt2.txt3.txt
[[email protected] ~]# cat a.txtls: 无法访问aaa.txt: 没有那个文件或目录
[[email protected] ~]#

wc -l <2.txt ; 输入重定向,左边一定是一个命令,不能是文件

[[email protected] ~]# cat 2.txtchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologinchrony:x:997:995::/var/lib/chrony:/sbin/nologinaming:x:1000:1000::/home/aming:/bin/bashuser1:x:1001:1001::/home/user1:/bin/bashtcpdump:x:72:72::/:/sbin/nologinnginx:x:996:994:Nginx web server:/var/lib/nginx:/sbin/nologin

[[email protected] ~]# wc -l <2.txt10[[email protected] ~]#


以上是关于8.1 shell 介绍8.2 命令历史8.3 命令补全与别名8.4 通配符8.5 输入输出重定向的主要内容,如果未能解决你的问题,请参考以下文章

五周第三次课 8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.

8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重

8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重

8.1 shell 介绍8.2 命令历史8.3 命令补全与别名8.4 通配符8.5 输入输出重定向

8.1 shell介绍 8.2 命令历史 8.3 命令补全和别名 8.4 通配符 8.5 输入输出重

8.1 shell介绍8.2 命令历史8.3 命令补全和别名8.4 通配符8.5 输入输出重定向