shell基础,命令历史,命令补全,别名alias,通配符,输入输出重定向

shell基础

shell
是一个命令解释器,提供用户和机器之间的交互,支持特定语法,比如逻辑判断,循环等。
每个用户都可以有自己特性的shell,centos7默认shell为bash(bourne agin shell),还有zsh,ksh等,每种shell支持的特性不尽相同。

命令历史

history命令 查看命令历史。
~/.bash_history 用户命令历史记录文件
在正常退出终端,命令历史可保存在此文件,非正常退出终端,保存不全

/etc/profile history命令的配置文件

echo $HISTSIZE 查看命令历史最大记录值,默认HISTSIZE=1000
用history查看命令历史, 结果可能会大于设定的最大值,大于最大值的命令历史暂存在 内存中,未写入 .bash_history文件。
echo $HISTTIMEFORMAT 查看时间变量, 默认无此变量,需要添加

vim /etc/profile 进入配置文件
修改:
HISTSIZE=5000 修改变量HISTSIZE,设定最大值为5000
修改:
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " 定义时间变量,记录每条命令的操作时间

source /etc/profile 加载配置文件,修改profile后需要加载使修改生效。

history -c 清空内存中的命令历史
不会删除 .bash_history文件中的命令历史。

chattr +a ~/.bash_history 永久保存命令历史,只许追加不许删不受HISTSIZE值限制

快捷用法:
!! 最后一条命令
!n n= 命令历史号,指定使用历史号为n的命令
!word 使用由后向前查找最后一条以word开头的命令

例:

[root@localhost ~]# history 查看命令历史
~
~
省略
999 cd /usr/local/src
1000 ls 可看到历史命令记录号大于默认的 1000 ,这些 记录暂存于内存未写入 .bash_history文件
~
~
省略
1011 history
1012 ls /home/
1013 ls /home/ELON/.bash_history
1014 cat /home/ELON/.bash_history
1015 echo $HISTSIZE 查看变量HISTSIZE最大值,此变量规定了最大记录号
1016 cat /root/.bash_history 查看root用户的命令历史记录文件
1017 history
[root@localhost ~] # !cat 使用最后一条已cat开头的历史命令= cat /root/.bash_history
cat /root/.bash_history
ls /home
init 0
mount /dev/cdrom /mnt
cd /mnt/Packages/
yum -qa |grep pcre
yum list |grep pcre
yum install -y pcre-devel.x86_64
[root@localhost ~]#
[root@localhost ~]# !1015 指定使用记录号为1015的历史命令= echo $HISTSIZE
echo $HISTSIZE
1000 记录号为1000,默认值
[root@localhost ~]#
[root@localhost ~]# echo $HISTTIMEFORMAT 查看变量HISTTIMEFORMAT的值,默认无此变量
所以此处变量值为空
[root@localhost ~]# vim /etc/profile 配置history命令的配置文件
找到: HISTSIZE= 1000,将其改为: HISTSIZE=5000,并在下一行插入: HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "

[root@localhost ~]# echo $HISTSIZE 再次查看HISTSIZE变量并未生效
1000
[root@localhost ~]# echo $HISTTIMEFORMAT 再次查看HISTTIMEFORMAT变量也未生效
[root@localhost ~]# source /etc/profile 加载配置文件,使修改和新增的变量生效
[root@localhost ~]# echo $HISTSIZE 查看HISTSIZE变量已生效
5000
[root@localhost ~]# echo $HISTTIMEFORMAT 查看HISTTIMEFORMAT变量已生效
%Y/%m/%d %H:%M:%S
[root@localhost ~]#
[root@localhost ~]# history 再次查看命令历史,已经显示历史命令的操作时间
~
~
省略
1013 2018/04/21 01:51:37 ls /home/ELON/.bash_history
1014 2018/04/21 01:51:47 cat /home/ELON/.bash_history
1015 2018/04/21 01:53:38 echo $HISTSIZE
1016 2018/04/21 01:54:33 cat /root/.bash_history
1017 2018/04/21 03:18:22 history
1018 2018/04/21 03:25:03 cat /root/.bash_history
1019 2018/04/21 03:34:07 echo $HISTSIZE
1020 2018/04/21 03:49:09 echo $HISTTIMEFORMAT
1021 2018/04/21 03:53:09 vim /etc/profile
1022 2018/04/21 04:03:23 echo $HISTSIZE
1023 2018/04/21 04:04:01 echo $HISTTIMEFORMAT
1024 2018/04/21 04:07:03 source /etc/profile
1025 2018/04/21 04:07:15 echo $HISTSIZE
1026 2018/04/21 04:07:23 echo $HISTTIMEFORMAT
1027 2018/04/21 04:10:18 history
[root@localhost ~]# history -c 清空内存中的命令历史
[root@localhost ~]# history 查看命令历史,只剩此条命令的记录且有显示此命令的操作时间
25 2018/04/21 04:14:02 history
[root@localhost ~]# !! 使用最后一次命令
history
25 2018/04/21 04:14:02 history
[root@localhost ~]#


命令补全及别名

tab键
输入命令或参数时按一次补全,按两次查看以前面内容为前缀的命令或参数,也可用于文件或目录
centos7,支持参数补全,centos6只支持命令补全

参数补全,需安装 bash-completion 默认未安装,安装完需要重启系统才生效
~/.bashrc 用户自定义的alias存于用户家目录下此文件
/etc/profile.d/ 系统自带的alias在此目录下
unalias aliasname 取消alias

例:
[root@localhost ~]# yum install -y bash-completion 安装参数补全工具包
已加载插件:fastestmirror
base | 3.6 kB 00:00:00
epel/x86_64/metalink | 8.1 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/2): epel/x86_64/updateinfo | 916 kB 00:00:00
(2/2): epel/x86_64/primary_db | 6.3 MB 00:00:01
Loading mirror speeds from cached hostfile
* epel: mirrors.ustc.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 bash-completion.noarch.1.2.1-6.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
===================================================================================================================
Package 架构 版本 源 大小
===================================================================================================================
正在安装:
bash-completion noarch 1:2.1-6.el7 base 85 k
事务概要
===================================================================================================================
安装 1 软件包
总下载量:85 k
安装大小:259 k
Downloading packages:
bash-completion-2.1-6.el7.noarch.rpm | 85 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : 1:bash-completion-2.1-6.el7.noarch 1/1
验证中 : 1:bash-completion-2.1-6.el7.noarch 1/1
已安装:
bash-completion.noarch 1:2.1-6.el7
完毕!
安装完,重启服务器
[root@localhost ~]# alias restnet='systemctl restart network.service' 给此命令做restnet的别名
[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 restnet='systemctl restart network.service' 这是我们定义的别名为restnet的命令
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]#
[root@localhost ~]# cat /root/.bashrc 查看root用户自定义的别名
# .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 bash_completion.sh colorgrep.sh colorls.sh lang.csh less.csh PackageKit.sh vim.sh which2.csh
256term.sh colorgrep.csh colorls.csh flatpak.sh lang.sh less.sh vim.csh vte.sh which2.sh
[root@localhost ~]# unalias restnet 取消别名为restnet的命令
[root@localhost ~]# alias 再次查看,已无别名为restnet的命令
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'
[root@localhost ~]#


通配符
* 表示不分字符,不分数量
? 表示一个任意的字符
[ ] 表示范围中的每一个
{ ,} 表示指定的每一个

例:
[root@localhost ~]# ls
1.txt 2.txt 3.txt.new anaconda-ks.cfg A.txt C.txt initial-setup-ks.cfg
1.txt.new 2.txt.new 456 a.txt c.txt home
[root@localhost ~]# ls *.txt 列出后缀为.txt的所有文件
1.txt 2.txt a.txt A.txt c.txt C.txt
[root@localhost ~]# ls *txt* 列出文件名或后缀中包含txt的所有文件
1.txt 1.txt.new 2.txt 2.txt.new 3.txt.new a.txt A.txt c.txt C.txt
[root@localhost ~]# ls 1* 列出以1开头的所有文件
1.txt 1.txt.new
[root@localhost ~]# ls ?.txt 列出文件名为单个字符的所有文件
1.txt 2.txt a.txt A.txt c.txt C.txt
[root@localhost ~]# ls [0-9].txt 列出处于0至9范围中的所有文件
1.txt 2.txt
[root@localhost ~]# ls [a-zA-Z0-9].txt 列出处于0至9,或A至Z,或a至z范围的所有文件
1.txt 2.txt a.txt A.txt c.txt C.txt
[root@localhost ~]# ls [12].txt 分别列出1.txt和 2.txt
1.txt 2.txt
[root@localhost ~]# ls {1,2,c,A}.txt 分别列出指定的每一个文件
1.txt 2.txt A.txt c.txt
[root@localhost ~]#

输入输出重定向

> 正确输出 >> 正确追加 2> 错误输出 2>> 错误追加 < 输入重定向

>+2>== &> 正确输出,错误输出 >>+2>>==&>> 正确追加,错误追加

command [file1] >file2 用>前面命令输出的内容覆盖后面的文件内容
command [file1] >>file2 将>>前面命令输出的内容追加到后面的文件里
command [file1] > file2 2>file3 正确输出写入file2 错误输出写入file3
command < file 将<右边文件的内容输入到<左边命令里 , 只能用于命令

例:
[root@localhost ~]# echo "abdc " >> a.txt 追加abcd进a.txt文件
[root@localhost ~]# echo " 1234 " >> 1.txt 追加1234进1.txt文件
[root@localhost ~]# cat 1.txt >a.txt 用cat命令输出的内容覆盖后面的a.txt文件
[root@localhost ~]# cat a .txt 查看a.txt文件内容
1234 内容已被覆盖
[root@localhost ~]# cat 1.txt >>a.txt 将前面命令输出的内容追加进a.txt文件
[root@localhost ~]# cat a.txt
1234
1234
[root@localhost ~]# ls
1.txt 2.txt 3.txt 456 anaconda-ks.cfg a.txt b.txt c.txt d.txt home initial-setup-ks.cfg
[root@localhost ~]# ls 1.txt A.txt >b.txt 2>c.txt
用前面命令的正确输出替换b.txt文件内容,用前面命令的错误输出替换c.txt文件内容
[root@localhost ~]# ls 1.txt A.txt &>d.txt 用前面命令的正确输出和错误输出替换d.txt文件内容
[root@localhost ~]# cat b.txt
1.txt
[root@localhost ~]# cat c.txt
ls: 无法访问A.txt: 没有那个文件或目录
[root@localhost ~]#
[root@localhost ~]# cat d.txt
ls: 无法访问A.txt: 没有那个文件或目录
1.txt
[root@localhost ~]# ls 1.txt A.txt >>b.txt 2>>c.txt
将前面命令的正确输出追加进b.txt文件,将前面命令的错误输出追加进c.txt文件
[root@localhost ~]# ls 1.txt A.txt &>>d.txt 将前面命令的正确输出和错误输出追加进d.txt文件
[root@localhost ~]# cat b.txt
1.txt
1.txt
[root@localhost ~]# cat c.txt
ls: 无法访问A.txt: 没有那个文件或目录
ls: 无法访问A.txt: 没有那个文件或目录
[root@localhost ~]# cat d.txt
ls: 无法访问A.txt: 没有那个文件或目录
1.txt
ls: 无法访问A.txt: 没有那个文件或目录
1.txt
[root@localhost ~]# wc -l < d.txt 统计右边文件输入的内容行数
4
[root@localhost ~]#



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值