How to use functions in the shell environment

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 of 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 places that need to be modified in the program.

Two, Shell function format

Function format one:


function name() {      #function是shell中的关键字,专门用来定义函数  name是函数名
  statements           #statements是函数要执行的代码,也就是一组语句;
  [return value]       #return value表示函数的返回值,其中return是shell关键字,专门用在函数中返回一个值;这一部分可以写也可以不写。
}                      #由{ }包围的部分称为函数体,调用一个函数,实际上就是执行函数体中的代码。

Function format two:

name() {
  statements
  [return value]
}

Three, function return value

  • return means to exit the function and return an exit value, which can be displayed by the $? variable in the script

Usage principles:
1. Take the return value as soon as the function ends, because the $? variable only returns the exit status code of the last command executed.
2. The exit status code must be 0~255, and the value will be divided by 256 if it exceeds

function  double {
   read -p "请输入:"  data
   return $[$data* 2]
}
double
echo $?

Insert picture description here
operation result:
Insert picture description here

Insert picture description here

  • Echo
    echo can return any string result
    is usually used to return data, for example: string value or list value
double ( ) {
read -p "请输入:" data
echo $[$data * 2]
}
double

Insert picture description here

Insert picture description here

Four, function parameter transfer

#!/bin/bash
sum() { 
s=$[$1 + $2]
echo $s     
}           
sum $1 $2   
~          

Insert picture description here
Insert picture description here

#!/bin/bash  
#!/bin/bash 
sum() {     
s=$[$1 + $2]
echo $s     
}          
read -p "请输入第一个参数:" first
read -p "请输入第二个参数:" second
sum $first $second

Insert picture description here

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 globally effective by default

  • Use the local command to limit the variable to the function

#!/bin/bash  
ak () {     
local i         #设置局部变量
 i=8        
 echo $i    
}          

              
i=9             #全局环境
ak
echo $i

Insert picture description here
Insert picture description here

Six, recursion

  • Function calls its own function
#!/bin/bash  
factorial() { 
if [ $1 -eq 1 ] 
then        
  echo 1    
else       
  local temp=$[$1 - 1]
  local result=$(factorial $temp)
echo $[$1 * $result]
fi
}
  read -p "请输入:" data
  result=$(factorial $data)

  echo $result

Insert picture description here
Insert picture description here

Seven, recursive directory

function list_files () {
for f in 'ls $1' 
do
if [-d "$1/$f" ] ;then
    echo "$2$f"               #如果是目录就输出这个目录,$2为空格是用于区分父子目录
list_files "$1/$f" " $2"        

else
    echo "$2$f"               #不是目录则输出$f结果
fi

done
}

list_files "/var/log" ""

Insert picture description here

Insert picture description here

Eight, create a library

  • Create multiple functions in one script and use them in other scripts to call the functions in the 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

#!/bin/bash
.  /root/ku.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/zhangyuebk/article/details/114643237