五周第三次课(1月10日)
8.1 shell介绍
8.2 命令历史
8.3 命令补全和别名
8.4 通配符
8.5 输入输出重定向
shell介绍
shell是一个命令解释器,提供用户和机器之间的交互
支持特定语法,比如逻辑判断、循环
每个用户都可以有自己特定的shell
CentOS7默认shell为bash(Bourne Agin Shell)
还有zsh、ksh等
命令历史
- history命令
[[email protected] ~]# history
1 vi /etc/sysconfig/network-scripts/ifcfg-ens33
2 systemctl restart network.service
3 ip addr
4 ping -c 4 www.baidu.com
5 yum groupinstall -y "GNOME Desktop"
6 init5
7 init 5
8 vi /root/.ssh/authorized_keys
9 setenforce 0
10 vi /etc/selinux/config
11 mkdir /root/.ssh
- .bash_history 最大1000条
[[email protected] ~]# echo $HISTSIZE 1000 //运行的命令只能保存1000条
- history -c,内存中的命令清空
[[email protected] ~]# history -c [[email protected] ~]# history 1 history
- cat .bash_history 查看history配置文件,并未被清空
[[email protected] ~]# cat .bash_history
vi /etc/sysconfig/network-scripts/ifcfg-ens33
systemctl restart network.service
ip addr
ping -c 4 www.baidu.com
yum groupinstall -y "GNOME Desktop"
init5
init 5
vi /root/.ssh/authorized_keys
setenforce 0
vi /etc/selinux/config
mkdir /root/.ssh
chmod 700 /root/.ssh
vi /root/.ssh/authorized_keys
ssh -v
yum install -y openssh-client
yum install -y openssh-clients
init 5
init 6
- 变量HISTSIZE
/etc/profile中修改,修改完成用source命令才能完成更新
HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
[[email protected] ~]# echo $HISTSIZE
1000
[[email protected] ~]# source /etc/profile
[[email protected] ~]# echo $HISTSIZE
5000
- 改变环境变量HISTIMEFORMAT的定义,从而改变history的格式
[[email protected] ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " [[email protected] ~]# echo $HISTTIMEFORMAT %Y/%m/%d %H:%M:%S [[email protected] ~]# history 1 2018/01/10 23:18:28 history 2 2018/01/10 23:20:12 cat bash_history 3 2018/01/10 23:20:38 cat .bash_history 4 2018/01/10 23:25:45 ls -l .bash_history 5 2018/01/10 23:25:49 ls 6 2018/01/10 23:26:44 vi /etc/profile 7 2018/01/10 23:32:44 echo $HISTSIZE 8 2018/01/10 23:33:02 source /etc/profile 9 2018/01/10 23:33:05 echo $HISTSIZE
新建一个ssh channel后就不在有用了,为了彻底改变,需要对/etc/profile再次编辑
[[email protected] ~]# vim /etc/profile
[[email protected] ~]# source !$
source /etc/profile
- 永久保存 chattr +a ~/.bash_history
- 非正常关机退出,命令保存的不全
- !! 表示执行上一条命令
- !n 表示执行命令历史中的第几条指令
- !word 表示执行命令历史中最近一次以word开头的命令
命令补全及别名
- tab键,敲一下,补全指令
敲两下,系统会把所有的命令文件名列出来 - 参数补全,安装bash-completion
[[email protected] ~]# yum install -y bash-completion
- 当不清楚命令时,利用tab键来讲命令不全
[[email protected] ~]# systemctl res
rescue reset-failed restart
[[email protected] ~]# systemctl restart network
network-online.target network.service
[[email protected] ~]# systemctl restart network.service
- alias别名给命令重新起个名字
[[email protected] ~]# alias restartnet=‘systemctl restart network.service‘
[[email protected] ~]# restartnet
- 有哪些alias命令
[[email protected] ~]# 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 restartnet=‘systemctl restart network.service‘ alias rm=‘rm -i‘ alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
- 各用户都有自己配置别名的文件 ~/.bashrc
- ls /etc/profile.d/
自定义的alias放到~/.bashrc - unalias取消别名
[[email protected] profile.d]# unalias restartnet
[[email protected] profile.d]# restartnet
bash: restartnet: 未找到命令...
通配符、输入输出重定向
- [ ] ls .txt //用来匹配零个或多个字符
- [ ] ls ?.txt //用?匹配一个字符
- [ ] ls [0-9].txt ; ls [12].txt //范围&或者
- [ ] ls {1,2}.txt //
- [ ] cat 1.txt >2.txt //前命令的结果定向到后面的文档中,原文档删除
- [ ] cat 1.txt >> 2.txt //追加,原文档不删除
- [ ] ls aaa.txt 2>err
[[email protected] profile.d]# lsaa
bash: lsaa: 未找到命令...
[[email protected] profile.d]# lsaa 2>a.txt
[[email protected] profile.d]# cat a.txt
bash: lsaa: 未找到命令...
- [ ] ls aaa.txt 2>>err
- [ ] wc -l < 1.txt
- [ ] command >1.txt 2>&1
原文地址:http://blog.51cto.com/12995218/2059673
时间: 2024-10-08 18:39:30