tee命令无法按预期工作(带有读取和回显)

时间:2016-05-02 16:32:20

标签: linux bash unix

脚本和输出如下:

脚本:

#!/bin/bash
#tee_with_read.sh
function tee_test()
{
    echo "***This should be printed first but it is not***"
    read -r -p "Enter input : "
    echo "You entered : $REPLY"
}
tee_test | tee -a logfile

输出:

$ ./tee_with_read.sh
Enter input : ***This should be printed first, but it is not***
"My Input"
You entered : "My Input"

我正在尝试将输出附加到logfile。 但是正如你在输出中所看到的那样,似乎第一次读取被剔除,然后是回声,这不是预期的。

我在Windows 10上使用Git Bash版本3.1.23。 由于此版本中没有命名管道,因此我无法使用命名管道进行日志记录。

1 个答案:

答案 0 :(得分:9)

read -p正在将其输出写入stderr,而echo正在写入stdoutstdout通常是缓存的,而stderr则不会,因此在stderr之前显示stdout内容的情况并不罕见。

如果您愿意echo,您可以让stderr转到echo "string" >&2,或者如果您有unbuffer命令,可以在window.location.href='...命令或类似工具中运行decline_id可用

相关问题