-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTRAP-COMMAND-LINUX.txt
More file actions
98 lines (66 loc) · 2.27 KB
/
TRAP-COMMAND-LINUX.txt
File metadata and controls
98 lines (66 loc) · 2.27 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
https://www.shellscript.sh/trap.html
http://redsymbol.net/articles/bash-exit-traps/
http://redsymbol.net/articles/bash-exit-traps/
https://www.learnshell.org/en/Bash_trap_command
https://www.putorius.net/using-trap-to-exit-bash-scripts-cleanly.html --> IMP
https://www.youtube.com/watch?v=tF0Qau7zcsw
https://spin.atomicobject.com/2017/08/24/start-stop-bash-background-process/ --> IMP
https://www.linuxjournal.com/content/bash-trap-command
TRAP example
#!/bin/bash
FILE="/tmp/file.log"
function delete_file() {
[ -f ${FILE} ] && rm -rf ${FILE} && echo "${FILE} deleted" || echo "file ${FILE} not present"
}
trap delete_file EXIT
grap subham /etc/passwd
exit 1
==================================================
function egress {
rm -f /tmp/output.txt
systemctl start smb.service
iptables -D INPUT -p tcp -s 10.0.0.222 --dport 32400 -j ACCEPT
}
# Call the egress function
trap egress EXIT
# Script contents
# Do some stuff
# Then some more stuff
# Do stuff to the stuff already done
# etc...
# When the script is completed or exits for any reason, the
# commands in the egress function will be executed.
Conclusion
===================================================
#!/bin/bash
trap "iptables -D INPUT -p tcp -s 10.0.0.222 --dport 32400 -j ACCEPT" EXIT
echo "Opening ports for kiddos 2 hours of TV time"
iptables -I INPUT -p tcp -s 10.0.0.222 --dport 32400 -j ACCEPT
sleep 7200
=================================================================
function cleanup() {
rm -rf "${BUILD_DIR}"
rm -f "${LOCK_FILE}"
# get rid of /tmp detritus, leaving anything accessed 2 days ago+
find "${BUILD_DIR_BASE}"/* -type d -atime +1 | rm -rf
echo "cleanup done"
}
trap cleanup TERM INT QUIT
=======================================================
#!/bin/bash
# bash trap command
trap bashtrap INT
# bash clear screen command
clear;
# bash trap function is executed when CTRL-C is pressed:
# bash prints message => Executing bash trap subrutine !
bashtrap()
{
echo "CTRL+C Detected !...executing bash trap !"
}
# for loop from 1/10 to 10/10
for a in `seq 1 10`; do
echo "$a/10 to Exit."
sleep 1;
done
echo "Exit Bash Trap Example!!!"