Linux Shell Script筆記
何謂Shell
- 使用者登入後,LINUX系統會建立一個操作的環境,是介於使用者和LINUX核心之間的溝通介面。When you log on, the LINUX system starts running a program that acts as an interface between you and the LINUX kernel.
- the primary purpose of the shell is to interpret your commands
- it is also known as the LINUX command interpreter
- a shell is an interpreted program
- 解釋使用者所下達的指令。
- 指令直譯器。
Linux 的 Bash Shell 命令提示字串可以透過 PS1 這個環境變數來設定,通常他都是寫在 ~/.bashrc 或是 ~/.bash_profile 這些 Bash 的設定檔中。
指令種類
- 可分為兩種,分別為「內部(internal)指令」與「外部(external)指令」。
A shell command can be internal (built-in) or external. The code execute an internal command is part of the shell process, but the code to process an external command resides in a file in the form of a binary executable program file or a shell script.
- 內部指令:shell執行程序的一部分。
- 外部指令:以獨立檔案形式存在 (二進制執行檔或可執行的指令稿)。
- 如何區分:
- 指令簡易說明的查詢方式
- 內部指令:
help [指令]
- 外部指令:
[指令] --help
- 內部指令,以
cd
為例

- 外部指令,以
mv
為例

內部指令
- 指內建在 shell 中的指令
- 當你執行這類的指令時,系統並不會因為要執行這個指令而產生額外的行程(process)
- 執行效率非常高
外部指令
- 指那些不是內建於 shell 中的指令
- 通常是一個二進位的執行檔或是一個可執行的指令稿(script)
- 執行這類的指令時,系統就會產生而外的行程(process)
- 執行效率也就會比較低一些
- 外部指令要能跨存放位置執行,依靠PATH環境變數的定義
The names of the directories that a shell searches to find the file corresponding to an external command are stored in the shell variable PATH. Directory names are separated by colons in the Bash shell.
- PATH環境變數定義著外部指令所存置的目錄清單。
- 這個目錄清單,通常被稱為shell的search path。
- 目錄清單以冒號(:)區隔。
指令格式
- 通常分為三個部分
The shell interprets your command by assuming that the first word in a command line is the name of the command that you want to execute. It assumes that any of the remaining words starting with a hyphen (-) are options and that the rest of them are the command arguments.
- COMMAND
- OPTIONS
- Long options: –now, –add
- Short options: -n, -a
- ARGUMENTS
執行程序 (Process)
啟動程序
- 當LINUX系統一開機時,kernel會用程序的型態先啟動一些自己的工作,並啟動一個叫做init的程式。
- 早期常見SysV init、BSD init以及Linux Standard Base (LSB)的init。
- RHEL 6之前採用SysV int,RHEL 7之後則改採 Systemd int。
- 當今LINUX發行版本大多採用Systemd init。
- 可使用以下指令得知所使用的作業系統採用何種init機制。
stat /sbin/init
cat /proc/1/comm
ps -p 1
- init會執行一系列存置於 /etc 目錄下的shell script,稱之為「init script」,這些會啟動所有的系統服務。
- 每個程序都會被賦予一個 process ID (PID),PID的值會以升序(asending order)的方式去分配,但 init 永遠都會是PID 1。
建立子程序
- The shell executes commands by creating child processes using the fork and exec system calls.
- Every LINUX process has several attributes, including process ID (PID), process ID of parent (PPID), process name, process state (running, suspended, swapped, zombie, etc.), the terminal the process was executed on, the length of time the process has run, and process priority.
- The ps command can be used to display these attributes.
- The pstree command can be used to display process hierarchies.
背景執行
- 有的程序會在背景執行,不會直接與使用者互動。
- 背景執行的程序有兩個來源:使用者手動拋到背景處理的任務、系統服務(services 或 daemons)。
- 系統服務(services 或 daemons)的名稱通常都會以字母d做結尾,如:httpd、sshd或是ftpd。
- 要把程序放到背景執行,只要在指令後面多一個 & 符號。
- 透過 job 指令可以看到背景執行的指令。
- 透過 fg 指令可以把背景執行的程序送回前景執行。
- 透過 kill 指令可以把背景執行的程序強制終止掉。
標準輸入輸出
- 每個執行程序都有一個輸入、兩個輸出
- (0) 基本輸入: stdin
- (1) 基本輸出: stdout
- (2) 基本輸出錯誤: stderr
- 管線指令(|): 把一個命令的輸出傳遞給另一個命令當作輸入。
- 輸出結果比較
執行狀態回覆 (Exit Status Code)
- All LINUX commands return an exit status of 0 upon success and nonzero upon failure. The return status value of a command is stored in the read-only environment variable
$?
and can be checked by the calling process.
- 供使用者或執行程序了解上一個指令的執行狀態。
- 大部分的指令或動作都會回覆執行狀態。
- 使用
echo $?
的方式提取上一個指令的執行狀態碼。
- 0代表指令執行成功,非0代表指令執行有狀況。

On POSIX systems the standard exit code is 0 for success and any number from 1 to 255 for anything else.
Shell Script
- consist of shell commands to be executed by a shell and is stored in an ordinary LINUX file.
- A positional parameter is an argument specified on the command line, used to launch the current process in a shell. Positional parameter values are stored in a special set of variables maintained by the shell.

- IFS 是 Internal Field Separator 的縮寫,是 bash 的特殊變數,用在做字串分割 (splitting) 時做為切割字元 (delimiter),預設值是 space, tab 與 newline 。

各種指令介紹
test指令
- 檔案目錄存在與屬性檢查
Shell Script 測試檔案是否可讀或可寫入
-e
:檢查檔案是否在在
-f
:檢查檔案是否存在 (且為正規檔案 regular file)

-d
:檢查目錄是否存在
-s
:檢查檔案是否存在 (且為非空文件 not empty)
-r
:檢查檔案的讀取性
-w
:檢查檔案的可寫性
-x
:檢查檔案的可執行性
- 檔案時間與關連比較
-nt
:檢查 檔案A 是否比 檔案B 新
-ot
:檢查 檔案A 是否比 檔案B 舊
-ef
:檢查 檔案A 是否與 檔案B 有鏈結關係
- 空值檢查
- 字元比較
==
:檢查字串是否相等
!=
:檢查字串是否不相等
<
:檢查是否小於某字串
>
:檢查是否大於某字串
- 數字比較
-eq
:檢查是否等於某值
-ne
:檢查是否不等於某值
-lt
:檢查是否小於某值
-gt
:檢查是否大於某值
-le
:檢查是否小於等於某值
-ge
:檢查是否大於等於某值
- 指令呈現方式
- 正規寫法:
test expression
- 簡單寫法:
[ expression ]
系統設定指令簡介
Linux指令三劍客
- awk指令:
awk 'pattern' filename
awk '{action}' filename
awk 'pattern {action}' filename
awk 'BEGIN{action}{action}END{action}' filename'
- sed指令:
sed [option] '[address]command' filename
- grep指令:
grep [option] pattern filename
- 通常是Linux指令三劍客之一,與其他指令相互搭配
- 案例:看線上使用者
- tee指令:將輸出的副本送到標準輸出,另一副本拷貝到相應文件中。
- grep 指令
- Option
- -c 顯示出現次數
- -l 顯示文件名稱
- -n 顯示行號
- -i
- -v
- 正則表達式
- Linux中搜尋檔案 whereis/locate/find
應用
其他補充資料
Windows的Powershell