TL; DR
所有作业控制/崩溃消息在函数内发生时都会被隐藏.我将在下面详细介绍,但@Barmar指出可以通过在函数内部运行崩溃二进制文件来重现此问题,例如:
crun() {
/tmp/faulty $1 $2 $3
}
我在我的.zshrc中定义了一个函数来编译和放大使用以下函数运行源代码:
crun() {
local file=$1
shift
local exepath="$(mktemp)"
if [[ $file =~ "\.c$" ]]; then
gcc -g -Wall $file -o $exepath || return $?
else
echo "no filetype detected"
return 126
fi
$exepath "$@"
}
可以这种方式调用:
% crun source.cc arg_1 arg_2
这适用于普通程序,但是存在shell的作业控制消息(例如从段错误生成的消息)不会出现的问题.
举个例子:
% echo 'int main=0;' >> /tmp/faulty.c # a crashing c program
% crun faulty.c
% # no output generated
而等效的交互式命令会产生这样的:
% g++ faulty.c -o /tmp/faulty && /tmp/faulty
[1] 2894 segmentation fault (core dumped) # 🢀 zsh's job control output for SIGSEGV
有没有办法为动态计算路径的崩溃可执行文件显示这些消息?理想情况下,无需编写自己的陷阱/信号处理程序exec,使用sh -c“$exepath $@”,或完全编写一个全新的系统(3)包装器)