Shell notes (2)

Source: Internet
Author: User

> Array

Names = (max Helen Sam Zach)

Names [*] extracts all data and returns it as an element.

Names [@] Retrieves all data, but returns an array, which can be used to assign values to the array.

> Export

Make the variables of the parent process visible and available to the child process. Once a variable is used as a parameter in the expert, the shell will pass the value of the variable to the sub-process to obtain the call environment.Value Transfer callEach sub-process obtains a copy of the variable when using the variable.

Eg. Export cheese = American

Or cheese = American

Export cheese

> Typeset

In the function, partial variables are modified with typeset to avoid duplicate names.

------------------------------------------------Common special parameters-----------------------------------------------

>$ $

PID of the currently running process

> $!

$! Saves the PID of the process running in the background

>$?

The exit status of the process, or the return status code of the Command run, can be detected by $ ?, Judge the running result

>$ #

Number of parameters except commands on the command line

> $0

RunProgramCommand name

> $1 ~ $ N

Command Line Parameters

> $ *

The list of command line parameters, including all command line parameters. $ * Is a parameter, that is, all command line parameters are output as an element.

> @

Similar to $ *, the list of busy command parameters is also returned. The difference is that $ @ returns a list, a group of elements, and the command line parameters are returned as a list, each command line parameter is independent.

Note: $ * is different from $ @ when "" is added.

---------------------------------------------------------------------------------------------------------------------

> Set

Initialize command line parameter variables. Set assigns one or more parameters after the set parameter to the location parameter.

> Read

The built-in command reads user input and stores the content in the variable specified by the user. Eg. Read filename

If you do not want to specify a variable to save the read input content, you can: Read-p "Input :". Then, the read content is saved to the $ reply variable. Echo "$ reply" can view the read content. The option "-P" is used to display User prompts. "Input:" is only an input prompt, which can be "", but cannot be absent.

Read-P "prompt" can be followed by a variable to access the read content, or without a variable. The read value is obtained through $ reply.

It can also be separated from multiple variables by spaces during read input.

If the number of input variables is less than the number specified by the script, the unassigned variables are empty:

If the number of input variables exceeds the number specified by the script, the extra input values are used as the content of the last variable:

 

> Use read to read files

Note: At the end of the while statement, that is, after the done statement, use the redirection "<" to input the file name to be scanned.

> Exec

There are two main purposes: 1. Execute a command without creating a new process; 2. Redirect the file descriptor from inside the shell script

>/Dev/tty

Indicates the user's working screen.

Messages sent to/dev/tty devices are not transferred (standard output and error output from scripts are not redirected ). Even if the standard output redirection is used, the data sent to/dev/tty is displayed as usual.

> Trap

Capture the signal generated when the script is running, so that you can take measures even after receiving the signal.

Format: Trap ['commads'] [Signal]

After the signal is captured, run commands. To shield some signals, commands is null ''.

6 commonly used signals: 1 (suspended), 2 (CTRL + C interrupted), 3 (exited), 9 (ended), 15 (software interrupted ), 20 (stop Ctrl + Z)

When multiple signals are blocked, separate them with spaces. Eg. Trap ''1 2 3 15

Trap can be used to capture signals during script running or to clean up after running. Eg.

> Getopts

With this tool, you can configure options for the script you have written.

In my opinion, getopts is a tool called in shell, specifically used to process $1 ~ $ N, saving the need for manual parsing of $ *, will be $1 ~ $ N is input to getopts, and $0 ~ After $ n is parsed, you can directly use the getopts output result.

See the following example:

Use format of getopts: getopts optstring varname

In the example, the option string processed by getopts is BT: UW:, starting with ":", indicating that the script is responsible for generating error information, that is, shielding the error output of getopts.

T and W are followed by ":", indicating that the two options require parameters.

Getopts processes option strings in a loop-like manner and uses two special variables optind (option index) and optarg (option parameter ). Optind starts from 1. Each time getopts finds a parameter, optind adds 1.

Yes. As you can see, getopts reads $0 ~ $ N all: Because the optind of-B is 2.

When getopts finds an invalid option, set varname ?, Concurrent optarg is set as option letter;

When getopts finds that a parameter is missing, it sets varname to: And optarg to the option that lacks the parameter.

If you ignore: starting with optstring, The varname is set? : If the optarg variable is not set, getopts will write its diagnostic information to the standard output.

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.