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

What should be paid attention to when writing Bash scripts in Linux

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Today, the editor will share with you the skills of writing Bash scripts in Linux. The editor thinks it is very practical, so I will share it for you as a reference. Let's follow the editor and have a look.

1. Write more comments in the script

This is a recommended practice that can be used not only in shell scripting programs, but also in all other types of programming. Commenting on a script can help you or others to understand what different parts of the script are doing when flipping through your script.

For beginners, comments are defined by the # sign.

# TecMint is the best site for browsing all kinds of Linux articles

2. Exit the script 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 logic errors). Exit script execution in the event of a command failure using the following line:

10 tips for writing Bash scripts efficiently in Linux

3. Exit the script when Bash uses undeclared variables

Bash may also use undeclared variables that can cause logic errors. So tell bash to exit script execution when it tries to use an undeclared variable:

10 tips for writing Bash scripts efficiently in Linux

4. Use double quotation marks to reference variables

Using double quotation marks when referencing (using the value of a variable) helps prevent unnecessary matching due to the separation of words caused by spaces and the recognition and extension of wildcards.

Look at the following example:

Save the file and exit, and then run as follows:

$. / names.sh

Use double quotation marks in a script

5. Use functions in scripts

Except for 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:

When writing a single line of code, use a termination symbol after each command:

Check_root () {command1; command2;}

6. Use = instead of = when comparing strings

Note that = is synonymous with =, so only a single = is used for string comparison, for example:

7. Replace it with $(command) instead of the old command

Command substitution replaces the command itself with the output of this command. Use $(command) instead of quotation marks command for command substitution.

This approach is also recommended by shellcheck tool, which displays warnings and recommendations for shell scripts. For example:

8. Declare static variables with readonly

A static variable does not change; its value cannot be modified once it is defined in the script:

9. Environment variables are named in uppercase letters, while custom variables are named in lowercase

All bash environment variables are named in uppercase letters, so name your custom variables in lowercase letters to avoid variable name conflicts:

10. Always debug long scripts

If you are writing a bash script with thousands of lines of code, troubleshooting can turn into a nightmare. To make it easy to fix some errors before the script is executed, you need to do some debugging.

This is the end of the skills for writing Bash scripts in Linux. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

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