mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-06-08 00:32:32 +02:00
Merge pull request #5 from Crestwave/eof
Add support for lines that end without a newline
This commit is contained in:
commit
460ccfdfd7
|
@ -393,10 +393,12 @@ Alternative to the `head` command.
|
|||
head() {
|
||||
# Usage: head "n" "file"
|
||||
while read -r line; do
|
||||
[ "$i" = "$1" ] && break
|
||||
printf '%s\n' "$line"
|
||||
i=$((i+1))
|
||||
[ "$i" = "$1" ] && return
|
||||
done < "$2"
|
||||
|
||||
[ -n "$line" ] && printf %s "$line"
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -420,7 +422,7 @@ Alternative to `wc -l`.
|
|||
```sh
|
||||
lines() {
|
||||
# Usage: lines "file"
|
||||
while read -r _; do
|
||||
while read -r line || [ -n "$line" ]; do
|
||||
lines=$((lines+1))
|
||||
done < "$1"
|
||||
|
||||
|
@ -568,7 +570,7 @@ done
|
|||
## Loop over the contents of a file
|
||||
|
||||
```shell
|
||||
while read -r line; do
|
||||
while read -r line || [ -n "$line" ]; do
|
||||
printf '%s\n' "$line"
|
||||
done < "file"
|
||||
```
|
||||
|
|
Loading…
Reference in a new issue