脚本和输出如下:
脚本:
#!/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.
由于此版本中没有命名管道,因此我无法使用命名管道进行日志记录.
@H_502_16@
read -p将它的输出写入stderr,echo正在写入stdout. stdout通常是缓冲的,而stderr则没有,因此在stdout之前看到stderr事件的情况并不少见.
如果你喜欢做回声“字符串”>& 2你可以让你的回声也转到stderr,或者你可以在unbuffer命令或类似的工具中运行它,如果你有它们可用的话
@H_502_16@
@H_502_16@
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。