运行使用exec和tee来将stdout输出发送到终端和文件的脚本后,shell提示符似乎不再出现 [英] shell prompt seemingly does not reappear after running a script that uses exec with tee to send stdout output to both the terminal and a file

查看:82
本文介绍了运行使用exec和tee来将stdout输出发送到终端和文件的脚本后,shell提示符似乎不再出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个shell脚本,该脚本将所有输出写入日志文件和终端,这部分工作正常,但是如果我执行脚本仅当我按Enter时才会出现一个新的shell提示.为什么会这样,我该如何解决?

I have a shell script which writes all output to logfile and terminal, this part works fine, but if I execute the script a new shell prompt only appear if I press enter. Why is that and how do I fix it?

#!/bin/bash

exec > >(tee logfile)
echo "output"

推荐答案

首先,当我对此进行测试时,总是一个新的shell提示,只是有时字符串输出紧随其后,因此提示不是最后一个.您碰巧忽略了它吗?如果是这样的话,似乎是一个竞赛,shell在后台完成 tee 之前打印提示.

First, when I'm testing this, there always is a new shell prompt, it's just that sometimes the string output comes after it, so the prompt isn't last. Did you happen to overlook it? If so, there seems to be a race where the shell prints the prompt before the tee in the background completes.

不幸的是,无法通过在外壳中为 tee 进行 wait 来解决此问题,请参见

Unfortunately, that cannot fixed by waiting in the shell for tee, see this question on unix.stackexchange. Fragile workarounds aside, the easiest way to solve this that I see is to put your whole script inside a list:

{
your-code-here
} | tee logfile

这篇关于运行使用exec和tee来将stdout输出发送到终端和文件的脚本后,shell提示符似乎不再出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆