Shell Script 簡單來說,就是 Shell 指令的巨集,而且還有包含了程式語法功能。它能能做到許多神奇的事情,如:
- 自動化管理的重要依據
- 追蹤與管理系統的重要工作
- 簡單入侵偵測功能
- 連續指令單一化
- 簡易的資料處理
- 跨平台支援與學習歷程較短
Hello World
首先使用編輯器建立一個檔案取名叫 helloworld.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo -e "Hello World! \a \n"
exit 0執行程式:在 Linux CLI 下,切換到該檔的目錄下,再執行:
$ chmoe +x ./helloworld.sh
$ sh ./helloworld.shread
讀取使用者輸入的字串,這樣就可以做出簡易選單了
read key
case $key in
"1")
echo "你選的是 1"
;;
"2")
echo "你選的是 2"
;;
esacCheck file
基本檢查檔案是否存在的方法
# 存在會回傳 Found
[ -e filename ] && echo "Found"
# 不存在會回傳 Not Found
[ -e filename ] || echo "Not Found"
# 存在會回傳 Found,且不存在會回傳 Not Found
[ -e filename ] && echo "Found" || echo "Not Found"參數參考
-e單純判斷檔案在不在-d判斷檔案存在並且是個目錄-f判斷檔案存在並且就是檔案
check program exists
檢查程式是否存在 (或安裝)
$ command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
$ type foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
$ hash foo 2>/dev/null || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }