Code written in shell scripts

Centos7 is now using software bash, bash version can be viewed with the following command:

[root@localhost ~]# cat /etc/redhat-release     #查看系统的版本
CentOS Linux release 7.5.1804 (Core)     #我这里使用的是centos 7.5 1804
[root@localhost ~]# bash --version   #查看bash的版本
GNU bash, 版本 4.2.46(2)-release (x86_64-redhat-linux-gnu) #这一行就是bash版本
Copyright (C) 2011 Free Software Foundation, Inc.
许可证 GPLv3+: GNU GPL 许可证版本3或者更高 <http://gnu.org/licenses/gpl.html>

这是自由软件,您可以自由地更改和重新发布。
在法律允许的范围内没有担保.

If the server in a production environment using an older version of the system and shell, it is recommended to upgrade to the latest version of the shell, because nearly two-year-old version was the presence of more serious security vulnerabilities exposed.

bash software once broke serious flaws (broken shell vulnerability), by virtue of this vulnerability, others may take over the entire operating system of the computer, to gain access to confidential information in a variety of systems, and changes to the system and so on. Anyone's computer system, if you use the bash software, you need to immediately patch. Methods for detecting whether there are loopholes in the system are:

#测试系统是否存在漏洞
[root@localhost ~]# env x='() {:;}; echo be careful' bash -c "echo this is a test"
this is a test

Returns the contents of the above "this is a test", it means normal, if the return something, you need to upgrade bash, but merely for learning and testing do not care.

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

Tip: If no output be careful, you do not need to upgrade.

Upgrade method is as follows:

[root@localhost ~]# rpm -qa bash    #升级前的bash版本
bash-4.2.46-30.el7.x86_64
[root@localhost ~]# yum -y update bash    #执行升级命令
[root@localhost ~]# rpm -qa bash  #升级后的bash版本
bash-4.2.46-31.el7.x86_64

Code written in shell script:

1, the beginning of the script line indicate the use of what kind of interpreter, such as:! # / Bin / sh, # / bin / bash .......!
2, regulate the suffix name of the script, if the script is Shell, it is ".sh"; if it is a Python script, is ".py"; If expect script is ".exp".
3, written notes, not scolded, scripts, in addition to the first line after the # specified using the shell, all of the following are the # indicates a comment, you can share one line and the code can also be written on a separate line, it is best not to appear Chinese , if not write comments, be subject to the curse of their own over time you may also see a script to write their own do not understand.

shell script execution:

When Shell script runs, it first looks for system environment variables ENV, the environment variable specifies a file (usually /etc/profile,~/.bash_profile,~/.bashrc,/etc/bashrc load order, etc.), loading after the above-mentioned environment variable file, Shell began performing content Shell script

Shell scripts are top-down, command execution and statements of each line from left to right, that is, over the implementation of a command and then execute a, if you encounter sub-script Shell script (ie nested script), the content will be the first implementation of the sub-script, return to the parent after the completion of the script to continue execution of subsequent commands and statements within 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 is not executable permissions (i.e. attribute file permission bits x - number) method often used, or a method to use when there is no need to specify the beginning of the script file interpreter. This is the recommended method of use;
  • path / script-name or ./script-name: refers in the current path to execute the script (script requires execute permissions), the required permission to the script file to the executable (i.e., x plus attributes file permission bits), the specific method is chmod + x script-name. Then you can execute the script by script absolute or relative path directly.
  • source script-name or .script-name: "." This method is commonly used source or (dot) to be read or to load the specified script file Shell (e.g. san.sh), and then sequentially executes the specified script file Shell All statements in san.sh. These statements of the parent Shell script father.sh process run in the current (several other new models will start the process of implementation of the sub-script). Thus, use or source. "" San.sh itself may be a script or function values ​​of the variables such as the current parent returns the value passed to the script father.sh Shell used. This is the biggest difference between it and several other methods.
  • sh \ <script-name or cat scripts-name | sh: The same applies to the bash, but this usage is not very common, but sometimes it can also have the effect of a surprise move, such as: do not loop to achieve the boot from the start to streamline the case of service It is by all spliced ​​form of command strings, and then via duct to bash operation.

in conclusion:

. "" By the source or script loaded executed, since the implementation of the script in the current shell, so after the end of the script, the script variables (including functions) value remains in the current shell; while sh and bash execute scripts will launch a new sub-shell to execute, execute after returning to the parent shell. Therefore, the value of variables and functions, etc. can not be reserved, during the shell script development, if there is demand for a reference or execute the scripts or configuration files of other scripts, the best use. "" Or source to load the script or configuration file , after processing is complete, and then load them into the script below, you can call the contents of the script and configuration file variables and functions such as loading the source.

Code written in the shell:

  • The first line specifies the shell script is a script interpreter (not required);
  • The beginning of shell scripts plus version, copyright information, usually written by the beginning of the second line in the script (not required);
  • In shell scripts as possible without Chinese (not just that comment);
  • Named shell scripts should .sh extension.
  • shell script should be stored in a fixed path, usually "/ server / scripts"
    The following is a shell script code written in good habits:
  • A pair of symbols should try to write it once, then backspace symbol increase in content in order to prevent the omission. These symbols are typically "{}" "[]" .......
  • In brackets [] at both ends must have at least one space, so you can leave out the brackets when typing a space, and then in the middle of the backspace key to enter the content, and make sure both ends have at least one space, that is to say the first type one pair brackets, and a back frame, two input space, passed on a grid, double brackets [[]] is the same wording.
  • For flow control statements, the format should be finished at one time, and then add content, are so many languages, such as:
    time completion format if statement:
    if condition contents
    then
    content
    fi
    time to complete format for the loop:
    for
    do
    content
    done

while and until, case and other statements as well.

  • By indentation make the code more readable, such as the above if, for statement.
  • For a conventional variable strings are defined to be the value of the variable double quotes and no spaces before and after the equal sign, references require strong (refer to reference character that was seen), the single quotation marks '', if a command reference, the use single quotes `` (esc key this key is located below), for example: username = "lv jian zhao"
  • Script single quotes, double quotes and backticks must sign in English state, in fact, all the characters and symbols of Linux, should be under the sign of the English state, and that needs special attention.
    Description: Good habits allow us to avoid a lot of unnecessary trouble, improve work efficiency.

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14154700/2432361