Red Hat Linux Essentials Unit 6 - Using the bash Shell

 

學習目標

 

command Line Shortcuts File Globbing
wildcard expansion
*,代表沒有或多個
?,代表只有一個 character
[0-9],符合清單中的其中一個
[0-47-9],0到4 and 7到9
[abc],符合清單中的其中一個
[a-zA-z],符何任何大小寫英文字母
[^abc],不在清單中的任一個

ex: $ rm ?o*
第二字母為0的字都刪除
字元的分類,The syntax for a characer calss is [:keyword:]
alpha,upper(大寫),lower(小寫),digit(數字),alnum,punct(符號),space
ex: [[:digit:]],[^[:alnum:]],[[:lower:]] = [a-z]

Command Line Shortcuts The Tab Key
輸入 command line 時可善用 tab 鍵,命令、檔案、目錄都可以補(completion)

 

 

Command Line Shortcuts History
$ history,可以查看之前輸入過的命令
鍵或^P(Ctrl+p),往上一個指令
鍵或^N(Ctrl+n),往下一個指令
!!,重覆上一個指令
!char,往回找最近執行 char 命令的舊指令
!num,執行指令編號為 num 的指令
!-n,執行前 n 個舊指令
!?abc,往回找最近執行過指令符合 abc 字元的舊指令,abc 不一定是完整指令
Ctrl+r,在 $ history 中搜尋關鍵字
Esc,.,按 Esc 之後再按 .,recall 上指令的最後一個(period)參數
Alt+.,可一直 recall 上一指令的(period)參數

 

 

Command Line Expansion The tilde ~
Tilde(~),代表家目錄
ex:
$ cat ~/.bashrc ← ~ 代表自己家目錄
$ cat ~julie/public_html ← ~julie 代表使用者 julie 的家目錄

 

Command Line Expansion Commands and Braced Sets
$()
``,在一個指令中可以優先執行,執行後的結果再併入外層指令一起執行
ex:
$ echo "Today is $(hostname)"
Today is localhost.localdomain
$ echo "The kernel is `uname -r`"
The kernel is 2.6.9-42.0.8.EL
批次建立檔案

$ touch student{1,2,4,5}
student1 student2 student4 student5
批次刪除檔案
$ rm -f student{1,2,4,5}
批次建立目錄,參數 -p 自動按照路徑建立2層以上目錄
$ mkdir -p work/{work1,work2,work3}/{user1,user2,user3}

 

Command Editing Tricks
command line 的輸入的各種技巧,快捷鍵
Ctrl+a,跳到指令最開頭
Ctrl+e,跳到指令最後頭
Ctrl+u,清除游標以前的指令
Ctrl+k,清除游標以後的指令
Ctrl+w,後前刪,一次刪一個 word

 

 

使用 gnome-terminal
Ctrl+Shift+t
,creates a new tab
Ctrl+PgUp/PgDn,切換 tab
Ctrl+Shift+c,拷貝terminal中的文字
Ctrl+Shift+v,貼上文字


Creating Shell Scripts
Shell script 的第一行 #!/bin/bash
/bin/bash,指定想要用的shell,其他如 csh,sh,perl,python...
內容中起頭 # 符號代表註解
變更script檔案權限,使其有執行權限
$ chmod +x myscript.sh
如何執行 script?
使用絕對路徑 $ /home/user/mysh.sh
使用相對路徑時若在目前工作目錄下 $ ./mysh.sh
另外一種執行 script 方式 $ /bin/bash mysh.sh

 

Lab 6 重點
批次建立檔案或目錄
$ touch {report,memo,graph}_{sep,oct,nov,dec}_{a,b,c}_{1,2,3}
$ ls -l *dec_b_?
$ ls *sep*
( * 可有可無,? 單指一個字元)
$ ls *c_[1-3] (1 到 3 任選其一都可以)
$ ls -Fd *c* (檔案及目錄較容易分別,檔案顯示 file,目錄顯示 dir/)  
拷貝 sysconfig 目錄到 ~/backups/,並且用 date '+%Y%m%d' 指令產生日期
cp -av /etc/sysconfig ~/backups/sysconfig-$(date '+%Y%m%d')

 

 

 

 

 

 

 

by SmallKen