mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-05-17 21:06:36 +02:00
Add support for lines that end without a newline
This commit is contained in:
parent
bef1e1dc25
commit
172056a6c7
|
@ -393,10 +393,11 @@ Alternative to the `head` command.
|
||||||
head() {
|
head() {
|
||||||
# Usage: head "n" "file"
|
# Usage: head "n" "file"
|
||||||
while read -r line; do
|
while read -r line; do
|
||||||
[ "$i" = "$1" ] && break
|
|
||||||
printf '%s\n' "$line"
|
printf '%s\n' "$line"
|
||||||
i=$((i+1))
|
i=$((i+1))
|
||||||
|
[ "$i" = "$1" ] && return
|
||||||
done < "$2"
|
done < "$2"
|
||||||
|
[ -n "$line" ] && printf %s "$line"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -420,7 +421,7 @@ Alternative to `wc -l`.
|
||||||
```sh
|
```sh
|
||||||
lines() {
|
lines() {
|
||||||
# Usage: lines "file"
|
# Usage: lines "file"
|
||||||
while read -r _; do
|
while read -r line || [ -n "$line" ]; do
|
||||||
lines=$((lines+1))
|
lines=$((lines+1))
|
||||||
done < "$1"
|
done < "$1"
|
||||||
|
|
||||||
|
@ -568,7 +569,7 @@ done
|
||||||
## Loop over the contents of a file
|
## Loop over the contents of a file
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
while read -r line; do
|
while read -r line || [ -n "$line" ]; do
|
||||||
printf '%s\n' "$line"
|
printf '%s\n' "$line"
|
||||||
done < "file"
|
done < "file"
|
||||||
```
|
```
|
||||||
|
|
Loading…
Reference in a new issue