Article 53DNH [SOLVED] About Traps in bash script

[SOLVED] About Traps in bash script

by
ddenial
from LinuxQuestions.org on (#53DNH)
Hello All

I'm testing traps in Linux, and hence I've some doubts. Following is script for testing traps.
Code:#!/bin/bash
set -euo pipefail

echo "Script PID: $$"

function CleanUp {
echo
echo Oops I just got killed
exit 2
}
trap 'CleanUp' SIGHUP SIGINT SIGQUIT SIGPIPE SIGTERM EXIT

while true ; do
sleep 1
doneOutput:
Code:$ ./test.sh
Script PID: 16467
^C
Oops I just got killed

Oops I just got killedMy doubts are:
  1. What is 'set -euo pipefail'? I've seen most bash scripts start with this line. I know 'set -u', which makes mandatory to declare a variable before using it. But what about -e and -o pipefail? Read man pages, but couldn't understand.
  2. Why I'm getting multiple trap echo lines?
  3. How do I get exit code based on the type of interrupt signal rather than the exit code which I have specified explicitly (like 2 here)?
Thankslatest?d=yIl2AUoC8zA latest?i=4O_4MSKwXuM:MTA7eBwBIQw:F7zBnMy latest?i=4O_4MSKwXuM:MTA7eBwBIQw:V_sGLiP latest?d=qj6IDK7rITs latest?i=4O_4MSKwXuM:MTA7eBwBIQw:gIN9vFw4O_4MSKwXuM
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments