使用exec和写入屏幕的Bash脚本全局重定向
发布时间:2020-12-15 22:01:38 所属栏目:安全 来源:网络整理
导读:我有一个大的bash脚本,我想全局将所有stdout / stderr重定向到一个文件,除了一些进度指示器,如特定的回显消息.例如,请考虑以下脚本 #!/bin/bash# all stdout and stderr go to out.logexec out.logspecial_echo "starting"# many other commands go here# wh
我有一个大的bash脚本,我想全局将所有stdout / stderr重定向到一个文件,除了一些进度指示器,如特定的回显消息.例如,请考虑以下脚本
#!/bin/bash # all stdout and stderr go to out.log exec &> out.log special_echo "starting" # many other commands go here # which output both the stdout and stderr # also some special_echo commands in here,# for example echo "msg1" special_echo "middle" echo "msg2" >&2 special_echo "finished" 当它运行时,输出应该是 $./script starting middle finished $ 但是,out.log应该包含 msg1 msg2 如何实现special_echo功能?我已经尝试使用文件描述符并回显,但无法让它显示在屏幕上. 有没有办法实现这一点,而无需将重定向添加到每一行或执行类似于此answer的操作? 解决方法
是的,使用另一个文件描述符是要走的路:
#!/bin/bash exec 3>&1 special_echo () { echo "$@" >&3 } exec &> out.log special_echo "starting" echo "msg1" special_echo "middle" echo "msg2" >&2 special_echo "finished" (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 忽略时区Angularjs
- angularjs – 如何告诉gulp-connect打开index.html,无论URL
- 从Play 2 Scala控制器提供静态/公共/文件
- sass – 如何将TypeScript WebPack ts-loader与Angular2 @V
- Bootstrap+PHP实现多图上传
- 如何在scala项目中将play框架用作库
- angularjs – Ionic / Angular JS – 从Reverse Geolocatio
- 基于Bootstrap+jQuery.validate Form表单验证实践
- bash – 如何为shell中的for循环添加前导零?
- 用docker 打造前端持续集成开发环境