10月11日任務shell
8.1 shell介紹apache
8.2 命令歷史vim
8.3 命令補全和別名centos
8.4 通配符bash
8.5 輸入輸出重定向less
shell--命令解釋器,提供用戶和機器的交互,輸入命令,輸出結果。測試
不少Linux發行版(包括centos)的默認shell是bash(Bourne Again Shell);此外用戶還能夠自定義設置本身的shell(usermod -s SHELL USER),CentOS7默認支持的shell以下:centos7
# 當前系統使用的shell [root@localhost ~]# echo $SHELL /bin/bash # 系統默認支持的shell [root@localhost ~]# cat /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin
還能夠經過yum安裝zsh、ksh等其餘的shell。spa
shell支持特定語法,如邏輯判斷,循環等插件
用戶執行過的命令都會記錄到其家目錄下的 .bash_history文件中!
歷史命令記錄默認最大1000條,可是能夠經過修改環境變量HISTSIZE(/etc/profile配置文件中修改)來調整歷史記錄的條數。
[root@localhost ~]# echo $HISTSIZE 1000
注意,並非修改完/etc/profile文件,HISTSIZE就會當即生效,要想當即生效,執行source /etc/profile命令。
當前用戶登陸後執行的命令,會暫時存儲在內存中,只有在用戶退出終端纔會被寫入.bash_history文件中!!
自定義一個環境變量,使歷史命令儲存時保存時間。
暫時改變格式,只在當前終端下生效 [root@centos7 ~]# HISTTIMEFORMAT=「%Y/%m/%d %H:%M:%S」 永久保存格式 [root@centos7 ~]# vim /etc/profile#在HISTSIZE變量後追加一行 HISTTIMEFORMAT=「%Y/%m/%d %H:%M:%S」 :wq 保存退出 #馬上生效修改 [root@centos7 ~]# source /etc/profile [root@centos7 ~]# history ..... 517 ... 20:29:49halt 518 ... 20:29:58vim /etc/profile 519 ... 20:31:04source /etc/profile 520 ... 20:31:12history
HOSTTIMEFORMAT環境變量在用戶命令行下設置,變量只是本地變量,只在當前終端有效!
# 經過設置a隱藏權限,使文件沒法刪除以前多餘的命令,只能追加 [root@centos7 ~]# chattr +a ~/.bash_history
使用終端執行命令時,若是直接關閉終端,會致使**.bash_history文件沒法正常記錄完整執行記錄。所以在退出終端時,應該使用exit或logout**命令正常退出!
[root@localhost ~]# ch chacl chattr chfn chmod chronyc chrt chage chcon chgrp chown chronyd chsh chat chcpu chkconfig chpasswd chroot chvt
CentOS7支持參數補全(需安裝插件),CentOS6不支持。
安裝完畢需重啓系統!!!
[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 rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]# alias lt='ls -lt' [root@localhost ~]# lt ./ 總用量 68 -rw-r--r--. 1 root root 146 11月 4 09:12 file.py -rw-r--r--. 1 root root 586 10月 27 21:16 class.py drwxr-xr-x. 3 root root 58 10月 27 19:49 111 lrwxrwxrwx. 1 root root 11 10月 27 19:47 test -> /etc/passwd -rw-r--r--. 1 root root 225 10月 26 22:58 try.py -rw-r--r--. 1 root root 93 10月 23 18:38 iptables.log -rw-------. 1 root root 1422 10月 18 02:47 anaconda-ks.cfg
[root@localhost ~]# cat ~/.bashrc # .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' ...
[root@localhost ~]# ls -l /etc/profile.d/*.sh -rw-r--r--. 1 root root 841 11月 6 2016 /etc/profile.d/256term.sh -rw-r--r--. 1 root root 201 4月 29 2015 /etc/profile.d/colorgrep.sh -rw-r--r--. 1 root root 1606 11月 5 2016 /etc/profile.d/colorls.sh -rw-r--r--. 1 root root 2703 11月 6 2016 /etc/profile.d/lang.sh -rw-r--r--. 1 root root 121 7月 31 2015 /etc/profile.d/less.sh -rw-r--r--. 1 root root 269 8月 2 08:45 /etc/profile.d/vim.sh -rw-r--r--. 1 root root 169 1月 28 2014 /etc/profile.d/which2.sh
[root@localhost ~]# unalias lt [root@localhost ~]# lt ./ -bash: lt: 未找到命令
{} 以逗號鏈接的選項之一
#同時建立多個文件:{} [root@centos7 test]# touch {1,3}.txt [root@centos7 test]# ls -l 總用量 0 -rw-r--r--. 1 root root 0 ... 21:02 1.txt -rw-r--r--. 1 root root 0 ... 21:02 3.txt
[] 範圍內任選其一
# 測試 [root@centos7 test]# ls -l 總用量 0 -rw-r--r--. 1 root root 0 ... 21:02 1.txt -rw-r--r--. 1 root root 0 ... 21:02 3.txt -rw-r--r--. 1 root root 0 ... 21:04 aa.txt
[root@centos7 test]# ls *.txt 1.txt 3.txt aa.txt
[root@centos7 test]# ls ?.txt 1.txt 3.txt
[root@centos7 test]# ls [0-9].txt 1.txt 3.txt [root@centos7 test]# ls {1,3}.txt 1.txt 3.txt
[root@localhost ~]# echo "line 1" > 1.txt [root@localhost ~]# cat 1.txt line 1 # > 覆蓋以前的輸入 [root@localhost ~]# echo "line 2" > 1.txt [root@localhost ~]# cat 1.txt line 2
[root@localhost ~]# echo "line 3" >> 1.txt [root@localhost ~]# cat 1.txt line 2 line 3
錯誤輸出重定向和錯誤輸出重定向追加跟以前的相似,它會將命令執行時產生的錯誤輸出重定向或追加到文件中
# 將1.txt的內容做爲wc命令的輸入 [root@localhost ~]# wc -l < 1.txt 2
在使用腳本自動安裝源碼包時,咱們並不想輸出信息至終端,而是想將其存儲在文件中,方便後續查看:
[root@localhost httpd-2.2.34]# ./configure --prefix=/usr/local/apache2 >install.log 2> error.log