Added lines_loop

This commit is contained in:
Dylan Araps
2018-06-16 09:09:56 +10:00
parent 679e787bd2
commit 7e79899977
2 changed files with 27 additions and 3 deletions

View File

@@ -755,9 +755,7 @@ $ tail 1 ~/.bashrc
Alternative to `wc -l`.
**CAVEAT:** Requires `bash` 4+
**Example Function:**
**Example Function (bash 4):**
```sh
lines() {
@@ -767,11 +765,30 @@ lines() {
}
```
**Example Function (bash 3):**
This method uses less memory than the `mapfile` method and it's more
compatible but it's slower for bigger files.
```sh
lines_loop() {
# Usage lines_loop "file"
count=0
while IFS= read -r _; do
((count++))
done < "$1"
printf '%s\n' "$count"
}
```
**Example Usage:**
```shell
$ lines ~/.bashrc
48
$ lines_loop ~/.bashrc
48
```
## Count files or directories in directory.