shell基礎1

 

10月11日任務shell

8.1 shell介紹apache

8.2 命令歷史vim

8.3 命令補全和別名centos

8.4 通配符bash

8.5 輸入輸出重定向less

 

shell基礎

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支持特定語法,如邏輯判斷,循環等插件

歷史記錄 history

用戶執行過的命令都會記錄到其家目錄下的 .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文件沒法正常記錄完整執行記錄。所以在退出終端時,應該使用exitlogout**命令正常退出!

命令歷史快捷操做

  • 上一條命令 :!!
  • 執行history內的第n條命令:!n
  • 執行history內的最新的以command開頭的命令:!command

命令補全和別名

命令補全:tab鍵

  • 按一下:待選命令就一個,會自動補全
  • 按兩下:顯示待選命令列表
[root@localhost ~]# ch
chacl      chattr     chfn       chmod      chronyc    chrt
chage      chcon      chgrp      chown      chronyd    chsh
chat       chcpu      chkconfig  chpasswd   chroot     chvt

參數補全(默認不支持)

CentOS7支持參數補全(需安裝插件),CentOS6不支持。

  • 安裝軟件包:yum install -y **bash-completion **

安裝完畢需重啓系統!!!

別名的建立和使用

  • 查看當前系統內的別名
[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
  • 系統定義的部分和用戶自定義的別名:~/bashrc文件
[root@localhost ~]# cat ~/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
...
  • 其餘別名:/etc/profile.d/*.sh
[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
  • 匹配0或多個任意字符: *
[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

錯誤輸出重定向和錯誤輸出重定向追加跟以前的相似,它會將命令執行時產生的錯誤輸出重定向或追加到文件中

  • 錯誤輸出重定向: 2>
  • 錯誤重定向追加:2>>
  • 輸入重定向:wc -l < 1.txt
# 將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
相關文章
相關標籤/搜索