Shell 管道和重定向结果
考虑2个json文件:Shell 管道和重定向结果,shell,command-line,pipe,io-redirection,Shell,Command Line,Pipe,Io Redirection,考虑2个json文件: fileA.json: { "foo": "hey", "bar": "ola" } { "foo": "hoy" } fileB.json: { "foo": "hey", "bar": "ola" } { "foo": "hoy" } ,执行: % cat fileA.json fileB.json | json 返回 { "foo": "hoy", "bar": "ola" } 嗯 -- 现在为什么将stdout重定向到f
fileA.json
:
{
"foo": "hey",
"bar": "ola"
}
{
"foo": "hoy"
}
fileB.json
:
{
"foo": "hey",
"bar": "ola"
}
{
"foo": "hoy"
}
,执行:
% cat fileA.json fileB.json | json
返回
{
"foo": "hoy",
"bar": "ola"
}
嗯
--
现在为什么将stdout重定向到fileB.json
我得到:
% cat fileA.json fileB.json | json > fileB.json
我得到:
{
"foo": "hey",
"bar": "ola"
}
Ie:fileA.json
PS:
json
实用程序在这里:shell设置输出重定向,>fileB.json
,因此它在cat
开始读取之前打开并截断fileB.json
。这会导致cat
读取一个空文件。(它甚至可能最终读取部分写入的输出数据。)
切勿在管道中读取和写入同一文件。请尝试类似于>fileC.json
的方法