我有一個bash代碼給了我隨機結果。 我有我的代碼剝離下來,看起來像一些僞代碼:使用陷阱時的Bash隨機函數行爲USR1
#!/usr/bin/bash
function TrapQuit {
echo "Quitting"
}
function SubFunction {
echo "Full function call tree ${FUNCNAME[@]}"
}
function DisPatch {
echo "Running function ${FUNCNAME[0]}"
SubFunction "1"
}
function test {
kill -USR1 $$
}
trap DisPatch USR1
trap TrapQuit EXIT HUP
test &
test &
test &
test &
test &
while true; do
sleep 1
done
基本上,$ {FUNCNAME [@]}應該列出所有功能,從目前功能最多的主要要求。 當我運行這段代碼時,我得到了不同的結果。
好結果是:
Running function DisPatch Full function call tree SubFunction DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch test main ^CRunning function DisPatch Full function call tree SubFunction DisPatch main Quitting
奇怪的結果是:
Running function DisPatch Full function call tree SubFunction DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch SubFunction DisPatch DisPatch main Running function DisPatch Full function call tree SubFunction DisPatch main ^CQuitting
每隔三年或五年的運行,我得到了奇怪的結果,其中調度功能是三倍在$ {FUNCNAME [@]}中。
DisFatch函數如何從SubFunction中的$ {FUNCNAME [@]}中多次結束,似乎DisPatch已經遞歸執行了它本身?
感謝您的任何見解。
CentOS的下7
GNU bash中,版本42年3月4日(1)-release測試與
GNU bash中,版本4.2.46(1)-release下(x86_64-紅帽-Linux的GNU) (x86_64-redhat-linux-gnu)下的Fedora 24
那你期望的輸出是在調用信號處理程序在運行現有的信號處理? –