Bash
從函式內部執行郵件命令會導致“分叉炸彈”
當我嘗試
#!/bin/bash mail() { echo "Free of oxens" | mail -s "Do you want to play chicken with the void?" "example@example.org" } mail exit 0
有時你可以直接殺死命令,它會殺死子程序,但有時你必須
killall -9
.它不關心郵件是否發送。叉形炸彈是由任何一種方式產生的。而且它似乎沒有為退出程式碼添加任何檢查,例如
if ! [ "$?" = 0 ]
,幫助。但是下面的腳本按預期工作,它要麼輸出錯誤,要麼發送郵件。
#!/bin/bash echo "Free of oxens" | mail -s "Do you want to play chicken with the void?" "example@example.org" exit 0
為什麼會這樣?您將如何檢查郵件命令的退出程式碼?
您正在從同一函式中呼叫該函式*:*
#!/bin/bash mail() { # This actually calls the "mail" function # and not the "mail" executable echo "Free of oxens" | mail -s "Do you want to play chicken with the void?" "example@example.org" } mail exit 0
這應該有效:
#!/bin/bash mailfunc() { echo "Free of oxens" | mail -s "Do you want to play chicken with the void?" "example@example.org" } mailfunc exit 0
請注意,不再從函式本身呼叫函式名稱。