The Script Overview
The script provided allows for easy creation and deletion of blog posts with Hugo, a popular static site generator. By passing different flags and the title of your blog post as arguments, you can quickly manage your content without directly interacting with the file system or Hugo commands.
Usage
To use this script, you would typically invoke it from the command line with the appropriate flags and arguments. For example, to create a post titled “My First Blog Post,” you would run:
|
|
And to delete a post with the same title, you would use:
|
|
The Script
|
|
file header #!/usr/bin/env bash
The script starts with a shebang line #!/usr/bin/env bash
that specifies the interpreter to use for executing the script. This line is essential for running the script as an executable file. (happy running in zsh)
Understanding while test $# -gt 0;
The script begins with a while
loop that continues as long as there are arguments passed to the script ($#
is greater than 0). $#
is a special parameter in shell scripts that represents the number of arguments passed to the script. This loop allows the script to process multiple arguments sequentially, making it versatile and robust for different use cases.
shift
and Argument Processing
Within the loop, we use the shift
command to shift the arguments passed to the script. This command moves the positional parameters ($1
, $2
, etc.) to the left, effectively removing the first argument. This is crucial for processing different flags and arguments in the script.
How case
Works
Inside the while
loop, we encounter a case
statement. This statement is used for pattern matching. It takes the first argument passed to the script ($1
), compares it against several patterns (-h
, --help
, -c
, --create
, -d
, --delete
), and executes the corresponding block of code based on the match.
- Help (
-h
,--help
): Displays usage information and exits. This is crucial for any script, providing users with guidance on how to use the tool. - Create (
-c
,--create
): Shifts to the next argument (the title of the post) and uses Hugo’snew
command to create a new post. If no title is provided, it exits with an error. - Delete (
-d
,--delete
): Similar to create, but instead deletes the specified post directory usingrm -r
.
The Conditional if [[ -n $1 ]]; then
Within both the create and delete blocks, there’s a conditional statement if [[ -n $1 ]]; then
that checks if the first argument (after shifting, which would be the title of the post or the post directory) is non-empty. -n $1
returns true if $1
has a non-zero length. This check ensures that the script doesn’t attempt to create or delete a post without a specified title or directory, which could lead to errors or unintended behavior.