linux – tee命令无法正常工作(带有read和echo)

linux – tee命令无法正常工作(带有read和echo),第1张

概述脚本和输出如下: 脚本: #!/bin/bash#tee_with_read.shfunction tee_test(){ echo "***This should be printed first but it is not***" read -r -p "Enter input : " echo "You entered : $REPLY"}tee_test 脚本和输出如下:

脚本:

#!/bin/bash#tee_with_read.shfunction 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.shEnter input : ***This should be printed first,but it is not***"My input"You entered : "My input"

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

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

解决方法 read -p将它的输出写入stderr,echo正在写入stdout. stdout通常是缓冲的,而stderr则没有,因此在stdout之前看到stderr事件的情况并不少见.

如果你喜欢做回声“字符串”>& 2你可以让你的回声也转到stderr,或者你可以在unbuffer命令或类似的工具中运行它,如果你有它们可用的话

总结

以上是内存溢出为你收集整理的linux – tee命令无法正常工作(带有read和echo)全部内容,希望文章能够帮你解决linux – tee命令无法正常工作(带有read和echo)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1049440.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存