Linux中有許多指令是方便管理者快速管理系統的小工具,也可將這些指令在運用在script中以增加管理者的方便性。

echo
echo是個很常用的指令,可將輸入的文字顯示在營幕上也可利用">"或">>"將輸入的文字寫入檔案,也可叫出變數內容。
將輸入的文字顯示在營幕

ubuntu@ubuntu: echo Hi Welcome
Hi Welcome

輸入的文字寫入檔案

ubuntu@ubuntu: echo abc >echotest.txt #將abc導入echotest.txt
ubuntu@ubuntu: ls
echotest.txt #echotest.txt被建立了
ubuntu@ubuntu: cat echotest.txt  #顯示echotest.txt檔案內容
abc

回傳變數內容

ubuntu@ubuntu: echo HOSTNAME #回傳主機名稱變數值
ubuntu

特殊字元
某些特殊字元無法在command line中顯示,可使用ANSI-C規格的特殊字元"\"來表示

特殊字元
特殊字元 說明
\\ 單純顯示\符號
\' 單純顯示'符號
\" 單純顯示"符號
\a 警示聲(Be)
\b backspace鍵
\n 饋行
\t 水平定位點,同tab
\v 垂直定位點

希望回傳特殊字元的結果時需加上-e參數才會將\視為特特殊字元處理

ubuntu@ubuntu: echo "line1\nline2\nline3"
line1\nline2\nline3 #未加-e不會饋行
ubuntu@ubuntu: echo -e "line1\nline2\nline3"
line1
line2
line3 #加上-e後\n被解讀為饋行

alias 別名
Linux的shell有別名的功能,可以針對常用指令自訂別名,將常用的指令設定別名後可簡化指令。

ubuntu@ubuntu: alias #alias不加任何指令可以顯示目前已設定好的別名
alias ls='ls --color=auto'
ubuntu@ubuntu: alias ifc='ifconfig -a' #將ifconfig -a設定別名為ifc
ubuntu@ubuntu: ifc #執行ifc後的結果和執行ifconfig -a一樣
eth0      Link encap:Ethernet  HWaddr 44:15:1a:6d:00:05  
          inet addr:192.168.0.2  Bcast:192.168.0.254  Mask:255.255.255.255
          inet6 addr: fe80::4415:1aff:fe6d:5/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1460  Metric:1
          RX packets:295308 errors:0 dropped:0 overruns:0 frame:1
          TX packets:288002 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:259944983 (259.9 MB)  TX bytes:44758045 (44.7 MB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:19 errors:0 dropped:0 overruns:0 frame:0
          TX packets:19 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:1850 (1.8 KB)  TX bytes:1850 (1.8 KB)
ubuntu@ubuntu: unalias #刪除別名
ubuntu@ubuntu: ifc
No command 'ifc' found, but there are 16 similar ones
ifc: command not found #無法找到該指令

alias所定義的別名,只會在目前工作階段保留,若關閉終端機或登出,所定義的別名就會消失,在ubuntu若要永久定義別名可將別名寫入"/.bash_aliases"檔中(在CentOS或Fedora則是在/.bashrc檔中),在ubuntu中預設是沒有.bash_aliases這個檔的,需要自己建立,建立檔案會需要再重新登入一次才會所設定的別名才會生效。

ubuntu@ubuntu: vim .bash_aliases #使用編輯軟體編輯.bash_aliases
alias ifc='ifconfig -a'
alias dfh='df -h'
存當後離開,ubuntu需重新登入
ubuntu@ubuntu: dfh #使用新設定的別名執行
Filesystem      Size  Used Avail Use% Mounted on
udev            283M  4.0K  283M   1% /dev
tmpfs            59M  284K   59M   1% /run
/dev/sda1       9.9G  2.9G  6.5G  31% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
none            5.0M     0  5.0M   0% /run/lock
none            295M     0  295M   0% /run/shm
none            100M     0  100M   0% /run/user

統計檔案行數與字數 wc
wc指令(word count)可統計檔案內有多少行(newline)、英文字(word)及位元組(byte)

ubuntu@ubuntu: wc /var/log/dmesg #計算/var/log/dmesg之行數、英文字數及byte數
  400  3424 27270 /var/log/dmesg #檔案內有400行 3424個英文字 27270byte
ubuntu@ubuntu: wc -l /var/log/dmesg #計算/var/log/dmesg之行數
400 /var/log/dmesg #檔案內有400行
ubuntu@ubuntu: wc -w /var/log/dmesg #計算/var/log/dmesg之byte數
3424 /var/log/dmesg #檔案內有3424個英文字
ubuntu@ubuntu: wc -c /var/log/dmesg #計算/var/log/dmesg之英文字數
27270 /var/log/dmesg #檔案內有27270byte

取代與刪除字元 tr
語法:tr 欲被置換或刪除之字元 欲罝換之字元
tr可以取代或刪除文件中的字串

ubuntu@ubuntu: cat /etc/passwd #先查看正常/etc/passwd檔案內容
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash
ubuntu@ubuntu: cat /etc/passwd tr "\:" "\/" #使用tr將:改為tab
root    x       0       0       root    /root   /bin/bash
daemon  x       1       1       daemon  /usr/sbin       /usr/sbin/nologin
ubuntu  x       1000    1000    Ubuntu  /home/ubuntu    /bin/bash
ubuntu@ubuntu: cat /etc/passwd tr "\:" "\/"tr -d x #使用tr將:改為tab,再將"x"的刪除
root            0       0       root    /root   /bin/bash
daemon          1       1       daemon  /usr/sbin       /usr/sbin/nologin
ubuntu          1000    1000    Ubuntu  /home/ubuntu    /bin/bash
tr定義
參數 說明
[:alnum:] 所有大小寫字母與數字
[:alpha:] 所有大小寫字母
[:blank:] 空白
[:digit:] 所有數字
[:lower:] 所有小寫字母
[:upper:] 所有大寫字母
ubuntu@ubuntu: cat /etc/passwd tr [:lower:] [:upper:] #先查看正常/etc/passwd檔案內所有小寫字母換成大寫字母
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
DAEMON:X:1:1:DAEMON:/USR/SBIN:/USR/SBIN/NOLOGIN
UBUNTU:X:1000:1000:UBUNTU:/HOME/UBUNTU:/BIN/BASH

產生序列數字 seq
語法:seq 開始數字 結束數字

seq參數
參說 說明
-f 使用printf格式定義格式
-w 將所有輸出使用相同位元顯示(即補0)
-s 在輸出的數字加上指定字串
ubunua@ubuntu: seq 1 5 #產生1至5的數字
1
2
3
4
5
ubuntu@ubuntu: seq 2 2 12 #由2開始加2至12
2
4
6
8
10
12
ubuntu@ubuntu: seq -w 6 10 #將每個數字都使用相同的位元數
06
07
08
09
10
ubuntu@ubuntu: seq -s - 1 2 10 #在每個數字間加入"-"
1-3-5-7-9
ubuntu@ubuntu: seq -f seq -f "%1g" 6 10  #使用printf格式輸出數字6-10
6
7
8
9
10

文字檔內容排序 sort
sort可用來將文字檔內的每一列做排序,並將排序的結果在營幕上顯示。

ubuntu@ubuntu: cat /etc/passwd #先查看未排序的/etc/passwd檔的內容
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
ubuntu@ubuntu: sort /etc/passwd #排序/etc/passwd檔的內容
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
games:x:5:60:games:/usr/games:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin

sort預設會以第一個字元的ASCII碼比較,若相同再比較第二個字元,以此類推,若要依數值大小排序可以使用-g參數。

ubuntu@ubuntu: cat sorttest  #使用cat查看原始檔案內容
32468
21655
7317
19566
1247
ubuntu@ubuntu: sort sorttest  #使用sort排序檔案
1247
19566
21655
32468
7317
ubuntu@ubuntu: sort -g sorttest  #使用sort -g排序檔案
1247
7317
19566
21655
32468

過慮重覆 uniq
uniq可將檔案中相鄰且重覆的多行資料合併為一行,使資料有唯一性。

ubuntu@ubuntu: cat uniqtest  #查詢原使檔案內容
1
1
4
7
3
7
9
6
3
4
0
9
7
3
2
1
7
4
3
5
7
ubuntu@ubuntu: uniq uniqtest  #使用uniq將連續重復的資料合併
1
4
7
3
7
9
6
3
4
0
9
7
3
2
1
7
4
3
5
7
ubuntudavid00050@ubuntu: sort uniqtest uniq #排序後再由uniq合併連續重復的資料
0
1
2
3
4
5
6
7
9

擷取每行特定欄位 cut

cut參數
參數 說明
-c 指定擷取特定位元
-d 自定分隔符號
-f 指定欄位數(分隔符號為tab)
ubuntu@ubuntu: ls -l cut -c 1-10 #列出檔案清單後擷取1-10字元
total 16
drwxr-xr-x
-rw-rw-r--
-rw-rw-r--
-rw-rw-r--
ubuntu@ubuntu: cut -f 2 /etc/services #擷取/etc/services第2個欄位
20/tcp
42/tcp
50/tcp
50/udp
65/tcp
ubuntu@ubuntu: cut -d':' -f 1 /etc/passwd #自訂分隔符號為":",並擷取第1欄位
root
daemon
bin

比較檔案 diff
語法:diff 第一個檔案 第二個檔案
diff可比較2個檔案是否相同,若相同則不會出現訊息,若不同則會把不同的地方以">"號列出

ubuntu@ubuntu: diff test test1 #比較test和test1是否不同
ubuntu@ubuntu:  #因為沒有不同所以無任何訊息
ubuntu@ubuntu: diff difftest difftest1 #比較difftest和difftest1是否不同
1c1
< abcdefg
---
> abcdefh #不同之行數以>標示

 


參考資料:
printf命令