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

8.1shell介绍

8.2命令历史

8.3命令补全和别名

8.4通配符

8.5输入输出重定向

#Linuxshell基础

#8.1shell介绍
-什么是shell
1.shell是一个命令解释器,提供用户和机器之间的交互
2.支持特定语法,比如逻辑判断、循环
3.每个用户都可以有自己特定的shell
4.CentOS7认shell为bash(BourneAginShell)
5.还有zsh、ksh等
```
[root@aminglinux-01~]#
[root@aminglinux-01~]#yumlist|grepzsh
autojump-zsh.noarch22.3.0-3.el7epel
zsh.x86_645.0.2-25.el7_3.1updates
zsh-html.x86_645.0.2-25.el7_3.1updates
zsh-lovers.noarch0.9.0-1.el7epel
[root@aminglinux-01~]#yumlist|grepksh
ksh.x86_6420120801-26.el7base
mksh.x86_6446-5.el7base
python-XStatic-Rickshaw.noarch1.5.0.0-4.el7epel
python-moksha-common.noarch1.2.3-2.el7epel
python-moksha-hub.noarch1.4.8-1.el7epel
python-moksha-wsgi.noarch1.2.2-2.el7epel
[root@aminglinux-01~]#
```


#8.2命令历史

![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170814/225032244.png?imageslim)

-很多系统里面的命令都是存在用户的家目录下,/root/.bash_history
```
[root@aminglinux-01~]#ls/root/.bash_history
/root/.bash_history
[root@aminglinux-01~]#cat/root/.bash_history

echo$?
yumlist|grep-iapr
yumlist|grep-ipcre
yuminstall-ypcre.x86_64
yuminstall-ypcre-devel.x86_64
./configure--prefix=/usr/local/apache2
echo$?
make
echo$?
makeinstall
echo$?
ls/usr/local/apache2
init0
[root@aminglinux-01~]#
[root@aminglinux-01~]#echo$HISTSIZE
1000
[root@aminglinux-01~]#
```
-这是我们之前存的命令,这个文件里最大可以存1000条

-histroy-c仅仅是把内存当中的命令给清空了,并不会去删除配置文件
```
[root@aminglinux-01~]#history-c
[root@aminglinux-01~]#history
8history

[root@aminglinux-01~]#cat.bash_history

[root@aminglinux-01~]#ls-l.bash_history
-rw-------.1rootroot158108月1223:03.bash_history
[root@aminglinux-01~]#


```

-变量HISTSIZE可以定义的,在/etc/profile
```
[root@aminglinux-01~]#vi/etc/profile


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

exportPATHUSERLOGNAMEMAILHOSTNAMEHISTSIZEHISTCONTROL

#Bydefault,wewantumasktogetset.Thissetsitforloginshell
#Currentthresholdforsystemreserveduid/gidsis200
#YouCouldcheckuidgidreservationvalidityin
```
-可以把HISTSIZE=1000改为5000保存
```
HOSTNAME=`/usr/bin/hostname2>/dev/null`
HISTSIZE=5000
if["$HISTCONTROL"="ignorespace"];then
exportHISTCONTROL=ignoreboth
else
exportHISTCONTROL=ignoredups
fi

exportPATHUSERLOGNAMEMAILHOSTNAMEHISTSIZEHISTCONTROL

#Bydefault,wewantumasktogetset.Thissetsitforloginshell
#Currentthresholdforsystemreserveduid/gidsis200
#YouCouldcheckuidgidreservationvalidityin
--INSERT--
```
-虽然改了,但是并没有生效
```
[root@aminglinux-01~]#vi/etc/profile
[root@aminglinux-01~]#echo$HISTSIZE
1000
[root@aminglinux-01~]#
```

-使用命令source/etc/profile或者重新进入终端,才会生效
```
[root@aminglinux-01~]#source/etc/profile
[root@aminglinux-01~]#echo$HISTSIZE
5000
[root@aminglinux-01~]#

```
-会记录日期时间,这个效果是由环境变量改变的,但是只是在当前终端下生效,
```
[root@aminglinux-01~]#history
8history
9cat.bash_history
10ls-l.bash_history
11vi/etc/profile
12echo$HISTSIZE
13source/etc/profile
14echo$HISTSIZE
15history
[root@aminglinux-01~]#

改变命令历史的格式
[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
82017/08/1423:07:03history
92017/08/1423:09:09cat.bash_history
102017/08/1423:10:08ls-l.bash_history
112017/08/1423:28:55vi/etc/profile
122017/08/1423:33:10echo$HISTSIZE
132017/08/1423:34:10source/etc/profile
142017/08/1423:34:13echo$HISTSIZE
152017/08/1423:35:40history
162017/08/1423:39:00HISTTIMEFORMAT="%Y/%m/%d%H:%M:%s"
172017/08/1423:39:15echo$HISTTIMEFORMAT
182017/08/1423:39:28history
[root@aminglinux-01~]#
```

-改变命令历史的格式,这个只针对当前终端下生效,如果想要所有的都生效,需要编辑vi/etc/profile文件把HISTTIMEFORMAT="%Y/%m/%d%H:%M:%s"加入到文件里面vi/etc/profile

```
HOSTNAME=`/usr/bin/hostname2>/dev/null`
HISTSIZE=5000
HISTTIMEFORMAT="%Y/%m/%d%H:%M:%s"加入这一行

if["$HISTCONTROL"="ignorespace"];then
exportHISTCONTROL=ignoreboth
else
exportHISTCONTROL=ignoredups


[root@aminglinux-01~]#vi/etc/profile
[root@aminglinux-01~]#source!$
source/etc/profile
[root@aminglinux-01~]#
```
-这时候再打开另一个终端就可以看到生效了
![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170814/234805048.png?imageslim)

```
[root@aminglinux-01~]#echo$HISTTIMEFORMAT
%Y/%m/%d%H:%M:%s
[root@aminglinux-01~]#
9872017/08/1423:46:55echo$?
9882017/08/1423:46:55yumlist|grep-iapr
9892017/08/1423:46:55yumlist|grep-ipcre
9902017/08/1423:46:55yuminstall-ypcre.x86_64
9912017/08/1423:46:55yuminstall-ypcre-devel.x86_64
9922017/08/1423:46:55./configure--prefix=/usr/local/apache2
9932017/08/1423:46:55echo$?
9942017/08/1423:46:55make
9952017/08/1423:46:55echo$?
9962017/08/1423:46:55makeinstall
9972017/08/1423:46:55echo$?
9982017/08/1423:46:55ls/usr/local/apache2
9992017/08/1423:46:55init0
10002017/08/1423:46:57clear
10012017/08/1423:47:07echo$HISTTIMEFORMAT
10022017/08/1423:48:13history
[root@aminglinux-01~]#

```
-改变命令历史的格式就成功了

-永久保存命令历史chattr+a~/.bash_history
```
[root@aminglinux-01~]#chattr+a~/.bash_history
```
-这样运行过的命令都会记录下来,
-但是如果不正常退出,有时候敲了一些命令,但是你没有logout,exit退出,而是直接关闭终端,那样就会记录不全,命令就保存的不全。

-命令!!2个!其实就是你运行的上一条命令(最后一条命令)
```
[root@aminglinux-01~]#ls
1111_heard.txt1.txt~2343.txtanaconda-ks.cfg.1
1231_sorft.txt1.txt.bak2.txt.bak4.txtbiji.txt
[root@aminglinux-01~]#!!
ls
1111_heard.txt1.txt~2343.txtanaconda-ks.cfg.1
1231_sorft.txt1.txt.bak2.txt.bak4.txtbiji.txt
[root@aminglinux-01~]#
```
-!n表示运行history里面的第n条命令
```
[root@aminglinux-01~]#history
82017/08/1423:07:03history
92017/08/1423:09:09cat.bash_history
102017/08/1423:10:08ls-l.bash_history
112017/08/1423:28:55vi/etc/profile
122017/08/1423:33:10echo$HISTSIZE
132017/08/1423:34:10source/etc/profile
142017/08/1423:34:13echo$HISTSIZE
152017/08/1423:35:40history
162017/08/1423:39:00HISTTIMEFORMAT="%Y/%m/%d%H:%M:%s"
172017/08/1423:39:15echo$HISTTIMEFORMAT
182017/08/1423:39:28history
192017/08/1423:43:15vi/etc/profile
202017/08/1423:45:50source/etc/profile
212017/08/1423:51:50chattr+a~/.bash_history
222017/08/1423:54:18history
232017/08/1423:55:50ls
242017/08/1423:56:13history
[root@aminglinux-01~]#!10
ls-l.bash_history
-rw-------.1rootroot158818月1423:51.bash_history
[root@aminglinux-01~]#
```
-!word它会在命令历史里面倒着往上找第一个有word的命令
-比如运行命令!echo就是运行命令历史里倒着数第一个有ehco相关的命令
```
[root@aminglinux-01~]#history
82017/08/1423:07:03history
92017/08/1423:09:09cat.bash_history
102017/08/1423:10:08ls-l.bash_history
112017/08/1423:28:55vi/etc/profile
122017/08/1423:33:10echo$HISTSIZE
132017/08/1423:34:10source/etc/profile
142017/08/1423:34:13echo$HISTSIZE
152017/08/1423:35:40history
162017/08/1423:39:00HISTTIMEFORMAT="%Y/%m/%d%H:%M:%s"
172017/08/1423:39:15echo$HISTTIMEFORMAT
182017/08/1423:39:28history
192017/08/1423:43:15vi/etc/profile
202017/08/1423:45:50source/etc/profile
212017/08/1423:51:50chattr+a~/.bash_history
222017/08/1423:54:18history
232017/08/1423:55:50ls
242017/08/1423:56:13history
[root@aminglinux-01~]#!10
ls-l.bash_history
-rw-------.1rootroot158818月1423:51.bash_history
[root@aminglinux-01~]#!echo
echo$HISTTIMEFORMAT
%Y/%m/%d%H:%M:%s
[root@aminglinux-01~]#
```
-命令历史里关于ehco的命令是echo$HISTTIMEFORMAT%Y/%m/%d%H:%M:%s,所以!echo就是运行这个命令echo$HISTTIMEFORMAT%Y/%m/%d%H:%M:%s



#8.3命令补全和别名
![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170815/220451944.png?imageslim)

-按tab一下补全命令,tab按俩下列出以命令开头的命令
```
[root@aminglinux-01~]#ls
lslsblklsinitrdlslockslsmodlspci
lsattrlscpulsipclsloginslsnslsscsi
[root@aminglinux-01~]#mk
mkdictmkfifomkfs.ext2mkfs.xfsmknod
mkdirmkfsmkfs.ext3mkhomedir_helpermkpasswd
mkdumprdmkfs.btrfsmkfs.ext4mkinitrdmkswap
mke2fsmkfs.cramfsmkfs.minixmklost+foundmktemp
[root@aminglinux-01~]#mktemp
```
-运行mktab俩下就会显示一堆以mk开头的命令,按mkttab一下就会自动补全mktemp
-centos7里面支持参数补全
1.需要安装一个包
```
[root@aminglinux-01~]#systemctlrestartnetwork^C
[root@aminglinux-01~]#yuminstall-ybash-completion

已安装:
bash-completion.noarch1:2.1-6.el7
完毕!
[root@aminglinux-01~]#
```
2.安装完需要重启下系统才可以reboot或者init6
3.重启系统之后,先看下那个包是否安装,然后输入部分命令尝试按下tab看看是否会补全
```
[root@aminglinux-01~]#rpm-qabash-completion
bash-completion-2.1-6.el7.noarch
[root@aminglinux-01~]#systemctlres
rescuereset-Failedrestart
[root@aminglinux-01~]#systemctlres
rescuereset-Failedrestart
[root@aminglinux-01~]#systemctlres
rescuereset-Failedrestart
[root@aminglinux-01~]#systemctlrestartnetwork
network-online.targetnetwork.service
[root@aminglinux-01~]#systemctlrestartnetwork.service
[root@aminglinux-01~]#
```
-alias别名给名重新起个名字用alias把systemctlrestartnetwork.service改为restartnet
```
[root@aminglinux-01~]#systemctlrestartnetwork.service
[root@aminglinux-01~]#aliasrestartnet='systemctlrestartnetwork.service'
[root@aminglinux-01~]#restartnet

把系统里所有的alias都列出来
[root@aminglinux-01~]#alias
aliascp='cp-i'
aliasegrep='egrep--color=auto'
aliasfgrep='fgrep--color=auto'
aliasgrep='grep--color=auto'
aliasl.='ls-d.*--color=auto'
aliasll='ls-l--color=auto'
aliasls='ls--color=auto'
aliasmv='mv-i'
aliasrestartnet='systemctlrestartnetwork.service'
aliasrm='rm-i'
aliaswhich='alias|/usr/bin/which--tty-only--read-alias--show-dot--show-tilde'
[root@aminglinux-01~]#
```
-取消自定义别名
```
[root@aminglinux-01profile.d]#unaliasrestartnet
[root@aminglinux-01profile.d]#restartnet
-bash:restartnet:未找到命令
[root@aminglinux-01profile.d]#
```

-这些alias存在哪里呢.bashrc,/etc/profile.d
```
[root@aminglinux-01~]#vi.bashrc

#.bashrc

#Userspecificaliasesandfunctions

aliasrm='rm-i'
aliascp='cp-i'
aliasmv='mv-i'

#SourceglobaldeFinitions
if[-f/etc/bashrc];then
./etc/bashrc
fi
~
~
```
2.这个/etc/profile.d文件下面的colorls.sh,colorgrep.sh也有
```
[root@aminglinux-01~]#cd/etc/profile.d
[root@aminglinux-01profile.d]#ls
256term.cshcolorgrep.cshcolorls.shless.cshvim.sh
256term.shcolorgrep.shlang.cshless.shwhich2.csh
bash_completion.shcolorls.cshlang.shvim.cshwhich2.sh
[root@aminglinux-01profile.d]#vicolorls.sh
~
".bashrc"12L,176C



aliasll='ls-l'2>/dev/null
aliasl.='ls-d.*'2>/dev/null

INCLUDE=
COLORS=

forcolorsin"$HOME/.dir_colors.$TERM""$HOME/.dircolors.$TERM"\
"$HOME/.dir_colors""$HOME/.dircolors";do
[-e"$colors"]&&COLORS="$colors"&&\
INCLUDE="`/usr/bin/cat"$COLORS"|/usr/bin/grep'^INCLUDE'|/usr/bin/cut-d''-f2-`"&&\
break
done

/alias
```
3.colorgrep.sh文件也有
```
#color-grepinitialization

/usr/libexec/grepconf.sh-c||return

aliasgrep='grep--color=auto'2>/dev/null
aliasegrep='egrep--color=auto'2>/dev/null
aliasfgrep='fgrep--color=auto'2>/dev/null
~

"colorgrep.sh"7L,201C
```


#8.4通配符
![mark](http://oqxf7c508.bkt.clouddn.com/blog/20170815/223720549.png?imageslim)

-*.txt*表示任何字符
```
[root@aminglinux-01~]#ls
1111_heard.txt1.txt~2343.txtanaconda-ks.cfg.1
1231_sorft.txt1.txt.bak2.txt.bak4.txtbiji.txt
[root@aminglinux-01~]#ls*.txt
1_heard.txt1_sorft.txt3.txt4.txtbiji.txt
[root@aminglinux-01~]#ls*txt
1_heard.txt1_sorft.txt3.txt4.txtbiji.txt
[root@aminglinux-01~]#ls*txt*
1_heard.txt1_sorft.txt1.txt~1.txt.bak2.txt.bak3.txt4.txtbiji.txt
[root@aminglinux-01~]#ls1*
1_heard.txt1_sorft.txt1.txt~1.txt.bak

111:
12.tx~12.txt12_txt.swp2224913aming3

123:
aminglinux.logyum.log
[root@aminglinux-01~]#
```
-ls?.txt?表示一个字符任意的字符
```
[root@aminglinux-01~]#touch2.txt
[root@aminglinux-01~]#touch1.txt
[root@aminglinux-01~]#ls?.txt
1.txt2.txt3.txt4.txt
[root@aminglinux-01~]#toucha.txt
[root@aminglinux-01~]#touchbb.txt
[root@aminglinux-01~]#ls?.txt
1.txt2.txt3.txt4.txta.txt
[root@aminglinux-01~]#
```
-ls[0-9].txt[]13.txt或者的意思
```
[root@aminglinux-01~]#ls[0-3].txt
1.txt2.txt3.txt
[root@aminglinux-01~]#ls[123].txt
1.txt2.txt3.txt
[root@aminglinux-01~]#ls[23].txt
2.txt3.txt
[root@aminglinux-01~]#ls[13].txt
1.txt3.txt
[root@aminglinux-01~]#ls[0-9].txt
1.txt2.txt3.txt4.txt
[root@aminglinux-01~]#ls[0-9a-zA-Z].txt
1.txt2.txt3.txt4.txta.txt
[root@aminglinux-01~]#
```
-ls{1,2}.txt
```
[root@aminglinux-01~]#ls{1,2}.txt
1.txt2.txt
[root@aminglinux-01~]#ls{1,2,3,a}.txt
1.txt2.txt3.txta.txt
[root@aminglinux-01~]#
```

##8.5输入输出重定向

-[]>正确的输出重定向cat1.txt>2.txt
-[]>>正确的追加cat1.txt>>2.txt
-2>错误输出重定向lsaaa.txt2>err
-2>>错误的追加lsaaa.txt2>>err

```
[root@aminglinux-01~]#lsaaa
-bash:lsaaa:未找到命令
[root@aminglinux-01~]#lsaaa2>a.txt
[root@aminglinux-01~]#cata.txt
-bash:lsaaa:未找到命令
[root@aminglinux-01~]#lsaaa2>>a.txt
[root@aminglinux-01~]#cata.txt
-bash:lsaaa:未找到命令
-bash:lsaaa:未找到命令
[root@aminglinux-01~]#>>>2>2>>>+2>==&>^C
[root@aminglinux-01~]#
```
-&>可以把正确和错误的放一起
```
[root@aminglinux-01~]#ls[12].txtaaa.txt&>a.txt
[root@aminglinux-01~]#cata.txt
ls:无法访问aaa.txt:没有那个文件或目录
1.txt
2.txt
[root@aminglinux-01~]#
```
-&>同样也支持追加
```
[root@aminglinux-01~]#ls[12].txtaaa.txt&>>a.txt
[root@aminglinux-01~]#cata.txt
ls:无法访问aaa.txt:没有那个文件或目录
1.txt
2.txt
ls:无法访问aaa.txt:没有那个文件或目录
1.txt
2.txt
[root@aminglinux-01~]#
```
-&>既可以放正确也可以发个错误输出信息保存到指定的文件里
```
[root@aminglinux-01~]#ls[12].txtaaa.txt>1.txt2>a.txt
[root@aminglinux-01~]#cat1.txt
1.txt
2.txt
[root@aminglinux-01~]#cata.txt
ls:无法访问aaa.txt:没有那个文件或目录
[root@aminglinux-01~]#
```
-输入重定向把右边的一个文件文件内容给它左边输入到一个命令里面去
```
[root@aminglinux-01~]#wc-l<1.txt
2
[root@aminglinux-01~]#2.txt<1.txt
-bash:2.txt:未找到命令
[root@aminglinux-01~]#
```
-[]左边必须是一个命令,不可以是文件输入重定向用的比较少,做一个了解

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...