From the course: Linux: Bash Shell and Scripts
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Signals and traps - Linux Tutorial
From the course: Linux: Bash Shell and Scripts
Signals and traps
- [Instructor] Bash has a command, trap, that you use for catching signals and handling signals. You can even just ignore the signal, if you want. A handy thing to use the trap for is to gracefully die when the user does Control + C, instead of leaving behind some temporary files, for example, you can clean those up, and then terminate. The way the trap command works is you give the trap command a string of stuff to do, and then the name of the signal that you wanted to do that stuff for. Control + C is the interrupt signal, and the name of that is INT. Control + Backslash is the quit signal. So here we have a little script. We changed directory to slash, and we go into a seemingly infinite loop, and we print out looping, and then we do a du command, disk usage, and megabytes for everything. That'll take a little while, and that's gonna be looping and printing out the amount of disk space that all of the directories are taking. And we're gonna send error messages to dev/null, in case…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Using the coproc command5m 53s
-
(Locked)
Debugging scripts with -x and -u options4m
-
(Locked)
Signals and traps4m 14s
-
(Locked)
Using the eval and getopt commands6m 47s
-
(Locked)
Challenges: Debugging scripts using trap, eval, getopt, and coproc2m 49s
-
(Locked)
Solutions: Debugging scripts using trap, eval, getopt, and coproc7m 22s
-
(Locked)
-