Creating a Shell Script
One of the things LLMs excel at is creating simple, self-contained scripts to help you automate repetitive tasks. For example, I needed a way to rebuild this guide's TOC every time I moved sections or chapters around. I started with ChatGPT but wasn't happy with the results, so I switched to Github Copilot since I was already editing and running the script through Visual Studio code. I use these kinds of shell scripts all the time and they are a pain to build from scratch. I'll walk you through my process so you can see how I break the problems up into smaller tasks the LLM can handle better. The end goal was a script that would do the following:
Scan all the markdown files in the project.
Pull out all the chapter names from the folder via Regex.
Add each of the folder's markdown files to the TOC with links.
Use each markdown file's title for the TOC.
Clean up the chapter names by converting from snake case to title case.
Ignore folders and files that are not part of the guide.
Now let's look at my prompts and how I responded to the LLM to get it to create the final script I used.
Listing Directory Contents
I started by asking for a basic script to list directory contents recursively:
"Can you create a script to list the directory contents recursively?"
In response, the LLM provided a basic script that traversed directories and listed markdown files:
Extracting Titles from Markdown Files
Next, I wanted to extract H1 titles from the markdown files:
"Can you extract the H1 title from each markdown file?"
The LLM added a function to extract the H1 title:
The list_structure
function was then updated to use these titles:
Clean Up Chapter Names
I wanted to clean up the chapter names by removing numbers and converting them to title case:
"Can you clean up the title names? Remove the number and then title case the chapter name."
The LLM provided functions to handle this:
These functions were integrated into the list_structure
function:
Converting Snake Case to Title Case
Next, I saw that it wasn't converting the chapter titles correctly. It was still including the underscores and not capitalizing the other words so I asked to convert the snake case of the chapter folder titles to title case:
"You need to convert the snake case of the chapter folder titles to title case so remove the _ and capitalize things correctly."
The LLM updated the clean_chapter_name
function handled this:
Using Regex to Clean Up Names
I realized there was a cleaner way to do this, so I asked it to optimize the regex to clean up the names more effectively:
"Can you use regex to clean up the names more effectively?"
The LLM updated the clean_chapter_name
function to use regex for more efficient cleaning:
Removing Hard-Coded Directory Exclusions
After seeing I could clean this up more, I asked to remove the hard-coded directories I was asking it to ignore, like .github, images, and any loose markdown files in the root of the directory like the README and SUMMARY files since I was already using regex for the chapter names:
"Remove the hard-coded directories since we're using regex for the chapter names now."
The LLM updated the list_structure
function to dynamically handle directories:
Writing Back To Summary Markdown File
Finally, after testing this a few times and double checking the terminal output was correct I asked to write the TOC back to the summary.md file:
"Can you add writting these changes back to the SUMMARY.md file in the root of the directory."
The LLM updated the script with the following:
The Final Result
This approach can be adapted for various documentation projects, ensuring that the TOC remains up-to-date and correctly formatted as content evolves. Once everything worked, I asked for one final revision to the code:
"Can you heavily comment this to explain what is going on in each block of code."
The final script was then commented and looked like this:
This is an example of how I would use an LLM to automate something I could normally write in a few hours in a few minutes. The most important part is always to have the LLM comment on the code. This way, you can understand what is going on under the hood, and to help you if you need to go in and modify it yourself manually.
You can even go one step further and have it integrate this into your CI system:
Now, create a GitHub action that will call this shell script when I check my code, and make sure the SUMMARY is correct.md file is always up to date in case I forget to update it manually.
The LLM instructed me to create a file named .github/workflows/update_summary.yml
in your repository with the following content:
The response also included an explanation of what this does and how it works:
Trigger on Push: The workflow is triggered on every push to the
main
branch. You can change this to match your default branch.Checkout Repository: Uses the
actions/checkout
action to check out the repository.Set Up Git: Configures Git with a username and email for the commit.
Run Shell Script: Makes the
generate_summary.sh
script executable and runs it.Commit and Push Changes: Adds the updated
SUMMARY.md
file, commits the changes, and pushes them back to the repository. TheGITHUB_TOKEN
is used to authenticate the push.
At this point, I now have a fully automated workflow that ensures every time I push code to my repository, the generate_summary.sh
script is executed, and any changes to the SUMMARY.md
file are committed and pushed automatically.
Last updated