Shell Scripting Learning Guide

Source: Internet
Author: User
Tags shellcheck

Shell Scripting Learning Guide

The following eight points can not be said to be your shell Scripting Learning Guide de all, at least let you write a reliable shell script.

1. Specify bash

The first line of the shell script, what should be after #!?

If you ask someone about this question, the answers may vary from one person to another. I've seen/usr/bin/env bash,/bin/bash,/usr/bin/bash,/bin/sh, and/usr/bin/env Sh. This is the programming world "' anise ' word four ways."

In most cases, all five of these formulations are equivalent. However, people who have written the program know that "in a few cases" often hides unexpected pits.

What if the default shell of the system is not bash? For example, a Linux distribution version, the default SH is not bash.

What if the system bash is not in/usr/bin/bash?

I recommend using/usr/bin/env Bash and/bin/bash. The former adds a middle layer through env, allowing Env to search for bash in $path, which is the official endorsement of the conventional Bash location, and/usr/bin/bash is just a symbolic link to it.
2. Set-e and Set-x

OK, after some discussion, the first line is now settled. Now it's time to start writing the second line?
Wait! Before you begin to conceive and write down specific code logic, insert a line of SET-E and a line of set-x.

Set-x will output the executed content when executing each line of the shell script. It allows you to see the current execution, and the variables involved will be replaced with actual values.

SET-E will end the program in the execution of an error, just like "throw exceptions" in other languages. (accurately, not all errors will end the program, see note below)

Note: The conditions of the SET-E end program are more complex, in Man Bash, a full use of a word to describe various scenarios. Most executions exit when an error occurs, unless the shell command is in the following situation:

A non-ending part of a pipeline, such as Error | Ok

A non-ending part of a combined statement, such as OK && Error | | Other

A non-trailing part of a series of statements, such as error; Ok

In the judgment statement, including Test, if, while and so on.

These two combinations are used together and can save you a lot of time when you debug. For defensive programming reasons, it is necessary to insert them before writing the first line of specific code. Ask yourself, what is the number of times you can write a pair of code at a time? Most code, before committing, usually undergoes a process of repeatedly debugging changes. Rather than introducing these two configurations at the time of your own time, it's better to leave debug at the beginning. After the code can finally be submitted, it is not too late to consider whether to keep them.
3. Bring the Shellcheck

OK, now I have three lines (boilerplate) code, the specific business logic does not write a line. Is it time to start writing?
Wait! 工欲善其事, its prerequisite. This time, I'll introduce a shell scripting artifact: Shellcheck

Ashamed to say, although I wrote a few years shell script, some grammar I still can't remember clearly. This time will rely on Shellcheck guidance.

Shellcheck can also check out the common bad code (Getting Started tutorial qkxue.net) for Shell scripting, in addition to alerting you to grammatical problems. Originally my n advice, there are a few of these bad code, but considering that Shellcheck can fully explore these problems, so painful to remove them all out. There is no doubt that the use of Shellcheck has brought a huge leap to my shell writing skills.

The so-called "standing on the Shoulders of giants", although we recruit eggs, skills than veterans strong, but we can catch each other on the equipment Ah! Do a hands-on installation, you can get acquainted with a "teacher", why not?

By the way, Shellcheck was actually written in Haskell. Who says Haskell can only be used for faking?
4. Variable expansion

In a shell script, you can occasionally see this: Echo $xxx | Awk/sed/grep/cut .... Looks like a big picture of the situation, in fact, just want to change the value of a variable.

Why use sledgehammer to kill chickens? Bash built-in variable expansion mechanism is enough to meet your various needs! Or the old way, read the f**k manaul! Man bash then search for parameter Expansion, here's the trick you want. Key has also written a related article, hoping to help a helping hand: play the bash variable
5. Note the local

As the code writes more, you begin to refine the repetitive logic into functions. It's possible that you'll fall into a hole in bash. In bash, if the local qualifier is not added, the variables are all global by default. Variable Default global-this is similar to JS and Lua, but few bash tutorials tell you the truth from the start. In a top-level scope, it is not important whether a global variable is. But inside the function, declaring a global variable can contaminate other scopes (especially if you don't notice it at all). Ty300.com technology. Therefore, it is important to remember to add the local qualifier for variables declared within a function.
6. Trap Signal

If you have written a slightly more complicated program running in the background, you should know what the POSIX standard "signal" is. If you don't know, just look at the next paragraph. Like other languages, the shell also supports processing signals. Trap Sighandler int can call the Sighandler function when the SIGINT is received. How to capture other signals and so on.

However, the main application scenario of trap is not which signal to capture. The trap command supports "capturing" many different processes--precisely, allowing the user to inject function calls into a particular process. The most common of these is the trap func exit and trap func ERR.

Trap Func exit allows you to invoke a function at the end of the script. Since the registered function can be called regardless of normal exit or abnormal exit, in the scenario where a cleanup function needs to be called, I use it to register the cleanup function instead of simply invoking the cleanup function at the end of the script.

Trap Func ERR allows a function to be called when an error is run. A common technique is to use the global variable error to store the error message and then complete the corresponding error report in the registered function based on the stored value. It is sometimes miraculous to concentrate the original fragmented error-handling logic in one place. Remember, however, that when the program exits abnormally, it calls both the function of exit registration and the function that err registers.

Manuscripts: Diligent Learning qkxue.net

Read the full version of Shell Scripting Learning Guide

Shell Scripting Learning Guide

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.