mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-06-08 16:52:31 +02:00
Merge dced5f0d23
into 26bc7e8d6c
This commit is contained in:
commit
977e26ad14
35
README.md
35
README.md
|
@ -37,6 +37,7 @@ See something incorrectly described, buggy or outright wrong? Open an issue or s
|
||||||
* [Parsing a `key=val` file.](#parsing-a-keyval-file)
|
* [Parsing a `key=val` file.](#parsing-a-keyval-file)
|
||||||
* [Get the first N lines of a file](#get-the-first-n-lines-of-a-file)
|
* [Get the first N lines of a file](#get-the-first-n-lines-of-a-file)
|
||||||
* [Get the number of lines in a file](#get-the-number-of-lines-in-a-file)
|
* [Get the number of lines in a file](#get-the-number-of-lines-in-a-file)
|
||||||
|
* [Get the number of characters in a file](#get-the-number-of-characters-in-a-file)
|
||||||
* [Count files or directories in directory](#count-files-or-directories-in-directory)
|
* [Count files or directories in directory](#count-files-or-directories-in-directory)
|
||||||
* [Create an empty file](#create-an-empty-file)
|
* [Create an empty file](#create-an-empty-file)
|
||||||
* [FILE PATHS](#file-paths)
|
* [FILE PATHS](#file-paths)
|
||||||
|
@ -470,6 +471,40 @@ $ lines ~/.bashrc
|
||||||
48
|
48
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Get the number of characters in a file
|
||||||
|
|
||||||
|
Alternative to `wc -c`.
|
||||||
|
|
||||||
|
**Example Function:**
|
||||||
|
|
||||||
|
|
||||||
|
```sh
|
||||||
|
chars() {
|
||||||
|
# Usage: chars "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 IFS= read -r line && chars=$((chars+1)) || [ -n "$line" ]; do
|
||||||
|
# "${#line}": Number of characters in '$line'.
|
||||||
|
chars=$((chars+${#line}))
|
||||||
|
done < "$1"
|
||||||
|
|
||||||
|
printf '%s\n' "$chars"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Example Usage:**
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ chars ~/.bashrc
|
||||||
|
3526
|
||||||
|
```
|
||||||
|
|
||||||
## Count files or directories in directory
|
## Count files or directories in directory
|
||||||
|
|
||||||
This works by passing the output of the glob to the function and then counting the number of arguments.
|
This works by passing the output of the glob to the function and then counting the number of arguments.
|
||||||
|
|
Loading…
Reference in a new issue