1. 程式人生 > >shell介紹及基本用法

shell介紹及基本用法

shell alias 重定向

8.1:shell介紹:

shell是一個命令解釋器,提供用戶和機器之間的交互,支持特定語法,支持邏輯判斷、循環,並且每個用戶都可以有自己的shell:

Centos默認的shell是bash(Bourne Agin Shell):其實為了紀念sh的創造者bourne這個用戶:

常見的還有zsh(power - shell) ksh(korn - shell)這兩種:支持的特性比較少:

8.2:命令歷史:history

在系統中用戶使用的命令都會保存下來,會保存在當前用戶的家目錄下:

history命令

語法: history [-c]
-c:=clear 清除內存中的命令,不能刪除配置文件中的歷史命令

[root@adai003 ~]# history
1 ls
2 ls /tmp/
3 ls /boot/
4 ls /
5 dhclient
……
[root@adai003 ~]# ls /root/.bash_history/root/.bash_history history的家目錄

顯示使用過的命令歷史,默認保存1000條使用過的命令(註:此令需要是在正常關機操作情況下的處1000條命)!

history環境變量

  • 變量HISTSIZE

[root@adai003 ~]# echo $HISTSIZE1000

該變量決定命令歷史保存的命令的數目。

  • 定義變量HISTSIZE

編輯其配置文件
[root@adai003 ~]# vim /etc/profile ……
HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000……
[root@adai003 ~]# echo $HISTSIZE1000[root@adai003 ~]# source /etc/profile[root@adai003 ~]# echo $HISTSIZE2000

搜索關鍵字"HIST"找到‘HISTSIZE=1000’,在此更改其數字,保存退出,然後執行命令‘source /etc/profile’刷新該配置文件才會生效。

  • 更改history顯示格式

[root@adai003 ~]# echo $HISTTIMEFORMAT[root@adai003 ~]# HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "[root@adai003 ~]# echo $HISTTIMEFORMAT%Y/%m/%d %H:%M:%S
[root@adai003 ~]# history
1 2017/06/28 18:50:11 history 2 2017/06/28 18:51:32 echo $HISTTIMEFORMAT 3 2017/06/28 18:51:43 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
4 2017/06/28 18:51:45 echo $HISTTIMEFORMAT 5 2017/06/28 18:52:32 history

直接為‘HISTTIMEFORMAT’賦值即可,不過此時該格式只適用於當前終端。如果要其使用於所有用戶,則需要將其寫入history配置文件並刷新後生效。

[root@adai003 ~]# vim /etc/profile ……
HOSTNAME=`/usr/bin/hostname 2>/dev/null`HISTSIZE=1000HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "……
保存退出!
[root@adai003 ~]# source /etc/profile
  • 命令歷史永久保存
    即,使命令歷史記錄只能寫入不能被刪除!

[root@adai003 ~]# chattr +a ~/.bash_history

使用文件特殊權限,為‘.bash_history’文件配置‘a’權限(只可追加,不可刪除),限於正常關機操作。

‘!!’命令

[root@adai003 ~]# w……
[root@adai003 ~]# !!w
……

‘!’的用法:‘!n’(n代表數字),表示運行命令歷史中的第n條命令;‘!word’,表示運行上一次以該word開頭的命令。
eg:

[root@adai003 ~]# history
1 2017/06/28 18:50:11 history 2 2017/06/28 18:51:32 echo $HISTTIMEFORMAT 3 2017/06/28 18:51:43 HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
4 2017/06/28 18:51:45 echo $HISTTIMEFORMAT 5 2017/06/28 18:52:32 history
[root@adai003 ~]# !4echo $HISTTIMEFORMAT
%Y/%m/%d %H:%M:%S
[root@adai003 ~]# !HISTHISTSIZE=1000

8.3 命令補全和別名

命令補全Tab

按一次tab可以補全一個命令或參數(需要安裝包bash-completion,並重啟系統);按兩次tab可以顯示以某字母開頭的所有命令或文件名。

alias命令

語法: alias [命令別名]=[具體命令] 設置別名
取消別名:unalias [命令別名]

直接輸入alias會顯示系統所有的別名:

[root@adai003 ~]# aliasalias 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@adai003 ~]# 

系統別名存放在配置文件‘~/.bashrc’和‘ls /etc/profile.d/’下:

[root@adai003 ~]# cat !$cat .bashrc# .bashrc# User specific aliases and functionsalias rm=‘rm -i‘alias cp=‘cp -i‘alias mv=‘mv -i‘# Source global definitionsif [ -f /etc/bashrc ]; then
. /etc/bashrcfi[root@adai003 ~]# ls /etc/profile.d/256term.csh colorgrep.sh lang.sh qt-graphicssystem.sh which2.sh
256term.sh colorls.csh less.csh vim.csh
bash_completion.sh colorls.sh less.sh vim.sh
colorgrep.csh lang.csh qt-graphicssystem.csh which2.csh

8.4 通配符

  • 通配符‘*’代表零個或多個任意字符

  • 通配符‘?’代表一個任意字符

  • 中括號‘[]’,“ls [0-9].txt”表示0-9區間內的任意.txt文件

  • 花括號‘{}’,“ls {1,2,3}.txt”表示括號內任意.txt文件

輸入輸出重定向

“>,>>,<,2>,2>>”
‘>’:輸出重定向
‘>>’:追加重定向
‘2>’:錯誤重定向
‘<’:輸入重定向
使用‘>’命令時會將文件內原有內容刪除。

[root@adai003 tmp]# echo adaixuelinux > 1.txt[root@adai003 tmp]# cat 1.txtadaixuelinux
[root@adai003 tmp]# echo adaixu > 1.txt[root@adai003 tmp]# cat 1.txtadaixu#####################################[root@adai003 tmp]# echo adaixu >> 1.txt[root@adai003 tmp]# cat 1.txtadaixu
adaixu#####################################[root@adai003 tmp]# lsaaa-bash: lsaaa: 未找到命令
[root@adai003 tmp]# lsaaa 2> 2.txt[root@adai003 tmp]# cat 2.txt-bash: lsaaa: 未找到命令

輸入重定向:必須定向到(<左邊)一個命令下
[root@adai003 tmp]# wc -l 1.txt “ wc -l”該命令用於查看文件行數2 1.txt
  • 應用

[root@adai003 tmp]# ls {1,2}.txt aaaa.txt > 1.txt 2> 3.txt[root@adai003 tmp]# cat 1.txt1.txt2.txt
[root@adai003 tmp]# cat 3.txtls: 無法訪問aaaa.txt: 沒有那個文件或目錄

說明: 使用ls命令查看 {1,2}.txt aaaa.txt,1.txt和2.txt文件存在,可以使用ls查看,aaaa.txt不存在,使用ls查看會報錯,‘> 1.txt 2> 3.txt’意思是將正確信息保存到1.txt,將錯誤信息保存到3.txt。


本文出自 “芬野_de博客” 博客,請務必保留此出處http://yuanhaohao.blog.51cto.com/7714752/1982188

shell介紹及基本用法