The script we completed in the last exercise could have been a one-line terminal command run inside the desired working directory:
ls -R | grep ':$' | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
But this is a lot to type, and it’s easier to edit and reuse a script. With a script, we can also save input arguments and provide additional options to the user. Then, we can share the script with anyone! The
tree.sh script that we recreated in the last two exercises was originally written by Dem Pilafian.
Such powerful open-source scripts are available all over the internet and we should absolutely take advantage of them. We can use scripts inside our scripts as well! For example, a more feature-packed version of the tree script is available by Steve Baker at Indiana State University. We can install this script via this simple command in our own terminal:
apt-get install tree
Baker’s version features many options and better formatting thanks to several developers who contributed to it. Note the documentation as well!
Some developers have made collections of several handy scripts. One collection, called Bash-Snippets, provides many helpful utilities for the command line such as looking up the weather, converting currency, performing advanced numerical calculations, and even creating memes We can reuse these scripts in our own scripts!
In the next exercise, we will be using one of the scripts from the Bash-Snippets package to create a song-guessing game script.
We downloaded the script
weather.sh from Bash-Snippets. Check its usage with the command
./weather.sh -h. The
-h option is programmed into the script.
Get the weather of
Tokyo using the weather script
If the output looks strange, try maximizing the terminal window or adjust your screen size.