1. Write more comments in the script
This is a recommended practice that can be applied not only in shell scripts, but also in all other types of programming.
commenting in a script can help you or others to read your script Learn about the different parts of the script.
Comments are defined with a # number.
2. when the script exits when the run fails
Sometimes, even if some commands fail, bash may continue to execute the script, which affects the rest of the script (which eventually leads to a logic error).
Use the following line to exit script execution when a command failure is encountered:
# If the command fails to run, let the script exit execution Set-o err exit # or SET-E
3. When Bash causes a script to exit without declaring a variable
Bash may also use undeclared variables that can cause logic errors. So use the following line to notify Bash
When it tries to use an undeclared variable, it exits the script execution:
# If a variable is not set, let the script exit execution Set-o no unset # or Set-u
4 , use double quotation marks to refer to the variable
< Span style= "FONT-SIZE:12PX;" > when referencing (using the value of a variable), use double quotation marks to help prevent unnecessary matches due to whitespace causing the word to break apart and because of the recognition and expansion of wildcards.
take a look at the following example:
#!/bin/bash # If the command fails, let the script exit set -o errexit # If a variable that is not set is used, the script exits set -o nounset echo " Names without double quotes " echo names=" Tecmint Fossmint linusay " for name in $names; do echo "$name" done echo echo "Names with double quotes " echo for name in "$names"; do echo "$name" &Nbsp; done exit 0
Save the file and exit, then run it as follows:
5, using functions in scripts
In addition to very small scripts (only a few lines of code), always remember to use functions to modularize the code and make the script more readable and reusable.
The syntax for writing a function is as follows:
function check_root () { command1; command2; } # or check_root () { command1; command2; }
When you write a single line of code, you use the terminating symbol after each command:
Check_root () {command1; command2;}
6, string comparison with = instead of = =
NOTE: = = is synonymous with =, so only one single = To do string comparisons,
For example:
1 Please enter title value1= "tecmint.com "&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
value2= "fossmint.com" &NBSP;
if [ "$value 1" = "$value 2" &NBSP;]
7, use $ (command) to do substitution
command substitution is replacing the command itself with the output of this command. Use $ (command) instead of quotation marks ' command ' to do the command substitution.
This is also recommended by the Shellcheck tool, which displays warnings and recommendations for Shell scripting. For example:
User= ' echo ' $UID '
User=$ (echo "$UID")
8 . Declare static variables with readonly
ReadOnly passwd_file= "/etc/passwd"
ReadOnly group_file= "/etc/group"
9. Environment variables are named with uppercase letters, while custom variables are lowercase
all bash environment variables are named in uppercase letters, so use lowercase letters to name your custom variables to avoid variable name collisions:
# define custom variables in lowercase, and environment variables in uppercase
nikto_file= "$HOME/downloads/nikto-master/program/nikto.pl"
perl "$nikto _file"-H " $"
This article is from the "Boyhack" blog, make sure to keep this source http://461205160.blog.51cto.com/274918/1941165
Shell Scripting Normalization, standardization