บทความนี้อธิบายการใช้ functions ใน Bash scripts – ช่วยให้คุณนำโค้ดกลับมาใช้ใหม่และลดความซับซ้อนของสคริปต์
ทำไมต้องเขียนโค้ดซ้ำๆ หากเขียนครั้งเดียวแล้วนำกลับมาใช้ได้?
นั่นคือสิ่งที่ functions ให้คุณทำ
บทความนี้ outline การใช้ functions ใน Bash/Shell scripts
Function คืออะไร?
Function คือชิ้นส่วนโค้ดที่นำกลับมาใช้ใหม่ได้ Functions สามารถรับ parameters ซึ่งทำให้พวกมันทำซ้ำ actions เดียวกันบน inputs ที่ต่างกันได้
Functions ปกติทำ action ใดๆ output หรือ print results หรือ return ค่าเพื่อใช้ภายหลัง
ทำไมต้องใช้ Function?
ตัวอย่าง คุณอาจมีสคริปต์ที่ส่ง email notification ว่าเกิดบางอย่างขึ้น หากต้องการส่งหลายอีเมลต่างๆ ไปหลาย recipients คุณอาจต้องเขียน email-sending code หลายครั้งในสคริปต์เดียวกัน
ด้วย function คุณเขียนโค้ดครั้งเดียว แล้ว call มันด้วย recipients, subjects, messages ต่างๆ ด้วย single line function call แทน duplicated code
Bash Function Syntax
ไวยากรณ์สำหรับเขียน functions ใน Bash/Shell scripts มีดังนี้:
name_of_function () { # COMMANDS # Optional return value }
โปรดทราบว่า:
- name_of_function คือชื่อที่คุณต้องการ call function
- Alphanumeric characters และ underscores เท่านั้น!
- Function name ต้องตามด้วย () (standard brackets)
- สังเกต spaces รอบ brackets! พวกมัน required!
- โค้ดที่ต้องการให้ function execute ต้อง wrap ใน {} (curly brackets)
- โค้ดนอก curly brackets ไม่ใช่ส่วนของ function และจะไม่ execute
- COMMANDS สามารถเป็นคำสั่งใดๆ ที่ปกติ available บน Linux system ของคุณ
- คุณสามารถ return ค่าได้ optional – ดูตัวอย่างด้านล่าง
- เพื่อ terminate function ก่อนเวลา สามารถ call return โดยไม่มีค่าเพื่อ exit function
- Special variable $? จะเก็บ return value จากคำสั่งล่าสุดที่ execute ทำให้พร้อมใช้งานใน script หลัง function รันเสร็จ
ตัวอย่าง & คำอธิบาย
นี่คือตัวอย่างที่ illustrate ทุก element ของ Bash function – พร้อม comments อธิบายทุกอย่าง
#!/bin/bash # Define a global variable - available anywhere in the script as it is not defined inside a function or loop initial_value=3 # Define a function which does some mathematics my_math_function () { # Get the parameters passed to the function # Parameters are passed after the function name when calling the function, and will be named in order of appearance, starting with $1, then $2, etc # Below, these parameter values are assigned to local variables - available only inside the function - so that it's easier to tell what they are local multiplier_value=$1 local addition_value=$2 # Calculate the result and assign it to a local variable # Notice the $(( )) wrapping the calculations - this tells the script to assign the result of these calculations to the results variable, rather than assigning the calculations as a text value local result=$(( $initial_value * $multiplier_value + $addition_value )) # Print the result to the console # Depending on how the function is used, text output from the function can be used to read results from it echo $result # It is also possible to get output from the function using a return value return $result } # Call the function with different input parameters # Parameters are passed to the function by typing them after the function separated by a space # The function above expects two parameters - a multiplier_value and addition_value my_math_function 2 4 # Will output 10 (2 * 3 + 4) my_math_function 3 5 # Will output 14 (3 * 3 + 5) # The $? is a special variable in Bash scripts which always holds the return value from the last executed command # It can be used to get the value specified as the return value from the function. echo $? # Will output 14 # Assign the result of the function to a variable # This will assign any text outputted by the function (for example using the echo command) to a variable my_result=$(my_math_function 6 7) echo $my_result # Will output 25 (6 * 3 + 7)
What is the ‘#!’ in Linux Shell Scripts?
Want to pass values to your shell script (so you can then pass them to the functions in it)? Check out this article.