shelllings

a practical way to learn shell
git clone https://git.davidvoz.net/shelllings.git
index
logs
tree
license

31_trap.sh
1#!/bin/sh
2
3# 'trap' is a very useful tool that I've been using on the exercises
4# where I said you didn't have to worry about deleting the created file.
5#
6# $ trap command SIGNAL
7#
8# For example, in your tests/ files, many will have trap statements
9#
10# $ cleanup() {
11# $ [ -f "file" ] && rm file
12# $ }
13# $ trap cleanup EXIT
14#
15# exit is the signal for trap to call cleanup(). Below are the different
16# signals that trap detects in standard POSIX
17#
18# $ trap command EXIT # catches both exit 1 and exit 0
19# $ trap command INT # catches ctrl+c kills
20# $ trap command TERM # catches a signal terminate, like 'kill'
21# $ trap command INT EXIT # catches different instances
22#
23# You don't have to call functions when running trap, you can run
24# commands in the same line
25#
26# $ trap 'echo "Exiting.."; exit' TERM
27#
28# Below is a simple creation of a file, write a trap line that prints
29# out "Removing..." and removes the file
30
31touch file.txt
32echo "kill me" > file.txt