Learning bash notes-Basic shell programming

Source: Internet
Author: User
1. shell scripts and function scripts are files that contain shell commands. They are a shell program and run in three ways: one is to type source scriptname to read and run the script commands, just like typing them. Second, run bash scriptname and open a sub-shell to read and execute commands in the script file. The script file does not have the "execution permission ". Third, use./scriptname to open a sub-shell to read and execute commands in the script file. This script requires "execution permission ".
1. A function is a script in which you define shell code by name and save it in Shell memory for future calls and running. You can define a function in either of the following formats: function funcname {shell commands} Or funcname () {shell commands. You can use the unset-F funcname command to delete a function definition.
Type the declare-F command to find the function defined in the logon session. This command prints the function name and function definition. If you only need to print the function definition, you can use the declare-F command.
When you type a command in shell, the priorities of various resources are as follows: 1. alias 2. keyword, for example, fucntion3. function 4. shell built-in commands, such as CD and type5. executable programs of the script. Shell searches for the Directory squadron listed in the PATH environment variable. You can use the built-in command, builtin, and enable to change the priority order, which will be discussed later.
You can use the type command to view the specific details of the command: if you use the type command, search for the command information in the order of priority above, and find one of them and it will end. If type-all command is used, the information of the command is queried based on the priority order above, and information of all resources is displayed. If type-type command is used, a single word descriptor is output: keyword, function, built-in command or file.


2. Shell variable 2. 1. Location Parameter

You can use the statement varname = value to define the variable value. For example:

Hatter = mad
Echo "$ hatter"
Mad

The most important built-in variables are location parameters. When a script is called, they save the command parameters of the script. The location parameter is named 1, 2, 3, and the value is represented by $1, $2, $3.

There is also a location parameter 0, whose value is the script name.

There are also two special variables that contain all the location parameters (out of the location parameter 0): * and @. Their differences are not obvious but important, and they are only reflected in double quotation marks.

"$ *" Is a single string containing all parameter locations, separated by the first character in the environment variable IFS (internal domain separator, internal field seperator. IFS is

Space, tab, and newline. On the other hand, "[email protected]" is equivalent to "$1" "$2 ".... "$ N", here n is the number of location parameters, equivalent to N separate double quotes separated by Spaces

String. If there is no location parameter, "[email protected]" is blank.

The last "$ #" is the number of parameters.


Run the following script Params for testing:

Echo "[email protected]"
Echo "$ *"
Echo "$0 $1 $2 $3"
Echo "$ #"

The execution result is:

./Params 1 2 3
1 2 3
1 2 3
./Params 1 2 3
3


2. Parameters of the position in the function

Shell functions use location parameters and special variables, such as * and #, in the same way as shell scripts. In typical cases, several Shell functions must be defined in a shell script,

Therefore, each function must process its own parameters, indicating that each function must track the location parameters separately. Each function has a copy of these variables, which we call them

The function is local.

However, other variables defined in the function are not local, and their values are known in the shell scripts.

Use the following script for testing:

Func_a ()
{
Echo "$0, $1, $2"
Var1 = "var1 in func_a"
Echo "var1: $ var1"
}

Var1 = "var1 out func_a"
Echo "var1: $ var1"
Func_a arg1 arg2
Echo "var1: $ var1"
Echo "$0, $1, $2"

Run the script:

$./Var. Sh 1 2

Var1: var1 out func_a
./Var. Sh, arg1, arg2
Var1: var1 in func_a
Var1: var1 in func_a
./Var. Sh, 1, 2

Var1 defined outside the function is overwritten by the value assignment in the function.


2. 3. Local variables in the function

The local definition in the function definition makes all the variables involved are the local variables of the function. Modify the above script as follows:

Func_a ()
{
Echo "$0, $1, $2"
Local var1 = "var1 in func_a"
Echo "var1: $ var1"
}

Var1 = "var1 out func_a"
Echo "var1: $ var1"
Func_a arg1 arg2
Echo "var1: $ var1"
Echo "$0, $1, $2"

Execution result:

$./Var. Sh 1 2

Var1: var1 out func_a
./Var. Sh, arg1, arg2
Var1: var1 in func_a
Var1: var1 out func_a
./Var. Sh, 1, 2

In this case, var1 in the function becomes a local variable.


2. 4. Reference [email protected] and $ *

Why does "$ *" Use the first character of IFS instead of space to separate it? In this case, the output is flexible. The following is a simple example to print the location parameters separated by commas (,).

List, script:

Ifs =,
Echo "$ *"

The running result is:

$./A. Sh 1 2 3
1, 2, 3

Why is "[email protected]" used as n Independent double quotes to reference strings? The purpose is to allow you to use them again.


. Explanation of variable syntax

The $ varname syntax for getting a variable value is actually a simplified form of the common syntax $ {varname.

There are two syntaxes. One reason is that if the code references the extra nine location parameters, you must use $ {10} instead of $10.

Use the following script for testing:

Echo "$0 $1 $2 $3 $4 $5 $6 $7 $8 $9 $10 $20"

Running result:

$./Params 1 2 3 4 5 6 7 8 9 0

./Params 1 2 3 4 5 6 7 8 9 10

$10 is interpreted as $1 followed by a character 0, so $10 is 10.

Consider the following situation if you want to underline the user ID.

Echo $ uid _

Shell tries to use uid _ as the variable name. $ {Uid} _ must be used }_


3. String operations

Replacement OPERATOR:

$ {Varname:-word}

If varname exists and is not null, its value is returned; otherwise, word is returned.

$ {Varname: = word}

If varname exists and is not null, return its value. Otherwise, set it to word and return its value. You cannot set the location parameter or special DDH as follows.

$ {Varname: =? Message}

If varname exists and is not null, return its value. Otherwise, print varname: Message and release the current command or script. If message is omitted, the default value is generated.

Information: parameter null or not set.

$ {Varname: + word}

If varname exists and is not null, word is returned; otherwise, null is returned.

$ {Varname: Offset}

$ {Varname: offset: length}

Execute string extension. Returns a substring starting from offset with the length of $ varname. If the length is omitted, the string starts from offset until $ varname

End. If varname is @, Length indicates the number of parameter positions starting from offset.


Pattern Matching OPERATOR: (the pattern may contain any character string for character setting and range *,? And [])

$ {Variable # pattern}

If the pattern matching variable starts with the value, delete the shortest matching part and return the remaining part.

$ {Variable # pattern}

If the pattern matching variable starts with the value, delete the longest matching part and return the remaining part.

$ {Variable % pattern}

If the pattern matching variable value ends, the shortest matching part is deleted and the rest part is returned.

$ {Variable % pattern}

If the pattern matching variable ends with the value, delete the longest matching part and return the remaining part.

$ {Variable/pattern/string}

$ {Variable // pattern/string}

Replace the longest part of the matching pattern in variable with a string. In the first format, only the first matching part is replaced, and in the second format, all matching parts are

Replace. If the mode starts with #, it must match the beginning of variable. If it starts with %, it must match the end of variable. If string is null, the matching part is deleted.

If variable is @ or *, the operation is applied to each location parameter in turn and expanded to the result list.


Length OPERATOR:
$ {# Varname} returns the length of the variable string.


4. Command replacement

Command replacement allows you to use the standard output of the command, just as it is a variable value. Syntax for command replacement:
$ (Command)

Run the command in parentheses. The command is written to the standard output and is returned as the expression value.

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.