欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux学习lesson23

程序员文章站 2022-05-31 16:14:08
...

目录

1 shell介绍

2 命令历史

3 命令补全和别名

4 通配符

5 输入输出重定向


1 shell介绍

shell是系统跟计算机硬件交互时使用的中间介质,它只是系统的一个工具。实际上,在shell和计算机硬件之间还有一层东西——系统内核。如果把计算机硬件比作一个人的躯体,那系统内核就是人的大脑。至shell,把它比作人的五官似乎更贴切些。言归正传,用户直接面对的不是计算机硬件而shell,用户把指令告诉shell,然后shell再传输给系统内核,接着内核再去支配计算机硬件去执行各种操作

  • shell是一个命令解释器,提供用户和机器之间的交互
  • 支持特定语法,比如逻辑判断、循环
  • 每个用户都可以有自己特定的shell
  • CentOS7默认shell为bash(Bourne Agin Shell)
  • 还有zsh、ksh等

 

2 命令历史

~/.bash_history命令历史记录文件,需要正常退出系统才会记录的

[[email protected] ~]# cat ~/.bash_history | tail -20
ls
tar -zcvf dir123.tar.gz 123/
ls
tar -xcvf dir123.tar.gz
tar -zxvf dir123.tar.gz
ls
tar -jcvf dir.tar.bz2 dir123
ls
tar -jxvf dir.tar.bz2
ls
clear
ls
tar -Jcvf dir123.tar.xz dir123/
ls
tar -Jxvf dir123.tar.xz dir123/
ls
tar -tf dir123.tar.gz
tar -tf dir.tar.bz2
tar -tf dir123.tar.xz
init 0

变量HISTSIZE是设置记录历史命令的数量(默认是1000)

[[email protected] ~]# echo $HISTSIZE
1000
  • 当前内存的历史命令
[[email protected] ~]# history | tail -20
  975  yum install -y gcc
  976  ./configure --prefix=/usr/local/apache2.2
  977  echo $?
  978  make
  979  echo $?
  980  make install
  981  ls /usr/local/apache2.2/
  982  ls
  983  ls /usr/local/apache2.2/
  984  clear
  985  cd
  986  ls ~/.bash_history
  987  cat ~/.bash_history | wc -l | head
  988  cat ~/.bash_history | head -20
  989  cat ~/.bash_history | tail -20
  990  echo #HISTSIZE
  991  echo $HISTSIZE
  992  history
  993  history | head -20
  994  history | tail -20
  • 清除内存历史命令
[[email protected] ~]# history -c
[[email protected] ~]# history
    1  history

/etc/profile中可以修改存的命令历史数量

[[email protected] ~]# vim /etc/profile

    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then

其中HISTSIZE=1000就是修改记录历史命令数量 

  • 设置记录历史命令附加上时间

永久生效:
/etc/profile配置文件添加上HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
需要退出终端,重新登陆即可生效

[[email protected] ~]# history | tail -20
  952  2018/10/10 01:37:55 tar -Jcvf dir123.tar.xz dir123/
  953  2018/10/10 01:37:55 ls
  954  2018/10/10 01:37:55 tar -Jxvf dir123.tar.xz dir123/
  955  2018/10/10 01:37:55 ls
  956  2018/10/10 01:37:55 tar -tf dir123.tar.gz
  957  2018/10/10 01:37:55 tar -tf dir.tar.bz2
  958  2018/10/10 01:37:55 tar -tf dir123.tar.xz
  959  2018/10/10 01:37:55 init 0
  960  2018/10/10 01:37:55 history
  961  2018/10/10 01:37:55 vim /etc/profile
  962  2018/10/10 01:37:55 ls
  963  2018/10/10 01:37:55 history
  964  2018/10/10 01:37:55 logout
  965  2018/10/10 01:37:59 history
  966  2018/10/10 01:38:11 history | tail -20
  967  2018/10/10 01:38:35 vim /etc/profile
  968  2018/10/10 01:38:46 source $!
  969  2018/10/10 01:38:56 source /etc/profile
  970  2018/10/10 01:38:59 history
  971  2018/10/10 01:39:26 history | tail -20

临时生效: 

[[email protected] ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[[email protected] ~]# history | tail -20
  980  2018/10/10 01:40:10   957  2018/10/10 01:37:55 tar -tf dir.tar.bz2
  981  2018/10/10 01:40:10   958  2018/10/10 01:37:55 tar -tf dir123.tar.xz
  982  2018/10/10 01:40:10   959  2018/10/10 01:37:55 init 0
  983  2018/10/10 01:40:10   960  2018/10/10 01:37:55 history
  984  2018/10/10 01:40:10   961  2018/10/10 01:37:55 vim /etc/profile
  985  2018/10/10 01:40:10   962  2018/10/10 01:37:55 ls
  986  2018/10/10 01:40:10   963  2018/10/10 01:37:55 history
  987  2018/10/10 01:40:10   964  2018/10/10 01:37:55 logout
  988  2018/10/10 01:40:10   965  2018/10/10 01:37:59 history
  989  2018/10/10 01:40:10   966  2018/10/10 01:38:11 history | tail -20
  990  2018/10/10 01:40:10   967  2018/10/10 01:38:35 vim /etc/profile
  991  2018/10/10 01:40:10   968  2018/10/10 01:38:46 source $!
  992  2018/10/10 01:40:10   969  2018/10/10 01:38:56 source /etc/profile
  993  2018/10/10 01:40:10   970  2018/10/10 01:38:59 history
  994  2018/10/10 01:40:10   971  2018/10/10 01:39:26 history | tail -20
  995  2018/10/10 01:40:14 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
  996  2018/10/10 01:40:17 history
  997  2018/10/10 01:41:00   971  2018/10/10 01:39:26 history | tail -20
  998  2018/10/10 01:41:08 hist| tail -20
  999  2018/10/10 01:41:12 history | tail -20

若想永久保存历史命令,设置追加特殊权限

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

常见快捷命令操作

!! 最后一条命令

!n n代表数字,表示第几条命令

!word word表示以word开头的命令从后往前找并执行

 

 

3 命令补全和别名

tab键

敲一下:如果输入字符是唯一,可以立刻补全

敲两下:如果输入字符有相同的多个,列出具有相同字符的命令

  • 参数补全
[[email protected] ~]# yum install -y bash-completion   //需要重启系统
  • alias别名给命令重新起个名字
[[email protected] ~]# alias 'restartnet=systemctl restart network.service'
[[email protected] ~]# restartnet
  • 永久定义别名,设置配置文件即可

法1:

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

法2:

cd /etc/profile.d/

根据模板添加自定义的sh配置文件

  • 取消别名
[[email protected] ~]# unalias restartnet
[[email protected] ~]# restartnet
-bash: restartnet: command not found

 

4 通配符

"*"表任意字符

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

"?"表示任意一个字符

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

"[0-9]"表示0-9的数字范围

[[email protected] ~]# ls [0-9].txt
1.txt  2.txt  3.txt

{1,2}表示1或2

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

 

5 输入输出重定向

">"重定向

[[email protected] ~]# cat 1.txt
hello world
[[email protected] ~]# cat 1.txt > 2.txt
[[email protected] ~]# cat 2.txt
hello world

">>"追加重定向

[[email protected] ~]# cat 2.txt
hello world
[[email protected] ~]# cat 1.txt >> 2.txt
[[email protected] ~]# cat 2.txt
hello world
hello world

"2>"错误重定向

[[email protected] ~]# ls a.txt 2> err
[[email protected] ~]# ls
1.txt  2.txt  3.txt  anaconda-ks.cfg  dir1  dir6  err  test.txt
[[email protected] ~]# cat err
ls: cannot access a.txt: No such file or directory

"2>>"错误追加重定向

[[email protected] ~]# ls a.txt 2>> err
[[email protected] ~]# cat err
ls: cannot access a.txt: No such file or directory
ls: cannot access a.txt: No such file or directory

"<"输入重定向

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

"&>"表示错误和正确的同时输出到一个文件

[[email protected] ~]# cat 1.txt a.txt &> 2.txt
[[email protected] ~]# cat 2.txt
hello world
cat: a.txt: No such file or directory
等价于
[[email protected] ~]# cat 1.txt a.txt > 2.txt 2>&1

 

相关标签: centos shell