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

8.1 shell介绍

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

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

• 每一个用户均可以有本身特定的shellshell

• CentOS7默认shell为bash(Bourne Agin Shell)vim

• 还有zsh、ksh等centos

查看当前用户shell:

1.env命令bash

SHELL=/bin/zshspa

2.echo $SHELL.net

➜ ~ echo $SHELLrest

/bin/zshhtm

zsh和ksh安装:

[root@localhost ~]# yum list | grep zsh

zsh.x86_64 5.0.2-25.el7_3.1 updates

zsh-html.x86_64 5.0.2-25.el7_3.1 updates

[root@localhost ~]# yum install -y zsh

[root@localhost ~]# yum list |grep ksh

ksh.x86_64 20120801-26.el7 base

mksh.x86_64 46-5.el7 base

[root@localhost ~]# yum install -y ksh

查看linux系统中支持哪些shell:

[root@localhost ~]# cat /etc/shells

/bin/sh

/bin/bash

/sbin/nologin

/usr/bin/sh

/usr/bin/bash

/usr/sbin/nologin

/bin/zsh

/bin/ksh

/bin/rksh

修改当前用户shell:

1.chsh命令

[root@localhost]~# chsh

Changing shell for root.

New shell [/usr/bin/zsh]: /bin/bash

Shell changed.

2.vim /etc/passwd

root:x:0:0:root:/root:/bin/zsh 修改第七段用户的bash

3.usermod -s /bin/zsh root

[root@localhost]~# usermod -s /bin/zsh root

[root@localhost]~# head -n1 /etc/passwd

root:x:0:0:root:/root:/bin/zsh

8.2 命令历史

• history命令:查看历史使用命令

• .bash_history:命令历史存放目录

• 最大1000条:默认存放1000条

• 变量HISTSIZE:echo $HISTSIZE 产看历史命令记录最大值

HIASTSIZE变量的配置在/etc/profile里

• /etc/profile中修改

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

指定变量的模式,显示命令运行的时间

vim /etc/profile 加到HISTSIZE下面一行

source /etc/profile

1000 2017/06/29 20:09:09 history

1001 2017/06/29 20:13:31 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

1002 2017/06/29 20:13:35 history

• 永久保存 chattr +a ~/.bash_history

• !!

运行上一条命令

• !n

运行第n条命令

• !word

命令历史中最近的word开头的命令

history -c 晴空内存中的历史命令,存命令的.bash_history不会被删除。

打开终端运行的命令,都是暂存在内存中,只有退出终端时才会写入.bash_history文件。

8.3 命令补全和别名

• tab键,敲一下,敲两下

• 参数补全,安装bash-completion

centos 6中只支持补全命令;

centos 7中支持,须要安装bash-completion后重启;

yum install -y bash-completion

init 6

[root@localhost ~]# rpm -qa bash-completion

bash-completion-2.1-6.el7.noarch

[root@localhost ~]# systemctl res

rescue reset-failed restart

[root@localhost ~]# systemctl restart network

network-online.target network.service network.target

[root@localhost ~]# systemctl restart network.service

• alias别名给命令从新起个名字

alias 查看系统中全部的别名文件

• 各用户都有本身配置别名的文件 ~/.bashrc

• ls /etc/profile.d/

• 自定义的alias放到~/.bashrc

8.4 通配符

•* 任意个任意字符

ls *.txt

• ?一个任意字符

ls ?.txt

• [ ] 方括号中的任意一个字符,或的意思

ls [0-9].txt

ls [a-z].txt

ls [123].txt

ls [A-Z].txt

ls [0-9a-zA-Z].txt

• {} 花括号中的任意一个,也是或的意思,中间用,分开

ls {1,2}.txt

ls {a,c,3}.txt

8.5 输入输出重定向

• > 重定向到指定文件,覆盖已存在文件;命令错误时,错误信息不重定向,若目标文件存在,则

目标文件重置为空,目标文件不存在时新建文件。

cat 1.txt >2.txt

• >> 追加剧定向;当命令错误时,错误信息不追加到文件,目标文件不存在时新建目标文件。

cat 1.txt >> 2.txt

• 2> 错误信息重定向到文件;当命令正确时,目标文件存在则重置为空,目标文件不存在则新

建目标文件。

ls aaa.txt 2>err

• 2>> 错误追加剧定向;当命令正确时,目标文件存在则不追加剧定向保持原样,目标文件不存

在则新建目标文件。

ls aaa.txt 2>>err

• 目标文件参数能够写成:>file1 2>file2 正确的输出重定向到file1,错误的输出重定向到file2

ls [1-3].txt 123.fie >file1 2>file2

• &> 无论正确仍是错误都重定向;

• &>> 无论正确仍是错误都追加剧定向;

• < 输入重定向左边的命令执行

wc -l < 1.txt

相关文章
相关标签/搜索