Secretly learn the functions of shell scripts

One, Shell function

  • The function of the function is to list the part of the code that needs to be used multiple times in the program, and then give this part of the code a name. All other repeated calls to this part of the code can only be called by this name (similar to an alias). When you need to modify this part of the repeated code, you only need to change a piece of code in the function body to implement the call modification.
  • Advantages of functions
    • Defining the same program segment as a function can reduce the amount of code in the entire program.
    • Increase the readability of the program and ease of management.
    • When modifying, you only need to modify the function, and you don’t need to find the place to be modified in the program.

Two, Shell function format

函数格式一:
function 函数名 {
    
    
  命令序列
}
函数格式二:
函数名() {
    
    
  命令序列
}

调用函数格式一:直接调用
函数名
调用函数格式一:赋值再输出
result=`函数名`
echo "想要输出的结果"

Insert picture description here

Three, function return value

1、return

  • return means to exit the function and return an exit value, which can be displayed by the $? variable in the script
  • Principle of Use
    • The return value is taken 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, and the value will be divided by 256 if it exceeds
#!/bin/bash
function  cheng {
    
    
   read -p "请输入:" shu
   return $[$shu * 2]
}
cheng
echo $?

Insert picture description here
Insert picture description here

2、echo

  • echo can return any string result
  • Usually used to return data, such as: string value or list value
#!/bin/bash
function  cheng {
    
    
   read -p "请输入:" shu
   echo $[$shu * 2]
}
cheng

Insert picture description here

Four, function parameter transfer

  • Passing parameters is to pass the parameters in the main code to the function body, generally using positional parameters $n ($1, $2, $3...)
#!/bin/bash
sum() {
    
    
s=$[$1 + $2]
echo $s
}
read -p "请输入第一个参数:" first
read -p "请输入第二个参数:" second
sum $first $second

Insert picture description here

Five, the scope of function variables

  • Functions in Shell scripts are only valid in the current Shell environment
  • Variables in Shell scripts are global by default
  • Use the local command to limit the variable to the function
#!/bin/bash
Part() {
    
    
  echo "这是内部第一个i的值:$i"
  i=10
  echo "这是内部第二个i的值:$i"
  local i=15
  echo "这是内部第三个i的值: $i"
}

i=5
echo "这是外部i的值:$i"
Part
echo "这是函数执行完i的值$i

Insert picture description here

Six, recursion

  • Function calls its own function

1. Factorial

  • The factorial of a positive integer is the product of all positive integers less than or equal to the number
fact() {
    
    
if [ $1 -eq 1 ]
then
  echo 1
else
  local temp=$[$1 - 1]
  local result=$(fact $temp)
echo $[$1 * $result]
fi
}
  read -p "请输入:" n
  result=$(fact $n)
  echo $result

Insert picture description here
Insert picture description here

2. Recursive directory

  • Display all directories and files in this directory (including directories and files in subdirectories)
#!/bin/bash
list_files () {
    
    
for f in `ls $1`
do
  if [ -d "$1/$f" ]
  then
    echo "+$2$f"
    list_files "$1/$f" " $2"
  else
    echo "-$2$f"
  fi
done
}
list_files "/var/log" ""

Insert picture description here
Insert picture description here

3. Create a library

  • To create a library is to organize the reused code together. This file does not contain the main code

  • Build library

#!/bin/bash
jiafa() {
    
    
   echo $[$1 + $2]
}
jianfa() {
    
    
   echo $[$1 - $2]
}
chengfa() {
    
    
   echo $[$1 * $2]
}
chufa() {
    
    
if [ $2 -ne 0 ];then
   echo $[$1 / $2]
else
   echo "$2不能为0"
fi
}

Insert picture description here

  • Call library operation
#!/bin/bash
.  /root/test/ku1.sh
read -p "输入第一个参数值:" first
read -p "输入第二个参数值:" second

result1=`jiafa $first $second`
result2=`jianfa $first $second`
result3=$(chengfa $first $second)
result4=$(chufa $first $second)

echo "相加结果:$result1"
echo "相减结果:$result2"
echo "相成结果:$result3"
echo "相除结果:$result4"

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51326240/article/details/111590059