我希望下面的脚本打印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日。
一种解决方法是让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