How to test the correctness of shell scripts

Source: Internet
Author: User

In the actual work, the shell script needs to be tested for correctness.

How to test in the quickest and most effective way?

A lot of development habits are, apart, write/Get, run a, see input, output, want to do the operation is completed, also over.

In fact, this is very not rigorous, if not through the QA, the risk is quite large.

The following is the shell script test process, for reference only

1. Code for the daytime:

Finish writing, or get a shell script, do not have to rush to run, although the practice is the only standard of inspection and collation, but in the process of reading code, you can avoid a lot of low-level bugs.

Read what?

A. Code logic, what is the script used to do, how many steps are there, and what does it do separately?  used to check for missing logic, or to violate requirements. B. Specific syntax, variables, judge the  syntax of the sentence, whether the variable is defined, whether the statement logic is correct, whether to consider various exceptions, whether the error exits, returns the correct state value, and so on.

2. Syntax Detection:

The syntax of the shell is quite silent, and a lot of places that are easily overlooked.

Command format:

Sh-n ***.sh

If there is no abnormal output, it proves that the script has no obvious grammatical problems.

3. Run the trace:

Practice is the only standard for inspection and finishing, run a.

However, it is not directly run and then to see the final result, this will miss out a lot of intermediate process.

Command format:

SH-VX ***.sh

The results are as follows:

We can see

Each line of code original command (none +): [This is the effect of-V]

The case of code execution (with +), including the result of the operation, logical judgment result, variable assignment, etc. [-X effect]

What we want to focus on is this information, which is mainly the value of variables and the result of logical judgments.

4. Overwrite branches:

Run directly, can only cover the main process, for other control flow branches of the code can not be overwritten.

For critical, focused logic, we need to create conditions so that the running script can go into the corresponding branch

5. Other:

A. About BASHDB: It  can be tried, but the sense of input-output is not high B. About unit testing:  actual work, because the project pressure is large, the unit test cost is very high, so far not.

6. Is there a better way?

Well, single-step tracking, the script is good, the log information is not too much, if more, there are calls to other scripts and so on .....

The log volume reaches thousands of lines, which is very easy to do.

Tracked children's shoes have the same feeling, show not friendly, pale white a piece, 1000 line down, look at the dazzling.

It is easy to miss (LZ was pit a lot back, you see, or don't look ...) The error message is right there, just ignore it.

So. A layer of optimization was made to process logs, use regular, and annotate the information I care about

Contrast:

The script is implemented in Python, location: Https://github.com/wklken/pytools/tree/master/shell

The idea is: Execute, catch all logs, match with regular, color, and then output

All right, that's all.

The tool is implemented to improve efficiency and save time.

The end!

Transferred from: http://wklken.me/posts/2012/09/15/how-to-test-shell.html

How to test the correctness of shell scripts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.