# Shell Script :::info 日期 : 2025/02/26 ::: Shebang Line * 用來指定要使用的 Shell * 語法:#!/path/to/interpreter shell script 執行 ```bash chmod +x hello.sh # 賦予執行權限 ./hello.sh # 執行腳本 ``` --- ### 變數與使用者輸入 宣告變數: KEY=VALUE 使用者輸入變數: read KEY 獲取變數值: $KEY ```bash #!/bin/bash -> 這是shebang age=25 current_date=$(date) echo "What is your name?" read username echo "Hello, $username, you are $age years old on $current_date!" -> 這是輸出print ``` CLI  --- ### 條件判斷 (if/else) 與邏輯控制 ```bash #!/bin/bash echo "Enter your name:" read name echo "Enter your age:" read age if [ "$name" == "Jenn" ] && [ $age -gt 18 ]; then echo "Welcome, adult Jenn queen!" elif [ "$name" != "Jenn" ] && [ $age -gt 18 ]; then echo "Hello, adult $name!" else echo "Hello, child $name!" fi ```  --- ### 迴圈控制 (for, while, until) 遍歷list ```bash #!/bin/bash for name in Alice Bob Charlie do echo "Hello, $name!" done ```  遍歷數字 ```bash #!/bin/bash for i in {1..5} do echo "Number: $i" done ```  while loop ```bash #!/bin/bash count=5 while [ $count -gt 0 ] do echo "Countdown: $count" count=$((count - 1)) done echo "Time's up!" ```  --- ### 函數 (Functions) ```bash #!/bin/bash multiply() { result=$(( $1 * $2 )) return $result } multiply 3 4 echo "The product is $?" ```  --- ### 檔案操作 (File Operations) 1. 將內容寫入檔案 ```bash >:將內容寫入檔案 (會覆蓋檔案內容) >>:將內容附加到檔案末尾 (不會覆蓋原有內容) ``` ```bash #!/bin/bash echo "Enter the filename to create:" read filename echo "This is a new file created by Shell Script." > "$filename" echo "File '$filename' has been created." ```  新的new.txt  2. 檢查檔案是否存在 + 讀取檔案內容 ```bash -f:檢查檔案是否存在且為一般檔案 -d:檢查是否為資料夾 (directory) -e:檢查檔案或資料夾是否存在 cat:顯示檔案內容 head -n 5:顯示檔案前 5 行 tail -n 5:顯示檔案後 5 行 ``` ```bash #!/bin/bash echo "Enter the filename to check:" read filename if [ -f "$filename" ]; then echo "File '$filename' exists." else echo "File '$filename' does not exist." fi ```  --- ### 錯誤處理 (Error Handling) 1. Exit Code(退出狀態碼) 每個 Shell 命令執行後都會返回一個 Exit Code,可以用 $? 來查看,0代表Success,非0數值代表Error ```bash ls /nonexistent_directory # 列出目錄內容 echo $? # 輸出非0,因目錄不存在 ```  2. exit 指令 exit 用來終止腳本並回傳一個狀態碼,exit 0 代表成功,exit 1 代表錯誤 ```bash #!/bin/bash echo "開始執行" exit 1 # 這裡會讓腳本提前結束,並回傳 1 echo "這行不會執行" ```  3. set -e(遇錯即停) 任何命令發生錯誤時(Exit Code ≠ 0)自動停止 ```bash #!/bin/bash set -e echo "開始執行" ls /nonexistent_directory # 發生錯誤,腳本會立即停止 echo "這行不會執行" ```  4. trap(捕捉錯誤) 當腳本失敗時執行額外的動作 ```bash trap '...' ERR ``` 加入exit 1,可以讓外部系統(CI/CD Pipeline)或其他腳本正確偵測錯誤 ```bash #!/bin/bash trap 'echo "發生錯誤,腳本終止"; exit 1' ERR set -e ls /nonexistent_directory # 發生錯誤,觸發 trap echo "這行不會被執行" ```  5. || 處理錯誤 ```bash command1 || command2:如果 command1 失敗,則執行 command2,但兩種結果都會輸出 ``` ```bash echo "1th" ls /nonexistent_directory || echo "目錄不存在" echo "2th" echo "目錄不存在" || ls /nonexistent_directory ```  --- 參考資料 https://ithelp.ithome.com.tw/articles/10327486
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up