Added lines_loop
This commit is contained in:
23
README.md
23
README.md
@@ -755,9 +755,7 @@ $ tail 1 ~/.bashrc
|
|||||||
|
|
||||||
Alternative to `wc -l`.
|
Alternative to `wc -l`.
|
||||||
|
|
||||||
**CAVEAT:** Requires `bash` 4+
|
**Example Function (bash 4):**
|
||||||
|
|
||||||
**Example Function:**
|
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
lines() {
|
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:**
|
**Example Usage:**
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ lines ~/.bashrc
|
$ lines ~/.bashrc
|
||||||
48
|
48
|
||||||
|
|
||||||
|
$ lines_loop ~/.bashrc
|
||||||
|
48
|
||||||
```
|
```
|
||||||
|
|
||||||
## Count files or directories in directory.
|
## Count files or directories in directory.
|
||||||
|
|||||||
7
test.sh
7
test.sh
@@ -91,6 +91,13 @@ test_lines() {
|
|||||||
rm test_file
|
rm test_file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test_lines_loop() {
|
||||||
|
printf '\n\n\n\n\n\n\n\n' > test_file
|
||||||
|
result="$(lines_loop test_file)"
|
||||||
|
assert_equals "$result" "8"
|
||||||
|
rm test_file
|
||||||
|
}
|
||||||
|
|
||||||
test_count() {
|
test_count() {
|
||||||
result="$(count ./{README.m,LICENSE.m,.travis.ym}*)"
|
result="$(count ./{README.m,LICENSE.m,.travis.ym}*)"
|
||||||
assert_equals "$result" "3"
|
assert_equals "$result" "3"
|
||||||
|
|||||||
Reference in New Issue
Block a user