Functions and arrays of shell scripts

a function

1.1 The function of the function

  • The statement determines that defining a function is approximately equal to an alias, defining a function, and then referencing a function
  • Encapsulated reusable code with specific functionality

1.2 The basic format of the function

 方法一:
 【function】函数名 (){
         命令序列
         【return  x】            #使用return或者exit可以显示的结束函数
      }
     方法二:
     函数名(){
          命令序列
       }

1.3 Precautions for functions

1. Directly write the call in the function
2. The function directly writes the function with the same name as the function, and the first one takes effect
3. The calling function must be defined first
4. As long as the calling function is defined first

1.4 Method of function call

Example 1:
insert image description here
insert image description here
Example 2:

insert image description here

1.5 The return value of the function

  • return means to exit the function and return an exit value, $? can be used in the script variable representing the value

  • Function usage rules

    • The return value is negated as soon as the function ends, because the $? variable only returns the exit status of the last command executed
    • The exit status code must be 0~255, and the value will be divided by 256 to get the remainder

Example:

insert image description here

1.6 Function parameter passing

In the shell, parameters can be passed to functions when they are called. Inside the function body, the value of the parameter is obtained in the form of $n, for example, $1 represents the first parameter, and $2 represents the second parameter... that is, the positional parameter is used to achieve parameter passing

example

insert image description here

1.7 Calling functions externally

To call a command externally, you need to source it first. Then call the function, and then call the variable in the function

Example
insert image description here
Example 2: Calling in a script

insert image description here

1.8 Scope of Function Variables

  • Functions in shell scripts are only valid in the current shell environment
  • Variables in shell scripts are globally valid by default'
  • Limit variables inside the function using the local command

Example:

insert image description here
Example 2:
insert image description here

1.9 Recursion of functions

The function calls its own function
Example: find the factorial of any number

insert image description here

2. Array

2.1 Definition of shell array

  • Multiple values ​​can be stored in an array. The bash shell only supports one-dimensional arrays
  • The subscript of the data element starts from 0
  • Shell arrays are represented by parentheses, and elements are separated by "space" symbols
  • In the shell statement, when using and traversing the array, the array format should be written as arr [ @ ] or {arr[@]} ora rr [ @ ] or {arr[*]}

2.2 The way to define the group

method one;

insert image description here
Method Two:
insert image description here

2.3 Data types of arrays

  • value type
  • Character type: use "" or '' to define

Example:
insert image description here

2.4 Get the data list of the array

Example:
insert image description here

2.5 Get group length

insert image description here

2.6 Get the corresponding value under the array

insert image description here

2.7 Common operations of arrays

2.7.1 Array traversal

insert image description here

2.7.2 Array replacement

insert image description here

2.7.3 Array deletion and specified subscript value deletion

insert image description here

2.7.4 Adding elements to an array

Method 1: Directly use the subscript to add elements
insert image description here
Method 2: Use the length of the array as the subscript to add elements

insert image description here
Method 3: Use + to append
insert image description here

2.7.5 View all arrays –declare -a

insert image description here

3. Bubble sort

insert image description here

Guess you like

Origin blog.csdn.net/fyb012811/article/details/132304732