You'll find a downloadable shell script checklist and template that comes along with the Bash Scripting and Shell Programming Learning Path. In this Course, we're going to briefly go over the main points in that checklist.
You'll find a downloadable shell script checklist and template that comes along with this course. I'm going to briefly go over the main points in that checklist.
First off, all scripts should start with a shebang. This is the first line of your script and it explicitly declares which interpreter is to be used to execute the script.
Next, you should include at least a one-line comment summarizing what the script does and maybe why it exists. You can include more information if you'd like, some people like to include information such as the original author of the script, the version of the script, et cetera. If you're going to be using global variables in your script, they should come next unless their value cannot be determined until a later point in your script.
Functions should come next, group all of your functions together following the global variables. Within your functions, be sure to use the local keyword when defining variables that are local to the function.
Following the function section, the main portion of your shell script begins. Finally, remember to explicitly use an exit status. If you do not supply an exit status which is a number from zero to 255, the exit status of the previously executed command will be used as the exit status of your script. It's best to control this rather than to leave it to chance.
At least end your script with an exit zero line. If your script was able to get to the last line then in most cases, that means it's successfully completed. Also keep in mind the other places where your shell script may terminate. You'll want to use exit statements there as well. For example, if your script performs some sort of check and then stops because the check failed, you'll want to exit with a non-zero exit status at that point.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.