An Alias to Create Shell Scripts
Most shell scripts I create start the same, this saves a bit of time from having to create, chmod the file and set a few options.
Quick Jump: Demo Video
I recently added a new
mkscript alias to my dotfiles.
You can run
mkscript demo or
mkscript some/deeply/nested/path/myscript and it’ll create a new script for you as long as the file doesn’t exist already. It also makes the script executable and sets a few
set -o options.
It’s a fairly small alias but size doesn’t matter. A core principle here is that you can build tools and mold your dev environment to your liking. You are doing these things because it makes you happy.
In the video below we’ll go over how it was created, which covers shell topics such as:
- Ensuring a variable is defined
- Ensuring a file doesn’t already exist
- Using command groups
dirnameto get the path of a file
- Outputting a multi-line string to a new file
- 0:25 – Developer happiness is important
- 1:18 – Life without this alias
- 3:28 – Using the mkscript function
- 4:55 – Looking at the function
- 5:20 – Using human readable variables
- 5:22 – Making sure the file doesn’t already exist
- 6:03 – Using return 1 instead of exit 1
- 6:44 – 1 line if statements and command groups
- 8:32 – Avoid clobbering existing files
- 9:10 – Creating parent directories with mkdir -p and dirname
- 10:31 – Redirecting a multi-line string to a file
- 11:52 – Making the file executable
- 12:08 – What would you put into the generated file?
What’s your base line shell script look like? Let me know below.