Article Directory
One: shell function
The essence of the Shell function is a piece of script code that can be reused. This code is written in advance, placed in a designated location, and can be called directly when used.
The functions in Shell are similar to functions in other programming languages such as C++, Java, Python, C#, but differ in the details of the syntax.
The syntax format of Shell function definition is as follows:
function name() {
#function是shell中的关键字,专门用来定义函数 name是函数名
statements #statements是函数要执行的代码,也就是一组语句;
[return value] #return value表示函数的返回值,其中return是shell关键字,专门用在函数中返回一个值;这一部分可以写也可以不写。
} #由{ }包围的部分称为函数体,调用一个函数,实际上就是执行函数体中的代码。
1.1: Simplified writing of function definition
If you find it troublesome, you can omit the function keyword when defining the function:
name() {
statements
[return value]
}
If you write the function keyword, you can also omit the parentheses after the function name:
function name {
statements
[return value]
}
I recommend using standard writing so that no matter who writes this script, you can understand it.
[root@localhost opt]#vim jia.sh
#!/bin/bash
sum () {
s=`expr 5 + 3`
echo $s
}
sum
[root@server1 ~]# sh a.sh
8
sum () {
s=`expr $1 + $2`
echo $s
}
sum 5 6
[root@server1 ~]# sh a.sh
11
在这里插入代码片
sum () {
return $(($1 + $2))
}
sum 3 4
echo $?
[root@server1 ~]# sh a.sh
7
function sum(){
#命令序列
read -p "请输入第一个整数: " num1
read -p "请输入第一个整数: " num2
SUM=$[$num1+$num2]
echo "和:$SUM"
}
sum
[root@server1 ~]# sh a.sh
请输入第一个整数: 5
请输入第一个整数: 4
和:9
function sum(){
#命令序列
read -p "请输入第一个整数: " num1
read -p "请输入第一个整数: " num2
SUM=$[$num1+$num2]
echo $SUM
}
tom=`sum 4 2`
let tom+=1
echo $tom
[root@server1 ~]# sh a.sh
请输入第一个整数: 6
请输入第一个整数: 3
10
1.2: The scope of the function
- Functions in Shell scripts are only valid in the current Shell environment
- Variables in ShelI script are globally effective by default
- Use the local command to limit the variable to the function
Example
-
The internal variables of the function are implemented by local
-
By defining the myfun function, set the local variable i inside it
-
Assign values inside and outside the function to verify the results
function sh(){
#命令序列
read -p "请输入第一个整数: " num1
read -p "请输入第一个整数: " num2
s=$[$num1+$num2]
jerry=20
echo 函数内$jerry
echo 和:$s
}
sh
echo 函数外$jerry
[root@server1 ~]# sh a.sh
请输入第一个整数: 2
请输入第一个整数: 3
函数内20
和:5
函数外20
local defines global variables
function sh(){
#命令序列
read -p "请输入第一个整数: " num1
read -p "请输入第一个整数: " num2
s=$[$num1+$num2]
local jerry=20 #local定义全局变量
echo 函数内$jerry
echo 和:$s
}
sh
echo 函数外$jerry
[root@server1 ~]# sh a.sh
请输入第一个整数: 2
请输入第一个整数: 4
函数内20
和:6
函数外
1.3: Recursive function
Call your own function
Example
Recursively traverse the directory
Realize by defining the recursive function list_files
#!/bin/bash
function list_files(){
for s in `ls $1`
do
if [ -d "$1/$s" ]
then
echo "$2$s"
list_files "$1/$s" "$2"
else
echo "$2$s"
fi
done
}
list_files "/var/log"
#$1表示/var/log
#$s表示/var/log下一个目录
#$2表示指定格式 子目录一定要做一个首行缩进
[root@server1 ~]# sh bianli.sh
amanda
anaconda
anaconda.log
ifcfg.log
journal.log
ks-script-j10KNj.log
ks-script-mW8ioG.log
packaging.log
program.log
……
Two: shell array
2.1: Application scenarios include
- Application scenario package length
- Get array length
- Get element length
- Iterate over elements
- Element slice
- Element replacement
- Element deletion
- …
2.2: Description of the array
Array: Put the same type of data collection. There are many
application scenarios: the grades and names of
classmates [11,22,33,44] Open up a continuous space in the memory
Use the
array name arr arr=(44) ,33,22,11)
Array elements: 44 33 22 11 are all
Length of the array: arr has 4 elements
Array subscript: 33 elements are below 1 (for example, 11 subscript is 3 22 subscript is 2)
for temporary Scalar in array
do
done
2.3: Array definition method
method one
数组名=(shuai0 shuai1 shuai2...)
Method Two
数组名=([0]=shuai [1]=shuai [3]=shuai...)
[root@server1 test]# num=([0]=44 [1]=33 [2]=22 [3]=11)
[root@server1 test]# echo ${num[*]}
44 33 22 11
Method Three
列表名="shuai0 shuai1 shuai2.."
数组名= ($列表名)
[root@server1 test]# tom=" 1 2 3 4 "
[root@server1 test]# num=($tom)
[root@server1 test]# echo ${num[*]}
1 2 3 4
#!/bin/bash
list=`cat test.txt`
echo $list
[root@server1 test]# sh test.sh
5 6 7 8
#!/bin/bash
list=`cat test.txt`
f=($list)
echo ${
f[*]}
[root@server1 test]# sh test.sh
5 6 7 8
Method three is
often done is to replace an element
数组名[0]="shuai"
数组名[1]="shuai"
数组名[2]="shuai"
[root@server1 test]# num=([0]=2 [1]=5 [2]=6 [3]=8)
[root@server1 test]# echo ${num[*]}
2 5 6 8
[root@server1 test]# num[2]=10
[root@server1 test]# echo ${num[*]}
2 5 10 8
2.3.1: to 100 array
#!/bin/bash
for ((i=0;i<99;i++))
do
tom[$i]=$[$i+1]
done
echo ${
tom[*]}
[root@server1 test]# sh 1_100.sh
1 2 3 4 5 6 7 8 9 ……
2.3.2: Create odd arrays from 1 to 100
#!/bin/bash
for ((i=0;i<99;i+=2))
do
tom[$i]=$[$i+1]
done
echo ${tom[*]}
[root@server1 test]# sh 1_100.sh
1 3 5 7 9 11 13 15 ……
Method Two
#!/bin/bash
k=0
j=1
for ((i=0;i<=100;i++));do
k=$[ $i + $j ]
let j++
if [ $k -le 100 ];then
tom[$i]=$k
fi
done
echo ${
tom[@]}
[root@server1 test]# sh 1_100.sh
1 3 5 7 9 11 13 ……
2.3.3: Create any number and length, add elements according to customer needs
#!/bin/bash
i=0
while true
do
read -p "是否输入元素(yes/no):" e
if [ $e == "no" ];then
break
fi
read -p "请输入第$[$i+1]个元素:" f
tom[$i]=$f
let i++
done
echo ${
tom[@]}
[root@server1 test]# sh 1_100.sh
是否输入元素(yes/no):yes
请输入第1个元素:10
是否输入元素(yes/no):yes
请输入第2个元素:20
是否输入元素(yes/no):yes
请输入第3个元素:30
是否输入元素(yes/no):no
10 20 30
2.4.1: Get array length and subscript assignment
[root@server1 test]# tom=(66 77 88)
[root@server1 test]# echo ${
tom[@]}
66 77 88
[root@server1 test]# echo ${
tom[1]}
77
2.4.2: The following set of values, add to 100 if it is less than 80
#!/bin/bash
num=(56 79 63 32 88)
#遍历数组
for ((i=0;i<${
#num[*]};i++))
do
if [ ${
num[$i]} -lt 80 ];then
new[$i]=100
else
new[$i]=${
num[$i]}
fi
done
echo ${
new[*]}
[root@server1 ~]# sh shell.sh
100 100 100 100 88
2.4.3: Find the maximum value of the array
#!/bin/bash
num=(12 63 43 52)
tom=0
for ((i=0;i<${
#num[*]};i++))
do
if [ ${
num[$i]} -gt $tom ];then
tom=${
num[$i]}
fi
done
echo $tom
[root@server1 ~]# sh max.sh
63