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



8.1 shell介绍


Linux Shell基础

介绍shell的特性,用法。

8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向_学习

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

  • 支持特定语法,比如逻辑判断、循环。

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


CentOS7默认shell为bash(Bourne Agin Shell)

Bourne是一个用户,他开发的shell是sh.

后来CentOS7默认使用的shell是bash,基于sh优化而来。


shell还有zsh、ksh等


8.2 命令历史


history命令

8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向_笔记_02

.bash_history

history文件,存放命令

# cat /root/.bash_history

8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向_笔记_03


历史命令最大可以存1000条 (要修改最大值,可以修改/etc/profile文件)

查看变量HISTSIZE值, 

# echo $HISTSIZE 
1000

有时候敲命令,可以敲到1000+ ,这是因为这些命令还没有真正写到文件里,因为现在运行的命令是暂时存在内存中。



#history -c

清空当前命令历史,不能清空相关历史配置文件的,对配置文件无任何影响。只有退出终端,命令历史记录才会被写入文件中。


要改变$HISTSIZE,需在/etc/profile中修改

修改profile中的history参数之后,需要让修改生效,利用#source /etc/profile 

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

8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向_笔记_04

[root@centos7 ~]# echo $HISTSIZE
1000
[root@centos7 ~]# source /etc/profile
[root@centos7 ~]# echo $HISTSIZE 
5000

用时间显示使用过的命令历史记录。

使用参数:HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

[root@centos7 ~]# echo HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

Y年m月d日H时M分钟S秒

HISTTIMEFORMAT=%Y/%m/%d %H:%M:%S

[root@centos7 ~]# vi /etc/profile修改profile文件

8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向_学习_05

[root@centos7 ~]# source /etc/profile
使参数修改生效
[root@centos7 ~]# history 
    6  2018/03/07 15:08:26 history 
    7  2018/03/07 15:08:32 ls
    8  2018/03/07 15:08:33 history 
    9  2018/03/07 15:09:16 cd /etc/
   10  2018/03/07 15:09:19 cd -
   11  2018/03/07 15:09:23 history

用久保存

 chattr +a ~/.bash_history

如果不正常退出,保存的命令会不全。


  • !! 上一条命令,则history的最后一条命令。

  • !n  !接n条命令。例如!6 执行history的第6条命令

  • !word 从history倒退网上找最近一条相关word命令。例如!echo,从history里找最后一条echo的命令。


# history 
      26  2018/03/07 16:02:29 history 
   27  2018/03/07 16:04:17 echo HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 
   28  2018/03/07 16:04:29 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
   29  2018/03/07 16:06:06 history 
# !echo
echo HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 
HISTTIMEFORMAT=%Y/%m/%d %H:%M:%S



8.3 命令补全和别名


命令补全及别名

8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向_笔记_06

tab键,敲一下,敲两下

敲一下补全效果,

敲两下,列出相关补全文件或目录名


系统参数命令在未安装bash-completion之前,是不能补全的,例如敲入:

#systemctl restart network.service

会发现未安装之前是不能全部敲入的。


参数补全,安装bash-completion,安装完成后重启。

#yum install -y bash-completion
# rpm -qa bash-completion安装成功,
bash-completion-2.1-6.el7.noarch

alias别名给命令重新起个名字

给systemctl restart network.service做一个alias,名为:restartnet

[root@centos7 ~]# alias restartnet='systemctl restart network.service'
[root@centos7 ~]# restartnet
[root@centos7 ~]# 
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'

alias存放目录 

各用户都有自己配置别名的文件 ~/.bashrc

ls /etc/profile.d/

自定义的alias放到 ~/.bashrc

~/.bashrc 部分在这

/etc/profile.d/  
grep,colors


#unalias restartnet取消restartnet的alias



8.4 通配符


介绍

8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向_学习_07

*用法

[root@centos7 ~]# ls
1.txt  1.xtx  2.txt  a_(2).txt  anaconda-ks.cfg  anaconda-ks.cfg.1  a.txt  temp.1
[root@centos7 ~]# ls *txt目录下所有TXT文件
1.txt  2.txt  a_(2).txt  a.txt
[root@centos7 ~]# ls 1*txt目录下1开头的TXT文件
1.txt
[root@centos7 ~]# ls *2*.txt目录下名称含有2的TXT文件
2.txt  a_(2).txt

?用法

[root@centos7 ~]# touch 3.txt 4.txt a.txt bb.txt
[root@centos7 ~]# ls
1.txt  1.xtx  2.txt  3.txt  4.txt  a_(2).txt  anaconda-ks.cfg  anaconda-ks.cfg.1  a.txt  bb.txt  temp.1
[root@centos7 ~]# ls ?.txt 文件名只有一个字的TXT文件名
1.txt  2.txt  3.txt  4.txt  a.txt
[root@centos7 ~]# ls ??.txt 文件名有两个字的TXT文件名
bb.txt

[]数字范围用法,支持特定数字,也支持特定范围。

[root@centos7 ~]# ls [0-4].txt
1.txt  2.txt  3.txt  4.txt
[root@centos7 ~]# ls [14].txt
1.txt  4.txt
[root@centos7 ~]# ls [1234].txt
1.txt  2.txt  3.txt  4.txt

英文和数字用法

[root@centos7 ~]# touch A.txt
[root@centos7 ~]# ls [0-9a-zA-Z].txt
1.txt  2.txt  3.txt  4.txt  a.txt  A.txt

{}用法,例如{1,2,3}=[1-3]

[root@centos7 ~]# ls {1,2,3}.txt
1.txt  2.txt  3.txt
[root@centos7 ~]# ls [1-3].txt
1.txt  2.txt  3.txt

8.5 输入输出重定向


8.1-8.5 shell介绍,命令历史,命令补全和别名,通配符,输入输出重定向_笔记_08


输出重定向>

cat 1.txt >2.txt

 把1.txt输出的内容结果重定向输入到2.txt的文件里。

cat 1.txt >>2.txt

>>和>差不多,只是>>是追加,>>不把1.txt的文件内容删掉,会保留着,并且输入到2.txt,这个动作叫做追加。

>把前面输出的内容结果,重定向输入到后面的文件里。(前面被重定向的内容会被删掉)



ls aaa.txt 2>4.txt lsaaa.txt 
产生的错误信息指定输出到4.txt里
[root@centos7 ~]# ls aaa.txt 2> 4.txt
[root@centos7 ~]# cat 4.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
ls aaa.txt 2>>4.txtlsaaa.txt 产生的错误信息指定输出追加到4.txt里

2>产生的错误信息指定输出到指定文件里
2>>产生的错误信息指定输出追加到指定文件里
>正确的信息,2>错误的信息

>+2>=&> &>=>正确信息 2>的结合



&>举例演示

[root@centos7 ~]# ls [1-2].txt aaa.txt &>a.txt
[root@centos7 ~]# cat a.txt 
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt

&>>演示

[root@centos7 ~]# ls [1-2].txt aaa.txt &>>a.txt
[root@centos7 ~]# cat a.txt 
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt

用2个文件区分 正确与错误输出结果

[root@centos7 ~]# ls [1-2].txt aaaa.txt >1.txt 2>a.txt
[root@centos7 ~]# cat 1.txt 
1.txt
2.txt
[root@centos7 ~]# cat a.txt 
ls: 无法访问aaaa.txt: 没有那个文件或目录

输入重定向<,格式一般是:命令 < 文件 (这个比较少用)

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