Database Reference
In-Depth Information
Step 4: Remove Fixed Input
We now have a valid command-line tool that we can execute from the command line.
But we can do better than this. We can make our command-line tool more reusable.
The first command in our file is
curl
, which downloads the text from which we wish
to obtain the top 10 most-used words. So the data and operations are combined
into one.
But what if we wanted to obtain the top 10 most-used words from another ebook, or
any other text for that matter? The input data is fixed within the tool itself. It would
be better to separate the data from the command-line tool.
If we assume that the user of the command-line tool will provide the text, it will
become more generally applicable. So, the solution is to simply remove the
curl
com‐
mand from the script. See
Example 4-3
for the updated script, named
top-words-4.sh
.
Example 4-3. ~/book/ch04/top-words-4.sh
#!/usr/bin/env bash
tr
'[:upper:]'
'[:lower:]'
| grep -oE
'\w+'
| sort |
uniq -c | sort -nr | head -n 10
This works because if a script starts with a command that needs data from standard
input, like
tr
, it will take the input that is given to the command-line tools. Assuming
that we have saved the ebook to
data/inn.txt
, we could do, for example:
$
cat data/ | ./top-words-4.sh
Although we haven't done so in our script, the same principle
holds for saving data. It is, in general, better to let the user take care
of that. Of course, if you intend to use a command-line tool only
for your own projects, then there are no limits to how specific you
can be.
Step 5: Parameterize
There is one more step that we can perform in order to make our command-line tool
even more reusable: parameters. In our command-line tool, there are a number of
fixed command-line arguments—for example,
-nr
for
sort
and
-n 10
for
head
. It is
probably best to keep the former argument fixed. However, it would be very useful to
allow for different values for the
head
command. This would allow the end user to set
the number of most-often used words to be outputted.
Example 4-4
shows what our
file
top-words-5.sh
looks like if we parameterize
head
.