20.Shell介紹,history,Tab鍵,通配符,重定向
阿新 • • 發佈:2018-01-11
echo slim 系統 重新 上一條 變量 分享圖片 三次 ali 五周第三次課(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命令
[root@localhost ~]# 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條
[root@localhost ~]# echo $HISTSIZE 1000 //運行的命令只能保存1000條
- history -c,內存中的命令清空
[root@localhost ~]# history -c [root@localhost ~]# history 1 history
- cat .bash_history 查看history配置文件,並未被清空
[root@localhost ~]# 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 "
[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]# source /etc/profile
[root@localhost ~]# echo $HISTSIZE
5000
- 改變環境變量HISTIMEFORMAT的定義,從而改變history的格式
[root@localhost ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S " [root@localhost ~]# echo $HISTTIMEFORMAT %Y/%m/%d %H:%M:%S [root@localhost ~]# 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再次編輯
[root@localhost ~]# vim /etc/profile
[root@localhost ~]# source !$
source /etc/profile
- 永久保存 chattr +a ~/.bash_history
-
非正常關機退出,命令保存的不全
- !! 表示執行上一條命令
- !n 表示執行命令歷史中的第幾條指令
-
!word 表示執行命令歷史中最近一次以word開頭的命令
命令補全及別名
-
tab鍵,敲一下,補全指令
敲兩下,系統會把所有的命令文件名列出來 - 參數補全,安裝bash-completion
[root@localhost ~]# yum install -y bash-completion
- 當不清楚命令時,利用tab鍵來講命令不全
[root@localhost ~]# systemctl res
rescue reset-failed restart
[root@localhost ~]# systemctl restart network
network-online.target network.service
[root@localhost ~]# systemctl restart network.service
- alias別名給命令重新起個名字
[root@localhost ~]# alias restartnet=‘systemctl restart network.service‘
[root@localhost ~]# restartnet
-
有哪些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 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取消別名
[root@localhost profile.d]# unalias restartnet
[root@localhost 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
[root@localhost profile.d]# lsaa
bash: lsaa: 未找到命令...
[root@localhost profile.d]# lsaa 2>a.txt
[root@localhost profile.d]# cat a.txt
bash: lsaa: 未找到命令...
- [ ] ls aaa.txt 2>>err
- [ ] wc -l < 1.txt
- [ ] command >1.txt 2>&1
20.Shell介紹,history,Tab鍵,通配符,重定向