I’m often making shell scripts for various things – sometimes just a quick one for a specific task, sometimes just to test something out, sometimes for some kind of workflow task that I plan to keep around.
It’s always the same steps:
- Create the file.
- Add the header:
- Write the code.
- Save it.
- Exit the editor.
- Make it executable with
chmod +x <filename>
- Run, test, edit, etc.
When you’re repeating yourself, time for some automation. So I wrote a shell script that creates shell scripts.
#! /bin/bash if [ -f "$1" ] then echo "$1 already exists" exit 1 fi echo '#! /bin/bash' > $1 chmod +x $1 $EDITOR $1
Create a file, add that code, make it executable (sound familiar?) and put it somewhere in your path. I named mine
newss for new shell script.
Now you can just say:
And you are editing an executable shell script. As you can see, it checks to see if the file exists as a minimum level of safety. It will follow any paths, such as:
But only if the directories exist. Creating non-existent directories wouldn’t be too hard if that’s important to you.