我更喜欢实验:
#!/bin/bash
# FILE /tmp/bla.sh
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
MY_ID=$1 # Identifier for messages
function ctrl_c() {
echo >&2 "GOODBYE $MY_ID"
exit
}
# This will continue until interrupted, e.g. if the input/output get closed
cat
# If we somehow got to the end
echo >&2 "grace $MY_ID"
Run Code Online (Sandbox Code Playgroud)
拴住他们,跑动并打破他们
nitz@mars:~$ /tmp/bla.sh 1 | /tmp/bla.sh 2
^CGOODBYE 2
GOODBYE 1
0
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,两次执行都收到了中断信号,这意味着它们都被杀死了。此外,他们输出被杀的顺序是随机的,例如:
nitz@mars:~$ /tmp/bla.sh 1 | /tmp/bla.sh 2 | /tmp/bla.sh 3 | /tmp/bla.sh 4
^CGOODBYE 2
GOODBYE 4
GOODBYE 1
GOODBYE 3
Run Code Online (Sandbox Code Playgroud)