这个问题在这里已经有了答案:
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/