Creating a CLI with Shell Scripting
When tasked to automate anything within Linux, you might find that Shell is the easiest tool to get the job done fast. Most of the time these shell scripts are built for a specific purpose and don’t have any need for configuration in different usage scenarios. However, it is possible to build a fully fleshed Command Line Interface (CLI) using a shell script, which I will outline for you the steps on how to do this.
Outline for the script
1 |
|
Let’s go through this one section at a time.
Defining functions
First is defining functions for the script. Functions help to separate logic into reusable chunks, and they also help a lot to make the code more readable.
The first function I defined is show_help_and_exit()
. This function simply displays the help menu using echo
and then exits the script using exit
.
1 | show_help_and_exit() { |
The second function I defined is echo_args()
. This function takes in a list of args as input and displays them in a list as output.
1 | echo_args() { |
To call a function in Shell, you just type the function name with a list of args, just as if you are using a terminal command. For example:
1 | echo_args this is a list of args |
This would simply output to the terminal
1 | this |
Processing input arguments
Let’s break this into chunks that can be easily explained.
First, when there is no input we want to display the help menu. For this we can use the show_help_and_exit()
function we created before.
1 | if [ -z "$1" ]; then |
This if statement is checking if there is no arguments using -z
and testing against the first argument using "$1"
. If there are no arguments, then it calls the show_help_and_exit()
function. This function will then display the help menu and then exit the script.
Second, we need to loop through the arguments and capture information about which arguments are available.
1 | while [ -n "$1" ] && [ -z "$DONE_PROCESSING_INPUT" ]; do |
In this while loop we are continuing to loop until either the first argument is not valid, or until the variable DONE_PROCESSING_INPUT
is defined. We then enter a case statement that takes in the first argument and then checks its value. The syntax -h|--help)
will check if the first argument is either -h
or --help
and then execute the code within that case until the line with ;;
.
Another For each argument and option we are setting an environment variable so later in the code we can easily check if that option or argument is set. We are also echoing debug statements if the DEBUG option is set, and setting this debug option using the -d
or --debug
option.
The last case *)
is a catch-all and this is where we process invalid arguments. I simply echo to stderr helpful information to help the user debug this error.
This is the output when running this script with an invalid argument.
1 | > ./my_script.sh not_a_valid_argument |
Executing commands
After processing input we can then execute the commands based on the environment variables we set earlier.
1 | # process opt1 |
As you can see here we are displaying debug output if -d
or --debug
is set, and also echoing that the options and commands are set.
We are also calling the echo_args
function and passing in the contents of the array CMD1_ARGS
that we set earlier in the code when processing cmd1
input and where we set its value to the rest of the arguments like so CMD1_ARGS=( "$@" )
. The syntax echo_args ${CMD1_ARGS[@]}
allows us to pass in the entire array as input to the echo_args
function.
Running the script
Now is time to run the script!
1 | > ./template_shell_script.sh -d -o cmd1 this is a list of arguments |
This template is a good base to go off of when developing your own simple CLI within a Shell script. Good luck on all your projects!
Comments
<code><pre>insert.code.here()<pre/><code/>