Shell script writing code

Now centos7 is using the bash software, you can view the bash version through the following command:

[[email protected] ~]# cat /etc/redhat-release #View the system version 
CentOS Linux release 7.5.1804 (Core) #I use centos 7.5 1804 here
[[email protected] ~]# bash --version #View the version of bash
GNU bash, Version 4.2.46(2)-release (x86_64-redhat-linux-gnu) #This line is the bash version
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL License License version 3 or higher

This is free software, you can change and redistribute it freely.
There is no guarantee within the scope permitted by law.

If the server in the production environment uses an older version of the system and shell, then it is recommended to upgrade it to the latest version of the shell, because it is close to The two-year-old version was exposed to serious security vulnerabilities.

Bash software once exposed a serious loophole (shell-breaking loophole), by virtue of this loophole, others may take over the computer’s entire operating system, access to confidential information in various systems, and make changes to the system Wait. Anyone’s computer system needs to be patched immediately if bash software is used. The method to detect whether the system has vulnerabilities is:

#Test whether the system has vulnerabilities
[[emailprotected] ~]# env x='() {:;}; echo be careful' bash -c "echo this is a test"
this is a test

Return the above content “this is a test”, it means normal, if the following content is returned, you need to upgrade bash, However, it doesn’t matter just for learning and testing.

[[email protected] ~]# env x='() {:;}; echo be careful' bash -c "echo this is a test"
be careful
this is a test

Reminder: If there is no output be careful, you do not need to upgrade.

The upgrade method is as follows:

[[email protected] ~]# rpm -qa bash #Bash version before upgrade
bash-4.2.46-30.el7. x86_64
[[emailprotected] ~]# yum -y update bash #Execute the upgrade command
[[emailprotected] ~]# rpm -qa bash #Updated version of bash
bash- 4.2.46-31.el7.x86_64

Code for writing shell scripts:

1. At the beginning of the script, indicate which interpreter is used, such as: #!/bin/ sh, #!/bin/bash…….
2. The suffix name of the standard script, if it is a shell script, it is “.sh”; if it is a Python script, it is “.py”; if it is an expect script, it is “.exp”.
3. Write a good comment and do not get scolded. In the script, except for the first line, specify the shell after the # sign. All the following # signs indicate comments. You can share a line with the code or write a line separately. It is best Do not appear in Chinese. If you do not write comments, you will be scolded. You may not understand the script you wrote after a while.

The execution of the shell script:

When the Shell script runs, it will first look for the system environment variable ENV, which specifies the environment file (the order of loading is usually /etc/profile, ~/.bash_profile, ~/.bashrc, /etc/bashrc, etc.). After the above environment variable files are loaded, the Shell starts to execute the contents of the Shell script.

The shell script is from top to bottom, Execute each line of commands and statements from left to right, that is, execute one command before executing the next one. If a sub-script (ie script nesting) is encountered in the Shell script, the content of the sub-script will be executed first , And then return to the parent script to continue executing the subsequent commands and statements in the parent script.

Shell scripts can usually be executed in the following ways:

  • bash script-name or sh script-name: This is when the script file itself has no available Execution permission (that is, the file permission attribute x is the-sign) method often used, or the method that needs to be used when the interpreter is not specified at the beginning of the script file. This is also the recommended method;
  • path/script-name or ./script-name: refers to the execution of the script under the current path (the script needs to have execution permission), and the permission of the script file needs to be changed to Executable (that is, file permission attribute plus x bit), the specific method is chmod+x script-name. Then you can directly execute the script through the script’s absolute path or relative path.
  • source script-name or .script-name: This method usually uses source or “.” (dot) to read or load the specified Shell script file (such as san.sh), and then, Execute all the statements in the specified shell script file san.sh in turn. These statements will run in the current parent Shell script father.sh process (the other modes will start a new process to execute the child script). Therefore, using source or “.” can pass the variable value or the return value of the function in the san.sh script to the current parent shell script father.sh for use. This is the biggest difference between it and several other methods.
  • sh\

Conclusion:

Load the executed script through source or “.”, because it is executed in the current shell Script, so after the script ends, the values ​​of variables (including functions) in the script still exist in the current shell; while sh and bash will start a new subshell to execute when they execute the script, and then return to the parent shell after execution. Therefore, variables and function values ​​cannot be retained. During shell script development, if there is a need to reference or execute other script content or configuration files in the script, it is best to use “.” or source to load the script or configuration file first. After the processing is completed, load them under the script, and then you can call the variables and functions in the script loaded by the source and the configuration file.

Code for writing shell:

  • The first line of the shell script is to specify the script interpreter (not required);
  • shell At the beginning of the script, information such as version and copyright will be added, usually starting from the second line in the script (not necessary);
  • Try not to use Chinese in shell scripts (not just comments);< /li>
  • The name of the shell script should have the extension .sh.
  • Shell scripts should be stored in a fixed path, generally “/server/scripts”
    The following is a good habit of shell script code writing:
  • Paired symbols You should try to write it all at once, and then backspace to add content to the symbol to prevent omissions. These symbols are generally “{ }” “[ ]”…….
  • There must be at least one space at both ends of the brackets [], so you can leave spaces when you type the brackets, and then Enter the content in the middle with the backspace key, and make sure that there is at least one space at both ends, that is to say, type a pair of square brackets, then back one space, enter two spaces, then back one space, double square brackets [[] ] Is also written in the same way.
  • For flow control statements, you should write the format at one time, and then add the content. This is the case in many languages, such as:
    The format for completing the if statement at one time:
    if condition Content
    then
    Content
    fi
    The format of completing the for loop statement at one time:
    for
    do
    Content
    done

< p>while, until, case and other statements are the same.

  • Use indentation to make the code easier to read, such as the if and for statements above.
  • For the string definition of regular variables, double quotation marks should be added to the variable value, and there can be no spaces before and after the equal sign. If strong quotation is required (referring to what you see is what you get), use single quotation marks” , If it is a command quotation, use reverse quotes “ (the key is located below the esc key), for example: username=”lv jian zhao”
  • Single quotes, double quotes and backquotes in the script It must be a symbol in English. In fact, all Linux characters and symbols should be symbols in English. This point requires special attention.
    Note: Good habits can help us avoid a lot of unnecessary troubles and improve work efficiency.

———————— This concludes this article, thanks for reading————————

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5536 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.