rox*_*xto 8 bash shell-script mail-command
当我尝试mail从 bash 脚本中的函数内部执行时,它会创建类似于 fork 炸弹的东西。为了澄清,这会产生问题:
#!/bin/bash
mail() {
echo "Free of oxens" | mail -s "Do you want to play chicken with the void?" "example@example.org"
}
mail
exit 0
Run Code Online (Sandbox Code Playgroud)
有时您可以终止命令,它会终止子进程,但有时您必须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
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况?您将如何检查邮件命令的退出代码?
And*_*nle 29
你调用该函数 mail从同一个函数中:
#!/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
Run Code Online (Sandbox Code Playgroud)
这应该有效:
#!/bin/bash
mailfunc() {
echo "Free of oxens" | mail -s "Do you want to play chicken with the void?" "example@example.org"
}
mailfunc
exit 0
Run Code Online (Sandbox Code Playgroud)
请注意,不再从函数本身内部调用函数名称。
mik*_*erv 15
除此以外:
mail(){
echo olly olly oxenfree | command mail -s 'and the rest' and@more
}
Run Code Online (Sandbox Code Playgroud)
...应该可以正常工作。