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

How to write a robust and reliable shell script

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

Share

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

How to write a robust and reliable shell script, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

It's easy to write shell scripts with the attitude that you can run, but it's not that easy if you want to write robust, reliable shell scripts. So are there any operational experiences or methods?

Grammar check

The first and easiest way is to use a tool to check the script to find syntax errors in shell scripts as much as possible. If you don't know it, you should never miss it.

In order to ensure that the script is robust and reliable, it is necessary to ensure that the script appears as early as possible in some special situations to avoid getting caught. Let's see what possible techniques are available.

Exit when the script fails

You can set the following at the beginning of the script:

Set-e

For example:

#! / bin/bashset-elp # there will be an error date when running here

In this case, if something goes wrong, the run will exit, and one problem will not be missed:

/ test.shlp: Error-no default destination available.

Of course, there is a downside to this. Sometimes the execution of the command may have failed. You still want it to continue. You can temporarily add | true:

#! / bin/bashset-elp | | true date

However, I don't think such a setting is particularly useful, because many times you just need to deal with different error situations, and you can only exit with an error or think it's correct, so that you can't get to the abnormal branch.

Set it back through set + e:

Set-e#commandset + e#other command

Print script execution process

During the debugging phase, you may want to know which commands are executed in the whole process and what each command is executed in detail, which can be executed in the following ways:

Sh-x test.sh

Or, like above, add set-x at the beginning:

/ / Source: official account [programming Zhuji] / / author: Mr. Watchman #! / bin/bashset-xif [$#-lt 1] then echo "no para" else echo "para 1 $1" fi

When executed, the output is as follows:

+ [0-le 1] + echo no parano para

The content with + in front is the actual execution of the command, you can see what the comparison condition is, the variable is expanded into the specific content, and it is very clear which branch to go to.

Show undefined variables

Variables are not defined in shell and can still be used, but the results may not be what you expect. For example:

/ / Source: official account [programming Zhuji] / / author: Mr. Shouwang #! / bin/bashif ["$var" = "abc"] then echo "not abc" else echo "abc" fi

Originally, we wanted to judge whether the content of var is abc. In fact, var is not defined, but there is no error in using it here. If we want to find such problems early and avoid being covered up in complex scripts, we can add:

Set-u

Run it again and you will be prompted:

Test.sh: 5: test.sh: num: parameter not set

Imagine again, you were going to delete:

Rm-rf $dir/*

Then what does dir become when it is empty?

Do you feel a chill in your back?

The whole failure when a pipe command fails

Sometimes we may execute commands like this:

Cat test.sh | grep if | cut-d';'- f 2

Three commands are executed on one line, and if we want to fail in one of them, the whole command fails and avoids the execution of subsequent meaningless commands, then we can set it at the beginning:

Set-o pipefail

Without setting, even if the execution of cat test.sh fails, the subsequent grep will actually continue to execute, which may lead to some unexpected situations, which is helpful if you don't want this to happen.

Use readonly for static variables

Usually we define some static variables at the beginning of the script:

MY_PATH=/usr/bin

To prevent MY_PATH from being accidentally modified, you can do this:

Readonly MY_PATH=/usr/bin

In this way, an error will be reported if there is an order to try to modify it later.

#! / bin/bashreadonly MY_PATH=/usr/binMY_PATH=/usr/local/bin

Give it a try:

$. / test.shtest.sh: 3: test.sh: MY_PATH: is read only

Look, I gave you a hint!

Set an optional initial value for the variable

For example:

Name=$ {1:-shouwang} echo "${name}"

Here, let name be $1, which is the first parameter, and when it is empty, make name shouwang.

Multiple command execution uses & &

For example:

Cmd0;cmd1;cmd1

Here, if the cmd0 fails, the following commands will still be executed, and if you do not want the subsequent commands to be executed, you can use:

Cmd0 & & cmd1 & & cmd1

Use function

It is good that the script itself is short, but once the script becomes longer and does not use functions, it will be difficult to maintain and poor readability.

Summary

In fact, the script checking tool introduced at the beginning is already very effective, basic errors can be detected, while the rest of the content is more focused on script debugging, not missing any possible errors.

Finally, the shellcheck tool is preferred.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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