Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use shell

Shulou Source: shulou.com Published: 2022-06-02 04:28:44 10月28日 Update

This article mainly shows you "how to use shell", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use shell" this article.

First, the shell script starts to set up

#! / bin/bashset-o nounsetset-o errexit

(1) set-o nounset

Encounter a variable that does not exist and terminate the execution of the script. When shell scripts are executed, they often continue to execute when they encounter undefined variables, which is not in line with expectations, and it often brings inexplicable problems and increases the difficulty of troubleshooting.

(2) set-o errexit

During the execution of the shell script, errors are encountered, but execution often continues, which is not in line with expectations. Coupled with this setting, the execution of the shell script will be terminated if an error is encountered during the execution of the script.

These two settings are in line with the design philosophy of fail fast.

Second, it is necessary to encapsulate functions.

Encapsulation can improve reuse.

Third, use readonly and local to modify variables

Readonly: as the name implies, read-only.

Local: variables within the function.

It not only improves security, but also avoids a lot of collapsing and inexplicable mistakes.

4. Use $() instead of `(anti-single quotation marks)

Example:

#! / bin/bashpath= `pwd`echo $pathecho "`pwd`" echo "$(pwd)"

$() can support embedding

$() need not be escaped

Use [[]] instead of []

[[]] more in line with human coding

Avoid escape problems

There are a lot of new features

New features include, but are not limited to:

| |: logic or

& &: logic and

<: string comparison, no escape processing required

= =: wildcard (globbing) string comparison

= ~: regular expression (regular expression, RegEx) string comparison

Starting with bash4.2, neither wildcards nor regular expressions can be wrapped in quotation marks, so the quotation marks of wildcards and regular expressions are literal comparisons.

So if there are spaces in the expression, you must store it in a variable and compare the wildcard with the regular.

VI. Debugging methods

1. Echo printing

2. Use-n to check the syntax of the script.

Sh-n myshell.sh

3. Use-v to track the execution of each command in the script.

Sh-v myshell.sh

4. Use-x to track the execution of each command in the script and add additional information.

Sh-x myshell.sh

You can also add the following settings to the script to permanently specify the output debugging information

Set-o verbose set-o xtrace

Remove after testing.

The above is all the contents of this article "how to use shell". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Script variable regular expression wildcard content character string quotation mark article escape error inexplicable this is not information function command new function procedure logic Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL Docker OPPO Reno Microsoft