[count] Allow counting lines from stdin

As a simple replacement for `wc -l`.

This counts both lines on stdin _and_ arguments.

So if "file" has three lines, then `count a b c < file` will print 6.

And since it counts newlines, like wc, `echo -n foo | count` prints 0.
This commit is contained in:
Fabian Homborg
2018-01-04 00:42:12 +01:00
parent cb36a9ca36
commit e7a964fdfa
5 changed files with 55 additions and 4 deletions

View File

@@ -5,17 +5,20 @@ Synopsis
--------
count $VARIABLE
COMMAND | count
count < FILE
Description
-----------
``count`` prints the number of arguments that were passed to it. This is usually used to find out how many elements an environment variable array contains.
``count`` prints the number of arguments that were passed to it, plus the number of newlines passed to it via stdin. This is usually used to find out how many elements an environment variable array contains, or how many lines there are in a text file.
``count`` does not accept any options, not even ``-h`` or ``--help``.
``count`` exits with a non-zero exit status if no arguments were passed to it, and with zero if at least one argument was passed.
Note that, like ``wc -l``, reading from stdin counts newlines, so ``echo -n foo | count`` will print 0.
Example
-------
@@ -30,3 +33,11 @@ Example
count *.txt
# Returns the number of files in the current working directory ending with the suffix '.txt'.
git ls-files --others --exclude-standard | count
# Returns the number of untracked files in a git repository
printf '%s\n' foo bar | count baz
# Returns 3 (2 lines from stdin plus 1 argument)
count < /etc/hosts
# Counts the number of entries in the hosts file