shell介紹及基本用法

2021-09-20 22:51:45 字數 4747 閱讀 6596

8.1:shell介紹:

shell是乙個命令直譯器,提供使用者和機器之間的互動,支援特定語法,支援邏輯判斷、迴圈,並且每個使用者都可以有自己的shell:

centos預設的shell是bash(bourne agin shell):其實為了紀念sh的創造者bourne這個使用者:

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

8.2:命令歷史: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條命)!

[root@adai003 ~]# echo $histsize1000

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

編輯其配置檔案

[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』重新整理該配置檔案才會生效。

[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

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

語法: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

「>,>>,<,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 .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命令檢視 .txt aaaa.txt,1.txt和2.txt檔案存在,可以使用ls檢視,aaaa.txt不存在,使用ls檢視會報錯,『> 1.txt 2> 3.txt』意思是將正確資訊儲存到1.txt,將錯誤資訊儲存到3.txt。

Struts基本介紹及用法

struts是一筐經典的mvc框架 1.在web.xml中配置actionservlet action org.apache.struts.action.actionservlet config web inf struts config xml debug 3 detail 3 startup 0...

Shell基本用法 2

if condition then fi if condition then else fi if condition then elif fi 邏輯與 邏輯或 a b c a為true則執行b,a為false則執行c history 檢視歷史命令 crtl r 查詢命令 for c1 c2 c3 ...

Shell之基本用法

一 shell簡介 1.什麼是shell shell的中文意思是 外殼 通俗地講,shell是乙個互動程式設計介面,通過獲得使用者輸入來驅動作業系統核心完成指定工作。shell除了作為命令解釋程式以外,還是一種高階程式設計語音,它有變數 關鍵字 有各種控制語句 支援函式模組,有自己的語法結構。she...