bash - 来自 tee 命令的意外或空输出

标签 bash centos

这个问题在这里已经有了答案:





Why does reading and writing to the same file in a pipeline produce unreliable results?

(2 个回答)


3年前关闭。




echo "hello" | tee test.txt
cat test.txt
sudo sed -e "s|abc|def|g" test.txt | tee test.txt
cat test.txt

输出:
第二个命令和最后一个命令的输出不同,其中命令相同。

问题:
上面脚本中的以下行给出了一个输出,但为什么它没有被重定向到输出文件?

sudo sed -e "s|abc|def|g" test.txt

最佳答案

sudo sed -e "s|abc|def|g" test.txt | tee test.txt

读写test.txt在同一命令行中容易出错。 sed正试图同时从文件中读取 tee想要截断它并写入它。

您可以使用 sed -i就地修改文件。不需要tee . (也不需要 sudo 。您制作了文件,没有理由要求 root 访问权限来阅读它。)
sed -e "s|abc|def|g" -i test.txt

关于bash - 来自 tee 命令的意外或空输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56451413/

相关文章:

bash - 文件中行数的变量

Linux bash脚本: Recurisely delete files if empty based on file format from user input

linux - Debian : Cron bash script every 5 minutes and lftp

bash - 索引大型排序哈希文件的最快方法

linux - Firebird 数据库的安全性

linux - 关于 CentOS 或任何 Linux 机器中的可执行路径

bash - 在 awk 中分配一个 bash 关联数组

centos - 如何在 CentOS 上禁用 Nouveau 内核驱动程序

apache - Apache网络服务器中的KeepAliveTimeout 0

python - Linux进程崩溃后如何重新运行?