In this present post, we will continue the Tutorial 05 from our tutorial series on Shell scripting. Specifically, we will address a series good practice, to take into account when carrying out the same.
Since, in the previous (Tutorial 04) we address others basic practical points related to these, specifically the how they are generated, how they are executed, and what are the parts that make up a bash shell script.
And, before starting this post called “Shell Scripting – Tutorial 05”, we recommend exploring the following related contents, at the end of reading this post today:
Shell Scripting Tutorial 05
Best good practices to create a Script
Top 10 Best Practices for Shell Scripting
Among the latests Moravia's compositions 10 most important that we can mention are the following:
- Indent the code: A code developed in readable form is very vital for its better understanding. And the necessary indentations will offer a clear view of the elaborate logical structure.
- Add separating spaces between sections of code: Separating the code into modules or sections makes any code more readable and easy to understand, no matter how long it is.
- Comment the code as much as possible: Adding useful and necessary descriptions to each line or order of command, section of code or function developed, makes it easier to understand what has been programmed.
- Create variables with descriptive names of your functions: Assigning variable names that clearly describe and identify the function for which it was created helps to understand its purpose.
- Use the syntax
VARIABLE=$(comando)
for command substitution: Instead of, the old way now deprecated followingVARIABLE=`date +%F`
. - Use modules or variables for validation of superuser and authorized users, with or without a password: To increase security levels, in the required parts of the code.
- Use modules or validation variables of the Operating System (Distro, Version, Architecture): To prevent the use of the files on unsupported computers (or servers).
- Use modules or procedures to confirm the execution of critical or batch actions: To minimize mistakes due to improvisation or carelessness.
- Include assorted essential modules: Among those that can be mentioned, Welcome and Farewell modules, double execution verification, for a better user experience.
- Create user-friendly visual interfaces: Both by, Terminal (CLI) and for the Desktop (GUI) using the commands
"dialog"
,"zenity"
,"gxmessage"
,"notify-send"
and even the commands"mpg123 y espeak"
for sonic alerts and audible notifications with humanized or robotic voice.
Other important
- Rationalize Script size with External Functions and/or Modules: If a Script will end up being very large, it is best to split it using functions or split it into smaller Script files, which are called by a main Script.
- Invoke, in a clear and evident way, the calls to other Interpreters (programming languages) within the Script: To do this, we must invoke them clearly by lines or modules.
Summary
In short, we hope this Tutorial 05 on «Shell Scripting» on the best good practices when making scripts, and the previous ones, are increasing the knowledge of many, when making the most optimal and functional Script files generated with Bash Shell.
If you liked the content, comment and share it. And remember, visit the beginning of our «site», in addition to the official channel of Telegram for more news, tutorials and Linux updates.