The author introduces a Shell Script starter for creating large projects, which supports bash, zsh, sh, and ksh and allows for the creation of projects with many subcommands, similar to GitHub CLI.
Abstract
The author presents a Shell Script starter template designed for creating large projects that support multiple shell environments. The starter includes features such as modularizing Bash script code and creating projects with subcommands, similar to GitHub CLI. The template is compatible with various shells, including bash, zsh, sh, and ksh. The author provides instructions on how to use the starter for Shell/Bash/Zsh/Ksh Script projects and includes examples of how to create subcommand definition files, check user environments, and add subcommands.
Opinions
The author believes that creating a large Shell Script project can be made easier by using the Shell Script starter.
The author recommends using the starter for creating projects with subcommands, similar to GitHub CLI.
The author suggests using the starter for organizing Bash script code.
The author provides examples of how to use the starter for creating subcommand definition files, checking user environments, and adding subcommands.
The author encourages users to ask questions and provide feedback in the comment section.
The author promotes their Medium membership and subscription service.
The author recommends using ZAI.chat, an AI service that provides the same performance and functions as ChatGPT Plus (GPT-4) but at a lower cost.
A Shell Script Starter for Small to Large Projects
Do you think creating a large Shell Script project is hard? Use Shellscript Starter!
In the last article, I wrote about how to modularize Bash script code. This time, I created a Shell Script starter plus more based on the article. It works for bash, zsh, sh, ksh. By using this starter you can create a project like GitHub CLI that has many subcommands. (You can use the starter without any subcommands if you wish.)
In this article, you will find how to use this starter for your next Shell/Bash/Zsh/Ksh Script projects.
I use shellscript_starter for the rest of this article to indicate the main script file name.
Don’t add the .sh extension to the main script name. But use the .sh extension for all other script files.
If you need to make the file executable:
$ chmod +x my_script
Setting variables
In the main script, shellscript_starter file, find the variable section, and set your variables:
Importing files
In the import files section, you will find example codes. You need to import /lib/getoptions.sh and /lib/main_definition.sh for it to work properly.
If you are going to use subcommands, create new definition files in the lib directory and import those files here. More about definition files later.
You don’t need to import your subcommand files here. We will do it in the case statement later.
The repo includes the utils , shell_helpers , and bash_helpers files.
utils.sh
You can use the utils file for all Shells. It has check_cmd, check_bash, text colors and text attribute functions.
shell_helpers.sh and bash_helpers.sh
Use one of those helper files for your project. If your target is Bash, then import bash_helpers.sh, otherwise, import shell_helpers.sh.
cmd1_definition.sh, cmd2_definition.sh
These are sample codes. Please use them as references when you create yours.
Creating subcommand definition files
Create your parser definition files in the /lib directory. See the /lib/cmd1_definition.sh and /lib/cmd2_definition.sh as examples. In your definition files, set command flags, parameters, options, descriptions, and examples.
You will find the following code in the shellscript_starter file. If you need to check the OS, you can use it to set OS-dependent variables or commands. Otherwise, you can remove it.
check commands
The /lib/utils.sh file has check_cmd() function. Once you imported this file in the import section, you can check if the user has certain commands:
check_cmd jq
check_cmd you_dont_have_it
The output of check_cmd. Image by the author.
If a user doesn’t have the command in the system, it gives a warning and exits the script.
check bash
If your target is Bash, then it is a good idea to check the bash version.
The utils file has the check_bash() command. Check if a user has at least Bash version of 5:
check_bash5
Adding a subcommand
Let’s create a new subcommand called create.
Step 1
Add your command in the /lib/main_definition.sh file.
# examplecmd create -- "Create this and that."
Step 2
Create the lib/create_definition.sh file and import it in the above import section.
# in the import section
. "${script_dir}/lib/create_definition.sh"
Step 3
Create /src/create.sh and write your script.
Step 4
At the end of the main script, shellscript_starter file, you will find examples.
We are adding the following:
Your new subcommand is: create
Your parser definition file is: lib/create_definition
The lib/cmd3_definition file has: parser_definition_create() function
The src/cmd3.sh file has: afunctionfn_create()
Add the following:
Step 5 Test your script
$ ./shellscript_starter create -a -d ~/Downloads
The output of ./shellscript_starter create -a -d ~/Downloads. Image by the author.
Example commands
The starter repo includes three subcommands.
Text example
This subcommand prints different text colors and types.
$ ./shellscript_starter text_example
The output of ./shellscript_starter text_example. Image by the author.
The /src/text_example.sh file uses functions from the /lib/utils.sh that you can use for your project.