I have a script called test.sh
which processes the standard input line by line like this:
#!/usr/bin/env bash
echo "start"
while IFS= read -r line; do
echo "processing[$line]"
done < /dev/stdin
echo "done"
The problem with this is, it doesn't process the characters between the last newline and the eof.
printf $'line 1\nline 2\nlast chars' | test.sh
will output
start
processing[line 1]
processing[line 2]
done
The reason I read line by line is that I need to inspect the first line and in some cases I want to remove it from the output stream.
How can I process these last characters? I've looked into read -n
but then I would need to supply how many characters to expect at a maximum and I rather don't build in limits.
Also: I wouldn't know where to put this statement in the while-loop. I'm on the macOS platform.