Shell script from entry to complex second (variable)

When defining variables, do not add dollar signs to the variable names, such as:

# myname=sxzhou


Notice:

There can be no spaces between the variable name and the equals sign, which may be different from any programming language you are familiar with. At the same time, the naming of variable names must follow the following rules:

The naming can only use English letters, numbers and underscores, and the first character cannot start with a number.

There can be no spaces in between, you can use an underscore (_).

Punctuation marks cannot be used.

You cannot use keywords in bash (reserved keywords can be viewed with the help command).

"=" No spaces can be left or right.


Use variables:

Variables can be used by prefixing the variable name with a $ sign, for example:

# myname=sxzhou

# echo $myname


To help the interpreter recognize variable boundaries, curly braces can be added around the variable name. This is optional.

# echo $(myname)


Read-only variable:

# myname=sxzhou


set as read-only variable

# readonly myname


If you assign another value to the variable, the prompt message read-only variable: myname will appear


Remove variables:

Use unset to delete already defined variables, such as:

# unset myname 


But you can't delete read-only variables


Variable type:

When running the shell, three variables exist at the same time:

1) Local variables Local variables are defined in scripts or commands and are only valid in the current shell instance. Programs started by other shells cannot access local variables.

2) Environment variables All programs, including programs started by the shell, can access environment variables, and some programs require environment variables to ensure their normal operation. Shell scripts can also define environment variables when necessary.

3) Shell variables Shell variables are special variables set by the shell program. Some of the shell variables are environment variables, and some are local variables. These variables ensure the normal operation of the shell.


Shell string:

Strings are the most commonly used and useful data types in shell programming (except for numbers and strings, there are no other types that are easy to use), strings can use single quotes, double quotes, or no quotes.


# myname=sxzhou

# str='$myname is cool'


The value of the output variable str is $myname is cool


apostrophe:

Restrictions for single-quoted strings:

Any character in single quotes will be output as is, variables in single quoted strings are invalid;

Single quotes cannot appear within a single quoted string (nor after using an escape character for single quotes).


Double quotes :

# myname=sxzhou

# str="$myname is cool"

The value of the output variable str is sxzhou is cool


Advantages of double quotes:

Variables can be enclosed in double quotes

Escape characters can appear in double quotes


Concatenating strings:

# myname=sxzhou

# STATUS='is cool'

# echo $myname $STATUS


The output result is sxzhou is cool


Because status is a bash keyword, it cannot be used as a variable name.


Get string length:

The following example truncates 4 characters starting from the 3rd character of the string:

# str = 'sxzhou is cool'

# echo ${str:2:4}


Note the square brackets {} here.


Shell array:

Bash supports one-dimensional arrays (multi-dimensional arrays are not supported), and there is no limit to the size of the array.

Similar to the C language, the subscripts of array elements are numbered from 0. To get the elements in the array, use the subscript. The subscript can be an integer or an arithmetic expression, and its value should be greater than or equal to 0.


Define the array:

In Shell, arrays are represented by parentheses, and array elements are separated by "space" symbols. The general form of defining an array is:

arrayname=(value1 value2 ... valuen).


E.g:

array1 = (value0 value1 value)

or

array1=(

value0

value1

value

)


It is also possible to define the individual components of the array individually:

array1[0]=value0

array1[1]=value1

array1 [n] = value


Consecutive subscripts may not be used, and there is no limit to the range of subscripts.


Read array:

The general format for reading the value of an array element is:

${ArrayName[Subscript]}


E.g:

value = $ {array1 [n]}


Use the @ symbol to get all elements in an array, for example:

echo ${array1[@]}


Shell comments

Lines starting with # are comments and are ignored by the interpreter.

There are no multi-line comments in sh, only a # sign can be added to each line.


If there is a need to annotate a large number of lines, you can enclose the part that needs to be annotated with a pair of curly braces and define it as a function. If there is no place to call this function, this part will not be executed, and it will have the effect of annotation.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325276484&siteId=291194637