day01:shell基础(shell基础、alias及重定向)

一、shell基础介绍:shell

    shell是一个命令解释器,用于用户与机器的交互:vim

    也支持特定的语法(逻辑判断,循环):bash

   每一个用户都有本身特定的shell:Centos7的shell是bash(bourne   agin  shell):less

   shell还有zsh    ksh这两种:spa

二、命令历史:  history code

 命令历史存放路径:用户的家目录:用history命令能够查看命令历史:内存

[root@localhost ~]# ls -ld /root/.bash_history      #命令历史所在的家目录
-rw------- 1 root root 13038 Jun 30 22:36 /root/.bash_history
[root@localhost ~]# history                         #可直接查看系统中命令历史
  994  yum list |grep zsh
  995  cat .bash_history
  996  ls -ld /root/.bash_history
  997  history
  998  cat .bash_history
  999  history
 1000  exit
 1001  clear
 1002  history
 1003  history |tail -10

2.1:history能够存放1000条命令的数目:是有环境变量HISTSIZE决定的:ci

[root@localhost ~]# echo $HISTSIZE
1000

注:有时超过1000条后再写入时则暂时存在于内存中,当正常退出终端后才写入到文件:it

2.2:history  -c :只清空当前内存中的命令,不会清除配置文件中的历史命令:io

[root@localhost ~]# history             #第一次看出命令历史记录
 1001  clear
 1002  history
 1003  history |tail -10
 1004  echo $HISTSIZE
 1005  history |tail -10
 1006  history |tail -6
[root@localhost ~]# history -c         #清除当前内存的命令历史
[root@localhost ~]# history            #再次查看则没有历史记录,当从新开启终端后,则再次显示:
    8  history

2.3:history命令历史的显示数目可定义:以下:

[root@localhost ~]# vim /etc/profile                    #编辑此配置文件:
[root@localhost ~]# cat /etc/profile |grep -C3 HISTSIZE
fi
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=5000                                    #修改此命令条目为5000:
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi
[root@localhost ~]# echo   HISTSIZE=5000   
[root@localhost ~]# source /etc/profile         #保存后须要刷新才能生效:
[root@localhost ~]# echo $HISTSIZE              再次查看命令历史:
5000

/etc/porfile文件里,搜索关键字"HIST"找到HISTSIZE,在此更改其数字,保存退出,而后执行/source   /etc/profile命令刷新该配置才能生效:

2.4:更改hsitory的显示格式: HISTTIMEFORMAT

[root@localhost ~]# HISTTIMEFORMAT="%Y-%m-%d %H-%M-%S "    #直接给变量赋值便可:
[root@localhost ~]# history |tail -4                     #按年月日来显示:
 1023  2018-07-04 11-39-25 history
 1024  2018-07-04 11-39-33 HISTTIMEFORMAT="%Y-%m-%d %H-%M-%S "
 1025  2018-07-04 11-39-35 history
 1026  2018-07-04 11-39-40 history |tail -4
[root@localhost ~]# su - yuanhh                      #切换到另外一个普通用户:
Last login: Wed Jul  4 11:28:53 CST 2018 on pts/1
[yuanhh@localhost ~]$ history                        #则不显示:
    1  history \
    2  history
    3  echo $HISTSIZE
    4  EXIT
    5  exit
    6  history

如上:修改命令历史的格式直接给其变量赋值便可,不过该格式只适用于当前终端,若是要其使用与全部用户,则须要写入到history的配置文件才会生效:

[root@localhost ~]# vim /etc/profile             #修改此文件
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTTIMEFORMAT="%Y-%m-%d %H-%M-%S"               #新增添加这一行了:
HISTSIZE=5000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
[root@localhost ~]# ^C
[root@localhost ~]# source /etc/profile
[root@localhost ~]# su - yuanhh                 #切换到普通用户查看:
Last login: Wed Jul  4 11:37:41 CST 2018 on pts/0
[yuanhh@localhost ~]$ history                   #命令历史显示格式发生变化:
    1  2018-07-04 11-46-54history \
    2  2018-07-04 11-46-54history
    3  2018-07-04 11-46-54echo $HISTSIZE
    4  2018-07-04 11-46-54EXIT

2.5:命令历史永久保存命令历史只能写入不能删除

[root@localhost ~]# chattr +a /root/.bash_history
[root@localhost ~]# lsattr /root/.bash_history
-----a-------e-- /root/.bash_history

给文件增长+a权限,只能追加,不能删除:也仅仅root用户能够操做:

三、!!命令:

!!:两个叹号表示当前命令历史中的最后一条命令:等同与上翻按键:

!n:n等于数字,表示命令历史中的第n条命令:

!word:表示命令历史中以该word命令开头的命令:

[root@localhost ~]# pwd
/root
[root@localhost ~]# !!
pwd
/root
1033  2018-07-04 11-59-01ls
 1034  2018-07-04 11-59-06pwd
 1035  2018-07-04 11-59-56history
[root@localhost ~]# !1033               #表示第1033条命令:
ls
[root@localhost ~]# !p                  #表示以p开头的命令:
pwd
/root

四、命令补全和别名:  tab     alias  unalias

tab:补全命令:须要安装包:bash-completion   并从新启动系统便可:

按一次tab:补全一个命令或者参数:

按两次tab:补全某字母开头的全部命令及文件名:

4.1:命令别名:alias    unalias

alias              命令别名="具体的命令"                   #设置别名:

unalias         命令别名                                          #取消别名:

[root@localhost ~]# alias p="pwd"             #设置别名”pwd“:
[root@localhost ~]# p
/root 
[root@localhost ~]# unalias p                 #取消别名:
[root@localhost ~]# p
-bash: p: command not found

4.2:输入"alias"可查看系统中全部的别名:

[root@localhost ~]# 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'

4.2:系统别名的存放在配置文件~/.bashrc/etc/profile.d/下:

[root@localhost ~]# cat ~/.bashrc              #当前用户下.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
[root@localhost ~]# ls /etc/profile.d/
256term.csh         colorls.csh         less.sh
256term.sh          colorls.sh          vim.csh
bash_completion.sh  lang.csh            vim.sh               
colorgrep.csh       lang.sh             which2.csh            
colorgrep.sh        less.csh            which2.sh

当前用户目录下的.bashrc只对当前用户生效:

/etc/profile.d/目录下的针对于系统中全部用户生效:

五、通配符:  *     ?    [  ]     {    }

*表示匹配零个或多个字符

?表示匹配一个字符

[  ]表示匹配一个范围,中括号区间内的任意一个文件

{  }表示匹配花括号内的任意一个文件

[root@localhost ~]# ls
1.txt  2.txt  3.txt  ba.txt.c  cat.txt  dir1  pk.txt
[root@localhost ~]# ls *.txt             #"*"匹配一个或者多个文件:
1.txt  2.txt  3.txt  cat.txt  pk.txt
[root@localhost ~]# ls *txt*
1.txt  2.txt  3.txt  ba.txt.c  cat.txt  pk.txt
[root@localhost ~]# ls       
1.txt  2.txt  3.txt  ba.txt.c  cat.txt  dir1  pk.txt
[root@localhost ~]# ls ?.txt               #”?“只匹配一个字符:
1.txt  2.txt  3.txt
[root@localhost ~]# ls
1.txt  3.txt  5.txt  ba.txt.c  dir1
2.txt  4.txt  6.txt  cat.txt   pk.txt
[root@localhost ~]# ls [1-5].txt          #表示匹配1--5之间的任意.txt文件:
1.txt  2.txt  3.txt  4.txt  5.txt
[root@localhost ~]# ls
1.txt  3.txt  5.txt  ba.txt.c  dir1
2.txt  4.txt  6.txt  cat.txt   pk.txt
[root@localhost ~]# ls {1,2,3}.txt          #匹配花括号内的txt文件:
1.txt  2.txt  3.txt

六、输入输出重定向:   >    >>   2>   2>>   &>     <

>:输出重定向

>>:追加剧定向

2>:错误重定向

2>>:错误追加剧定向

&>:正确错误重定向

<:输入重定向

5.1:使用">"这个命令时会将源文件内容删除:

[root@localhost ~]# echo "111111" > 1.txt          #写入文件:
[root@localhost ~]# cat 1.txt
111111
[root@localhost ~]# echo "222222" > 2.txt          #再次重定向写入文件:
[root@localhost ~]# cat 2.txt                      #会删除源文件:
222222

使用">>"至关于追加,不删除源文件内容:

[root@localhost ~]# cat 2.txt
222222
[root@localhost ~]# echo "333333" >> 2.txt      #再次追加内容:
[root@localhost ~]# cat 2.txt                   #源文件内容不删除:
222222
333333

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

[root@localhost ~]# lsaaa              
-bash: lsaaa: command not found
[root@localhost ~]# lsaaa 2> 11.txt           #错误重定向:
[root@localhost ~]# cat 11.txt
-bash: lsaaa: command not found
[root@localhost ~]# ls222 2>> 11.txt          #错误追加剧定向:
[root@localhost ~]# cat 11.txt
-bash: lsaaa: command not found
-bash: ls222: command not found
[root@localhost ~]# ls 2> 11.txt             #此时输入一个正确的命令:
11.txt  2.txt  4.txt  6.txt     cat.txt  pk.txt
1.txt   3.txt  5.txt  ba.txt.c  dir1
[root@localhost ~]# cat 11.txt              #查看时发现无内容,说明命令正确时是不会被重定向的:

也能够同时使用重定向错误重定向

分别写入到两个文件1.txt2.txt

[root@localhost ~]# ls -ld 1.txt  aaa.txt >1.txt 2>2.txt
[root@localhost ~]# cat 1.txt            #正确的重定向在这里:
-rw-r--r-- 1 root root 0 Jul  5 16:18 1.txt
[root@localhost ~]# cat 2.txt            #错误的重定向在这里:
ls: cannot access aaa.txt: No such file or directory

如上:正确的文件写入到1.txt里,错误的则写入到2.txt文件下:

也能够写入到同一个1.txt文件里

[root@localhost ~]# ls -ld 1.txt  aaa.txt   &> 1.txt
[root@localhost ~]# cat 1.txt       #正确和错误都输出到同一个文件里:
ls: cannot access aaa.txt: No such file or directory
-rw-r--r-- 1 root root 0 Jul  5 16:23 1.txt

#等同于下面的命令:
[root@localhost ~]# ls -ld 1.txt  aaa.txt   > 1.txt 2>&1    #&1:表示前面的1.txt文件:
[root@localhost ~]# cat 1.txt
ls: cannot access aaa.txt: No such file or directory
-rw-r--r-- 1 root root 0 Jul  5 16:24 1.txt

#命令中"&1"表示前面的1.txt文件:

输入重定向:"<"输入重定向的左边必须是一个命令:用的较少:

[root@localhost ~]# wc -l < 1.txt
2
相关文章
相关标签/搜索