How to extract the signal name with sed
/usr/bin/kill -L
prints:
1 HUP 2 INT 3 QUIT 4 ILL 5 TRAP 6 ABRT 6 IOT 7 BUS 8 FPE 9 KILL 10 USR1
11 SEGV 12 USR2 13 PIPE 14 ALRM 15 TERM 16 STKFLT 17 CHLD 17 CLD 18 CONT 19 STOP 20 TSTP
21 TTIN 22 TTOU 23 URG 24 XCPU 25 XFSZ 26 VTALRM 27 PROF 28 WINCH 29 IO 29 POLL 30 PWR
31 SYS 34 RTMIN 64 RTMAX
Now I want to be able to print "11 SEGV", "12 USR2" or "SEGV", "USR2" (both solutions will be fine) by the number of a signal – with sed.
I did it with /usr/bin/kill -L|sed -n "s/(^| )11 ([^ ]+)+/<<2>>/;s/.*<<([^>]+)>>.*/1/p"
– it outputs "SEGV" for 11.
But the question is – is it possible to do it simpler, without the second substitution?
You could run
/usr/bin/kill -L | grep -o '[0-9]+ [A-Z0-9]+'
to separate the signals with newline and then easily grep
with grep -w "^$NUM"
.
Or more easy use kill -l "$NUM"
😉
$ kill -l 11
SEGV