Shell Scripting Normalization, standardization

Source: Internet
Author: User

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:

  1. # define custom variables in lowercase, and environment variables in uppercase

  2. nikto_file= "$HOME/downloads/nikto-master/program/nikto.pl"

  3. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.