-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_script_template.sh
46 lines (33 loc) · 1.01 KB
/
bash_script_template.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
# Set the script name and log file name
SCRIPT_NAME=$(basename "$0")
LOG_FILE="/var/log/${SCRIPT_NAME%.*}.log"
# Function to handle errors and log them to the log file
handle_error() {
local error_message="$1"
local line_number="$2"
# Log the error message
echo "${error_message}" | tee -a "${LOG_FILE}"
# Exit with non-zero status
exit 1
}
# Function to validate input parameters
validate_input() {
local input="$1"
# Add validation logic here
# Return 0 if input is valid, or 1 if input is invalid
return 0
}
# Trap any errors and log them
trap 'handle_error "Error on line ${LINENO}: $(echo $BASH_COMMAND)" "${LINENO}"' ERR
# Log the start of the script
echo "Starting ${SCRIPT_NAME} at $(date)" | tee -a "${LOG_FILE}"
# Validate input parameters
if ! validate_input "${1}"; then
handle_error "Invalid input: ${1}" "${LINENO}"
fi
# Add main script logic here
# Log the end of the script
echo "Ending ${SCRIPT_NAME} at $(date)" | tee -a "${LOG_FILE}"
# Exit with zero status
exit 0