aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDylan Araps <dylan.araps@gmail.com>2019-09-24 08:03:06 +0300
committerDylan Araps <dylan.araps@gmail.com>2019-09-24 08:03:06 +0300
commit303a209161076312c15f9e3912dac6178fbc9114 (patch)
tree60f821a362f4232268783737c197f27f43cadecc
parent5e6fd1e9265ae9d3dfb5b18cf3bc24983b688f06 (diff)
downloadpure-sh-bible-303a209161076312c15f9e3912dac6178fbc9114.tar.gz
pure-sh-bible-303a209161076312c15f9e3912dac6178fbc9114.zip
docs: update
-rw-r--r--README.md18
1 files changed, 18 insertions, 0 deletions
diff --git a/README.md b/README.md
index 698a995..1d32f85 100644
--- a/README.md
+++ b/README.md
@@ -416,6 +416,16 @@ head() {
[ "$i" = "$1" ] && return
done < "$2"
+ # 'read' used in a loop will skip over
+ # the last line of a file if it does not contain
+ # a newline and instead contains EOF.
+ #
+ # The final line iteration is skipped as 'read'
+ # exits with '1' when it hits EOF. 'read' however,
+ # still populates the variable.
+ #
+ # This ensures that the final line is always printed
+ # if applicable.
[ -n "$line" ] && printf %s "$line"
}
```
@@ -440,6 +450,14 @@ Alternative to `wc -l`.
```sh
lines() {
# Usage: lines "file"
+
+ # '|| [ -n "$line" ]': This ensures that lines
+ # ending with EOL instead of a newline are still
+ # operated on in the loop.
+ #
+ # 'read' exits with '1' when it sees EOL and
+ # without the added test, the line isn't sent
+ # to the loop.
while read -r line || [ -n "$line" ]; do
lines=$((lines+1))
done < "$1"