6 shell tips to make scripts no longer amateurish (1 minute series)

The long article "Memcache Core Technical Points" has a low reading, restart the 1-minute series, fast-moving era, fragmentation time may everyone prefer short articles, prefer technical practice articles.
Voiceover: To be honest, technical thought articles (WHY, HOW) are more difficult to write than technical practice articles (WHAT).

How can I make my shell look less amateurish? The following 6 points of practice must be useful.
Voiceover: This article is derived from a Google practice. It extracts some content that can be read in 1 minute and adds some analysis.

1. Start with the following sentence

6 shell tips to make scripts no longer amateurish (1 minute series)

By default, set -o nounset will ignore and continue execution when encountering a variable that does not exist. This is often not in line with expectations. Adding this option can prevent the evil consequences from expanding and terminate the execution of the script.
Voiceover: Some variable names are mishandled, which can make people crash debugging for a long time. In this way, this kind of hand mistakes are discovered.


By default, set -o errexit will skip and continue execution when it encounters an execution error. This is often not in line with expectations. Adding this option can prevent the evil consequences from expanding and terminate the execution of the script.
Voice-over: Some Linux commands, such as the -f parameter of rm, can be forced to ignore errors. At this time, the script cannot capture errexit. Such parameters are not recommended in the script.

These two options are in line with the fail fast design concept.

Second, it is necessary to encapsulate functions

Don't just slip and write down, encapsulation can improve reuse.
6 shell tips to make scripts no longer amateurish (1 minute series)
As in the above example:
log() is
simply encapsulated, which can save a lot of repetitive code
[$(date +%Y/%m/%d\ %H:%M:%S)]
.
Voiceover: This log() is a bit interesting, have you learned it?

At the same time, encapsulation can improve the readability of the code.
6 shell tips to make scripts no longer amateurish (1 minute series)
As in the example above:
ExtractBashComments is
much more readable than
egrep "^#"
.
Voiceover: Some friends who carry the bar will say that they don’t know English.

Three, use readonly and local to modify variables

6 shell tips to make scripts no longer amateurish (1 minute series)

As the name implies, readonly is read-only.


Variables in the local function.

Don't try to save trouble, while improving security, you can avoid many inexplicable mistakes that make people crash. The script is written professionally or unprofessional, and it is often not a sophisticated point, which can be reflected from the basic skills.
Voiceover: It is said that the level of a C++ programmer can be seen from the frequency of using const in ta code.

Fourth, use $() instead of `(back quotes)

6 shell tips to make scripts no longer amateurish (1 minute series)
why? After reading the above example, you will understand:
(1) $() can support inline;
(2) $() does not need to be escaped;
(3) Some fonts, `(back quote) and '(single quote) are very Like, it is easy to confuse people;

5. Use [[]] instead of []

Use single brackets:
6 shell tips to make scripts no longer amateurish (1 minute series)
use double brackets:
6 shell tips to make scripts no longer amateurish (1 minute series)

Do you see the difference? [[]] More in line with human nature coding:
(1) Avoid escaping problems;
(2) There are many new features;

New features include but are not limited to:
||: logical or
&&: logical and
<: string comparison (no need to escape)
==: wildcard (globbing) string comparison
=~: regular expression (regular expression, RegEx) characters It
6 shell tips to make scripts no longer amateurish (1 minute series)
should be noted that since bash3.2, wildcards and regular expressions cannot be wrapped in quotation marks (so, in the above example, adding quotation marks is a literal comparison).
6 shell tips to make scripts no longer amateurish (1 minute series)
So if there are spaces in the expression, it must be stored in a variable, and then the wildcard and regular comparisons.

Six, echo is not the only debugging method

6 shell tips to make scripts no longer amateurish (1 minute series)
You can use -n to check the syntax of the script.
6 shell tips to make scripts no longer amateurish (1 minute series)

You can use -v to track the execution of each command in the script.
6 shell tips to make scripts no longer amateurish (1 minute series)

You can use -x to track the execution of each command in the script and append extended information.

Of course, you can also add
set -o verbose
set -o xtrace in the script
to permanently specify the output debugging information.
Voice-over: I'll see it after I try it on the machine.

I hope you will find something in this minute.
6 shell tips to make scripts no longer amateurish (1 minute series)
Architect's Road-Sharing technical ideas
related recommendations:
"What are the technical points to master when writing a cache"

When researching and writing scripts:
(1) Have you driven yourself crazy because of a hand error?
(2) Did you faint yourself because of escaping?
(3) Is it to debug with echo?

Guess you like

Origin blog.51cto.com/jyjstack/2548721