如何打印到标准输出从EVAL期间调用陷阱中

问题描述 投票:1回答:1

我希望下面的脚本打印This is redirected to 'output'.当我按Ctrl + C:

#!/bin/bash

trap_function(){
    trap '' EXIT INT TERM
    echo "This is redirected to 'output'."
    touch this_will_exist
}
trap trap_function EXIT INT TERM

eval "sleep 100" &> output

相反,不显示任何内容,文本转到文件output。如何从trap_function内逃脱重定向,并有显示给用户的文本?

echo "This is redirected to 'output'." > /dev/stdout没有收到预期的效果。

我在Ubuntu 16.04.5 LTS运行GNU的bash,版本48年3月4日。

bash eval stdout trap
1个回答
0
投票

一种解决方法是让eval在一个子shell运行:

#!/bin/bash

trap_function(){
    trap '' EXIT INT TERM
    echo "This is redirected to 'output'."
    touch this_will_exist
}
trap trap_function EXIT INT TERM

(eval "sleep 100") &> output
© www.soinside.com 2019 - 2024. All rights reserved.