bash 有没有办法从 stdin 读取一行并直接写入文件?就像是:
t="$(mktemp)"
while true
do
[read single line from stdin] > "${t}"
[nothing to read] && break
printf '%s\n' "$t"
t="$(mktemp)"
done
我的可用内存有限,预计线路可能会长得离谱。否则我只会做一个 while-read 循环。
编辑:
我需要做一些提取和过滤。更完整的伪代码片段将类似于:
f () {
t="$(mktemp)"
while true
do
[read single line from stdin] > "${t}"
[nothing to read] && break
printf '%s\n' "$t"
t="$(mktemp)"
done | while read -r file
do
if cat "$file" | grep -q '[criteria]'
then
cat "$file" | jq '[filter1]'
cat "$file" | jq '[filter2]' >> [seprate file]
[etc]
else
printf '%s\n' "There was a mismatch" > /dev/stderr
fi
rm "$file"
done
}