FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Bash Functions and Aliases for Traps, Kills, and Signals

Download: trapstuff.txt

See also: signal.h

Bash Trap and Kill Aliases

alias traps='trap -l|sed "s,\t,\n,g;s,),:,g;s,SIG,,g;s, \([0-9]\),\1,g"'
alias kills='for h in $(builtin kill -l); do echo "$(builtin kill -l $h): $h"; done';

traps output

1: HUP
2: INT
3: QUIT
4: ILL
5: TRAP
6: ABRT
7: BUS
8: FPE
9: KILL
10: USR1
11: SEGV
12: USR2
13: PIPE
14: ALRM
15: TERM
16: STKFLT
17: CHLD
18: CONT
19: STOP
20: TSTP
21: TTIN
22: TTOU
23: URG
24: XCPU
25: XFSZ
26: VTALRM
27: PROF
28: WINCH
29: IO
30: PWR
31: SYS
34: RTMIN
35: RTMIN+1
36: RTMIN+2
37: RTMIN+3
38: RTMIN+4
39: RTMIN+5
40: RTMIN+6
41: RTMIN+7
42: RTMIN+8
43: RTMIN+9
44: RTMIN+10
45: RTMIN+11
46: RTMIN+12
47: RTMIN+13
48: RTMIN+14
49: RTMIN+15
50: RTMAX-14
51: RTMAX-13
52: RTMAX-12
53: RTMAX-11
54: RTMAX-10
55: RTMAX-9
56: RTMAX-8
57: RTMAX-7
58: RTMAX-6
59: RTMAX-5
60: RTMAX-4
61: RTMAX-3
62: RTMAX-2
63: RTMAX-1
64: RTMAX

Trap and Kill Functions

Using traps are helpful to use in functions like this:

function yn ()
{ 
	local post=`tput op;tput sgr0;tput cnorm`;
	trap 'echo -e "${post}"' 1 2 3 6 15 RETURN;
	(
		local a= YN=1 msg="${@}" pre=`tput civis;tput setab 4;tput setaf 7` post=`tput op;tput sgr0;tput cnorm`;
		trap 'echo -e "${post}"' 1 2 3 6 15 RETURN;
		until [[ $YN == 0 || $YN == 65 ]]; do
			echo -en "\n\n${pre}:: ${msg}? (y/N)${post} " && read -s -n 1 a;
			case $a in 
				[yY]) YN=0;
				;;
				[nN]) YN=65;
				;;
			esac;
			echo;
		done;
		return $YN
	)
}

Info about a Signal number or name

function trapinfo () 
{ 
   local signum=${1:-} sigdesc="`trapdesc ${1:-}`";
   case $signum in 
	  1 | HUP | SIGHUP) echo "${signum}: ${sigdesc}: HUP - SIGHUP"
		 ;;
	  2 | INT | SIGINT) echo "${signum}: ${sigdesc}: INT - SIGINT"
		 ;;
	  3 | QUIT | SIGQUIT) echo "${signum}: ${sigdesc}: QUIT - SIGQUIT"
		 ;;
	  4 | ILL | SIGILL) echo "${signum}: ${sigdesc}: ILL - SIGILL"
		 ;;
	  5 | TRAP | SIGTRAP) echo "${signum}: ${sigdesc}: TRAP - SIGTRAP"
		 ;;
	  6 | ABRT | SIGABRT) echo "${signum}: ${sigdesc}: ABRT - SIGABRT"
		 ;;
	  7 | BUS | SIGBUS) echo "${signum}: ${sigdesc}: BUS - SIGBUS"
		 ;;
	  8 | FPE | SIGFPE) echo "${signum}: ${sigdesc}: FPE - SIGFPE"
		 ;;
	  9 | KILL | SIGKILL) echo "${signum}: ${sigdesc}: KILL - SIGKILL"
		 ;;
	  10 | USR1 | SIGUSR1) echo "${signum}: ${sigdesc}: USR1 - SIGUSR1"
		 ;;
	  11 | SEGV | SIGSEGV) echo "${signum}: ${sigdesc}: SEGV - SIGSEGV"
		 ;;
	  12 | USR2 | SIGUSR2) echo "${signum}: ${sigdesc}: USR2 - SIGUSR2"
		 ;;
	  13 | PIPE | SIGPIPE) echo "${signum}: ${sigdesc}: PIPE - SIGPIPE"
		 ;;
	  14 | ALRM | SIGALRM) echo "${signum}: ${sigdesc}: ALRM - SIGALRM"
		 ;;
	  15 | TERM | SIGTERM) echo "${signum}: ${sigdesc}: TERM - SIGTERM"
		 ;;
	  16 | STKFLT | SIGSTKFLT) echo "${signum}: ${sigdesc}: STKFLT - SIGSTKFLT"
		 ;;
	  17 | CHLD | SIGCHLD) echo "${signum}: ${sigdesc}: CHLD - SIGCHLD"
		 ;;
	  18 | CONT | SIGCONT) echo "${signum}: ${sigdesc}: CONT - SIGCONT"
		 ;;
	  19 | STOP | SIGSTOP) echo "${signum}: ${sigdesc}: STOP - SIGSTOP"
		 ;;
	  20 | TSTP | SIGTSTP) echo "${signum}: ${sigdesc}: TSTP - SIGTSTP"
		 ;;
	  21 | TTIN | SIGTTIN) echo "${signum}: ${sigdesc}: TTIN - SIGTTIN"
		 ;;
	  22 | TTOU | SIGTTOU) echo "${signum}: ${sigdesc}: TTOU - SIGTTOU"
		 ;;
	  23 | URG | SIGURG) echo "${signum}: ${sigdesc}: URG - SIGURG"
		 ;;
	  24 | XCPU | SIGXCPU) echo "${signum}: ${sigdesc}: XCPU - SIGXCPU"
		 ;;
	  25 | XFSZ | SIGXFSZ) echo "${signum}: ${sigdesc}: XFSZ - SIGXFSZ"
		 ;;
	  26 | VTALRM | SIGVTALRM) echo "${signum}: ${sigdesc}: VTALRM - SIGVTALRM"
		 ;;
	  27 | PROF | SIGPROF) echo "${signum}: ${sigdesc}: PROF - SIGPROF"
		 ;;
	  28 | WINCH | SIGWINCH) echo "${signum}: ${sigdesc}: WINCH - SIGWINCH"
		 ;;
	  29 | IO | SIGIO) echo "${signum}: ${sigdesc}: IO - SIGIO"
		 ;;
	  30 | PWR | SIGPWR) echo "${signum}: ${sigdesc}: PWR - SIGPWR"
		 ;;
	  31 | SYS | SIGSYS) echo "${signum}: ${sigdesc}: SYS - SIGSYS"
		 ;;
	  34 | RTMIN | SIGRTMIN) echo "${signum}: ${sigdesc}: RTMIN - SIGRTMIN"
		 ;;
	  35 | RTMIN+1 | SIGRTMIN+1) echo "${signum}: ${sigdesc}: RTMIN+1 - SIGRTMIN+1"
		 ;;
	  36 | RTMIN+2 | SIGRTMIN+2) echo "${signum}: ${sigdesc}: RTMIN+2 - SIGRTMIN+2"
		 ;;
	  37 | RTMIN+3 | SIGRTMIN+3) echo "${signum}: ${sigdesc}: RTMIN+3 - SIGRTMIN+3"
		 ;;
	  38 | RTMIN+4 | SIGRTMIN+4) echo "${signum}: ${sigdesc}: RTMIN+4 - SIGRTMIN+4"
		 ;;
	  39 | RTMIN+5 | SIGRTMIN+5) echo "${signum}: ${sigdesc}: RTMIN+5 - SIGRTMIN+5"
		 ;;
	  40 | RTMIN+6 | SIGRTMIN+6) echo "${signum}: ${sigdesc}: RTMIN+6 - SIGRTMIN+6"
		 ;;
	  41 | RTMIN+7 | SIGRTMIN+7) echo "${signum}: ${sigdesc}: RTMIN+7 - SIGRTMIN+7"
		 ;;
	  42 | RTMIN+8 | SIGRTMIN+8) echo "${signum}: ${sigdesc}: RTMIN+8 - SIGRTMIN+8"
		 ;;
	  43 | RTMIN+9 | SIGRTMIN+9) echo "${signum}: ${sigdesc}: RTMIN+9 - SIGRTMIN+9"
		 ;;
	  44 | RTMIN+10 | SIGRTMIN+10) echo "${signum}: ${sigdesc}: RTMIN+10 - SIGRTMIN+10"
		 ;;
	  45 | RTMIN+11 | SIGRTMIN+11) echo "${signum}: ${sigdesc}: RTMIN+11 - SIGRTMIN+11"
		 ;;
	  46 | RTMIN+12 | SIGRTMIN+12) echo "${signum}: ${sigdesc}: RTMIN+12 - SIGRTMIN+12"
		 ;;
	  47 | RTMIN+13 | SIGRTMIN+13) echo "${signum}: ${sigdesc}: RTMIN+13 - SIGRTMIN+13"
		 ;;
	  48 | RTMIN+14 | SIGRTMIN+14) echo "${signum}: ${sigdesc}: RTMIN+14 - SIGRTMIN+14"
		 ;;
	  49 | RTMIN+15 | SIGRTMIN+15) echo "${signum}: ${sigdesc}: RTMIN+15 - SIGRTMIN+15"
		 ;;
	  50 | RTMAX-14 | SIGRTMAX-14) echo "${signum}: ${sigdesc}: RTMAX-14 - SIGRTMAX-14"
		 ;;
	  51 | RTMAX-13 | SIGRTMAX-13) echo "${signum}: ${sigdesc}: RTMAX-13 - SIGRTMAX-13"
		 ;;
	  52 | RTMAX-12 | SIGRTMAX-12) echo "${signum}: ${sigdesc}: RTMAX-12 - SIGRTMAX-12"
		 ;;
	  53 | RTMAX-11 | SIGRTMAX-11) echo "${signum}: ${sigdesc}: RTMAX-11 - SIGRTMAX-11"
		 ;;
	  54 | RTMAX-10 | SIGRTMAX-10) echo "${signum}: ${sigdesc}: RTMAX-10 - SIGRTMAX-10"
		 ;;
	  55 | RTMAX-9 | SIGRTMAX-9) echo "${signum}: ${sigdesc}: RTMAX-9 - SIGRTMAX-9"
		 ;;
	  56 | RTMAX-8 | SIGRTMAX-8) echo "${signum}: ${sigdesc}: RTMAX-8 - SIGRTMAX-8"
		 ;;
	  57 | RTMAX-7 | SIGRTMAX-7) echo "${signum}: ${sigdesc}: RTMAX-7 - SIGRTMAX-7"
		 ;;
	  58 | RTMAX-6 | SIGRTMAX-6) echo "${signum}: ${sigdesc}: RTMAX-6 - SIGRTMAX-6"
		 ;;
	  59 | RTMAX-5 | SIGRTMAX-5) echo "${signum}: ${sigdesc}: RTMAX-5 - SIGRTMAX-5"
		 ;;
	  60 | RTMAX-4 | SIGRTMAX-4) echo "${signum}: ${sigdesc}: RTMAX-4 - SIGRTMAX-4"
		 ;;
	  61 | RTMAX-3 | SIGRTMAX-3) echo "${signum}: ${sigdesc}: RTMAX-3 - SIGRTMAX-3"
		 ;;
	  62 | RTMAX-2 | SIGRTMAX-2) echo "${signum}: ${sigdesc}: RTMAX-2 - SIGRTMAX-2"
		 ;;
	  63 | RTMAX-1 | SIGRTMAX-1) echo "${signum}: ${sigdesc}: RTMAX-1 - SIGRTMAX-1"
		 ;;
	  64 | RTMAX | SIGRTMAX) echo "${signum}: ${sigdesc}: RTMAX - SIGRTMAX"
		 ;;
	  65) echo "${signum}: ${sigdesc}: invalid signal specification"
		 ;;
	  *) echo "${signum}: ${sigdesc}: Unknown Signal"
		 ;;
   esac
}

Function to describe the signal

function trapdesc () 
{ 
   case $1 in 
	  0) echo "Bogus signal"
		 ;;
	  1) echo "Hangup"
		 ;;
	  2) echo "Interrupt"
		 ;;
	  3) echo "Quit"
		 ;;
	  4) echo "Illegal instruction"
		 ;;
	  5) echo "BPT trace/trap"
		 ;;
	  6) echo "ABORT instruction"
		 ;;
	  7) echo "Bus error"
		 ;;
	  8) echo "Floating point exception"
		 ;;
	  9) echo "Killed"
		 ;;
	  10) echo "User signal 1"
		 ;;
	  11) echo "Segmentation fault"
		 ;;
	  12) echo "User signal 2"
		 ;;
	  13) echo "Broken pipe"
		 ;;
	  14) echo "Alarm clock"
		 ;;
	  15) echo "Terminated"
		 ;;
	  17) echo "Child death or stop"
		 ;;
	  18) echo "Continue"
		 ;;
	  19) echo "Stopped (signal)"
		 ;;
	  20) echo "Stopped"
		 ;;
	  21) echo "Stopped (tty input)"
		 ;;
	  22) echo "Stopped (tty output)"
		 ;;
	  23) echo "Urgent IO condition"
		 ;;
	  24) echo "CPU limit"
		 ;;
	  25) echo "File limit"
		 ;;
	  26) echo "Alarm (virtual)"
		 ;;
	  27) echo "Alarm (profile)"
		 ;;
	  28) echo "Window changed"
		 ;;
	  29) echo "I/O ready"
		 ;;
	  30) echo "power failure imminent"
		 ;;
	  31) echo "Bad system call"
		 ;;
	  *) echo "Unknown"
		 ;;
   esac
}

Shell Scripting bash kill trap

 

 

Comments