Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shell efficient programming: shell scripts have never been so beautiful

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

In normal work, do you feel that shell scripts are just piled up of commands; often they are just to achieve function without a good organizational structure; their own scripts do not want to see it again; , I will teach shell the "Sunflower Book", which makes your shell scripts have the same aesthetic feeling as python and is very easy to maintain. It is really shell scripts that can't be stopped.

In normal work, do you feel that shell scripts are just piled up of commands; often they are just to achieve function without a good organizational structure; their own scripts do not want to see it again; , I will teach shell the "Sunflower Book", which makes your shell scripts have the same aesthetic feeling as python and is very easy to maintain. It is really shell scripts that can't be stopped.

Take the nginx control script as an example to take a look at the cosmetic process of the script:

The wonderful use of the #! / bin/bash # set command is left at the end of the article: set-eu # nginx overloaded configuration file nginx-c / etc/nginx/nginx.conf-t kill-HUP `ps auxf | grep-E "nginx: [[: space:]] + master" | awk'{print $2} '``variable extraction from the secret book of Baodian.

In the script, we need to rely on external configuration, such as configuration file location, listening port and other variable parameters, which need to be extracted into variables when programming, for better reference and maintenance, saving the trouble caused by external configuration changes.

The wonderful use of the #! / bin/bash # set command is left at the end of the article. The set-eu # nginx configuration file may have different locations in different environments, so you need to extract it into configurable variables. Script to reference the NGINX_CONFIG_FILE=/etc/nginx/nginx.conf # nginx reload configuration file nginx-c $NGINX_CONFIG_FILE-t kill-HUP `ps auxf | grep-E "nginx: [[: space:]] + master" | awk'{print $2} '`extraction of variables from the treasure book

In the script, we need to rely on external configuration, such as configuration file location, listening port and other variable parameters, which need to be extracted into variables when programming, for better reference and maintenance, saving the trouble caused by external configuration changes.

The wonderful use of the #! / bin/bash # set command is left at the end of the article. The set-eu # nginx configuration file may have different locations in different environments, so you need to extract it into configurable variables. Script to reference NGINX_CONFIG_FILE=/etc/nginx/nginx.conf # nginx reload configuration file nginx-c $NGINX_CONFIG_FILE-t kill-HUP `ps auxf | grep-E "nginx: [[: space:]] + master" | awk'{print $2} '`Modularization of Treasure Book

After all, operations and maintenance is not a professional programmer, how to write a script is comfortable, to put it simply, it is executed in turn from top to bottom, which leads to poor reusability and is not easy to maintain. The key to solve this problem lies in functional and modular thinking. Although shell is a relatively simple language, the basic logic control and functional functions of the language have, which makes it possible for us to write high-quality shell scripts. Next, let's take a look at specific examples to feel it:

# since nginx configuration file checking is the first step in performing other operations, we separate it into a separate function #! / bin/bash # the wonderful use of the set command at the end of the article set-eu # nginx configuration file may be different in different environments, so we need to extract it into configurable variables Script to reference NGINX_CONFIG_FILE=/etc/nginx/nginx.conf # extract configuration file to check as a separate function config_test () {nginx-c $NGINX_CONFIG_FILE-t} get_nginx_master_pid () {echo `ps auxf | grep-E "nginx: [[: space:]] + master" | awk'{print $2}'} # extraction configuration is overloaded as a stand-alone function reload () {kill-HUP `get_nginx_master_ pid`} # nginx overloaded configuration file main function of config_test reload bibliography

The maintainability of the script depends on the structure of the script. In order to have a better structure, you usually need to define the entry function, that is, the main function, in the script, so that when I maintain the script, I can better grasp the organizational structure of the script and find the entry point:

# since nginx configuration file checking is the first step in performing other operations, we separate it into a separate function #! / bin/bash # the wonderful use of the set command at the end of the article set-eu # nginx configuration file may be different in different environments, so we need to extract it into configurable variables Script to reference NGINX_CONFIG_FILE=/etc/nginx/nginx.conf # extraction configuration file as a separate function config_test () {nginx-c $NGINX_CONFIG_FILE-t} get_nginx_master_pid () {echo `ps auxf | grep-E "nginx: [[: space:]] + master" | awk'{print $2}'} # extraction configuration is overloaded as a stand-alone function reload () {kill-HUP `get _ nginx_master_ pid`} # entry function main () {config_test reload} # main here you need to get the parameters of the script itself So pass $@ to the function return value of the secret book of the main function main $@

In other programming languages, the return value of a function can be obtained through return, but the meaning of the return statement is different in shell. The return command returns a function with a single numeric parameter that is visible in the script that calls the function. If no return parameter is specified, return returns the return code of the last command by default. So how do you achieve return effects similar to those of other programming languages? You can use the echo command:

#! / bin/bash set-eu NGINX_CONFIG_FILE=/etc/nginx/nginx.conf config_test () {nginx-c $NGINX_CONFIG_FILE-t} # here you can directly use the master pid of nginx to obtain the value get_nginx_master_pid () {echo `ps auxf after echo through backquotes | grep-E "nginx: [[: space:]] + master" | awk'{print $2}'} reload () {# `get_nginx_master_ pid` acquires nginx master pid kill-HUP `get_nginx_master_ pid`} # entry function main () {config_test reload} # main. Here, you need to obtain the parameters of the script itself. Therefore, pass $@ to the set command of the secret book of the main function main $@.

Bash's built-in set command can change the execution behavior of our script, so that I can grasp and debug the script more powerfully. here are several commonly used set instructions, which I believe you will like:

Set-e: the bash script exits immediately upon an error

Set-n: check script syntax but do not execute

Set-u: exit immediately if you encounter an unset variable

Set-o pipefail: control to exit immediately if there is an error in the execution of the pipe character

Set-x: step-by-step debugging command

When writing a script, we can simply write it in the following format at the beginning of the script:

#! / bin/bashset-euxo pipefail

When checking the syntax of a bash script, you can write:

Bash-n main.sh

The original text is from: https://www.linuxprobe.com/shell-programming-scripts.html

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report