-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrap_and_debug_example.sh
More file actions
35 lines (25 loc) · 1.04 KB
/
Copy pathtrap_and_debug_example.sh
File metadata and controls
35 lines (25 loc) · 1.04 KB
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
#!/usr/bin/env bash
# Description: This script will demonstrate using trap to run a function after the script exists successfully. It will also demonstrate how to use trap as a debugging tool.
# NOTE: This was demonstrated, learned from Dave Eddy's (YSAP) bash scripting course on youtube https://www.youtube.com/watch?v=uAmIIWYMgS4&list=PL-my9REMIFtGgiQAXqKPJ5UrLdSkxcLBT
# cleanup function
function cleanup() {
echo -e "\nScript has ran \e[32msuccessfully\e[0m. Will now commence to clean up after itself."
}
# debug function
function debug_me() {
# BASH_COMMAND is a bash built-in variable that holds the command
# that is currently being executed
echo -e "\n\e[33mDEBUG\e[0m: \e[34m${BASH_COMMAND}\e[0m"
}
# use trap to call the cleanup function on exit
trap cleanup exit
# use trap to call the debug_me function for debugging
#trap debug_me debug
# logic to print colors
for i in {1..256}
do
echo -e "\e[${i}mColor ${i}\e[0m\n"
done
# successful exit
exit 0
# NOTE: for more info on using trap see: https://share.google/aimode/cCmXCS9RNz2316guo