From 303a209161076312c15f9e3912dac6178fbc9114 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Tue, 24 Sep 2019 08:03:06 +0300 Subject: [PATCH] docs: update --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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"