1, Functions
1.1 function
A statement block is defined as a function approximately equal to an alias, a function is defined, and then a function is referenced
Encapsulated reusable code with specific functions
1.2 basic format of function
Method I: [function] Function name (){ Command sequence [return x] #Use return or exit to explicitly end the function } Method 2: Function name(){ Command sequence }
1.3 function considerations
1. call directly in write function
2 function directly writes the function name, and the function with the same name takes effect
3. The calling function must be defined first
4. As long as other functions called are defined first, the definition order is irrelevant
1.4 method of function call
Example 1:
#!/bin/bash h () { echo "hello" } w () { echo "world" } nihao () { h w } h w nihao
Example 2:
#Define function #!/bin/bash os (){ if grep -i -q "CentOS Linux 7 " /etc/os-release then echo "This operating system is centos 7" elif grep -i -q "CentOS Linux 6 " /etc/os-release then echo "This operating system is centos 6" elif grep -i -q "CentOS Linux 8 " /etc/os-release then echo "This operating system is centos 8" fi } #Call function os
1.5 return value of function
Return means to exit the function and return an exit value. You can use $? The variable represents the value
Principle of using function
Take the return value as soon as the function ends, because $? The variable only returns the exit status code of the last command executed;
The exit status code must be 0 ~ 255. When it exceeds, the value will be divided by 256.
Example:
#!/bin/bash user () { if [ $USER = root ] then echo "This is the administrator user" else echo "This is not an administrator" return 1 fi { user
1.6 parameter transfer of function
In Shell, when you call a function, you can pass parameters to it. Inside the function body, the value of the parameter is obtained in the form of $n. for example, $1 represents the first parameter, $2 represents the second parameter... That is, the position parameter is used to pass the parameter.
Example:
#!/bin/bash sum () { echo "First variable:" $1 echo "Second variable:" $2 let n=$1+$2 echo $n } sum $2 $1
1.7 calling functions externally
To call a command externally, you need to source it first. Then call the function and call the variables in the function.
Example 1:
[root@localhost data]#vim function.sh #!/bin/bash color () { RED="echo -e \E[31m" GREEN="echo -e \E[32m" END="\E[0m" }
Example 2: invoke in script;
1.8 scope of function variables
Functions in shell scripts are only valid in the current shell environment
Variables in shell scripts are valid globally by default
Restrict variables to functions and use the local command
Example 1:
#!/bin/bash aa () { a=10 } a=20 aa echo $a
Example 2:
#!/bin/bash aa () { local a=10 } a=20 aa echo $a
1.9 recursion of function
Function calls its own function
Example: Factoring any number
#!/bin/bash fact () { #Judge the parameter passed in. If it is 0, it will directly output 1 if [ $1 -eq 0 -o $1 -eq 1 ] then echo 1 else #If the passed in parameter is not 1, the function calls itself and the passed in parameter is calculated echo $[$1* $(fact $[$1-1])] fi } fact $1 3
2, Array
2.1 definition of shell array
Multiple values can be stored in the array. Bash Shell only supports one-dimensional arrays (multi-dimensional arrays are not supported)
Subscripts of array elements start with 0.
Shell arrays are represented by parentheses, and elements are separated by a "space" symbol
In shell statements, when using and traversing arrays, the array format should be written as ${arr [@]} or ${arr [*]}
2.2 method of defining belonging group
Method I:
Array name=(value1 value2 ... valuen) [root@localhost data]#a=(1 2 3 4 5) [root@localhost data]#echo ${a[@]} 1 2 3 4 5
Method 2:
Array name=([0]=value0 [1]=value0 [2]=value0 ...) [root@localhost data]#b=([0]=1 [1]=2 [2]=3 [3]=4 [4]=5) [root@localhost data]#echo ${b[@]} 1 2 3 4 5
Method 3:
Array name[0]="value" Array name[1]="value" Array name[2]="value [root@localhost data]#c[0]=1 [root@localhost data]#c[1]=2 [root@localhost data]#c[3]=3 [root@localhost data]#c[4]=4 [root@localhost data]#echo ${c[@]} 1 2 3 4
2.3 data type of array
value type
Character type: defined with '' or ''
Example:
#Arrays can be numeric [root@localhost ~]#a=(1 2 3 4 5);echo ${a[@]} 1 2 3 4 5 #Arrays can be mixed [root@localhost ~]#a=(a 1 2 apple);echo ${a[@]} a 1 2 apple
2.4 get the data list of the array
echo ${Array name[*]} echo ${Array name[@]}
Example:
2.5 get array length
echo ${#Array name [*]} echo ${#Array name [@]}
Example:
2.6 get the value corresponding to the array subscript
Array name=(Element 0 element 1 element 2...) Define array echo ${Array name[Index value]} Output the element corresponding to the array index value, which starts from 0
Example:
2.7 common operations of arrays
2.7.1 traversal of array
#!/bin/bash a=(1 2 3 4 5 6) for i in ${a[@]} do echo $i done
2.7.2 array slicing
a=(0 1 2 3 4 5 6 7 8) echo "Output entire array: " ${a[@]} echo "Fetch arrays 1 to 3: " ${a[@]:1:3} echo "Take out the array 5 to all the following elements: " ${a[@]:5:5}
2.7.3 array replacement
#Temporary replacement [root@localhost data]#echo ${a[@]/4/6} #Reassignment, which can be permanently modified [root@localhost data]#a=(${a[@]/4/7})
2.7.4 array deletion and value deletion of specified subscript
2.7.5 adding elements to array
Method 1: directly use subscripts to append elements
Array name[subscript]=variable
Method 2: add elements by taking the length of the array as the subscript
Method 3: use + = to add
Array name+=(Variable 1 variable 2 ...)
2.7.6 view all arrays - declare -a
3, Bubble sorting
#!/bin/bash read -p "Please enter a string of numbers:" num a=($num) for ((i=1;i<${#a[*]};i++)) do for ((j=0;j<${#a[*]}-i;j++)) do if [ ${a[$j]} -gt ${a[ $j + 1]} ] then tmp=${a[$j + 1]} a[ $j + 1]=${a[$j]} a[$j]=$tmp fi done done echo ${a[*]}