Functions in shell scripts

Function definition

Variables are given a value by us to use. A function is like a shortcut key. After we define a function, we can use the function we set by referencing its corresponding function name.

Function creation and use

code show as below:

#!/bin/bash
function functionname(){
    
          ##function可以不写,functionname
							是这个函数的名字,使用时就是引用这个名字
	echo "haha"
	echo "heihei"
	echo "hello"
}
##函数定义格式 function functionname(){
    
    },花括号里输入我们需要的命令
##小括号里不需要写东西

functionname               ##调用函数,注意调用函数一定要在函数定义
							后才可以

Try to execute this script

[root@haha ~]# sh test.sh 
haha
heihei
hello

We can see that multiple commands achieve the desired effect by calling the function name

The return value of the function

By default, the exit status of a function is the exit status returned by the last command of the function

#!/bin/bash
function functionname(){
    
         
	echo "haha"
	echo "heihei"
	echo "hello"
	ls -l inexistent.txt                ##查看一个不存在的文件
}
functionname
echo "function's status:$?"

Execute this script

[root@haha ~]# sh test.sh 
haha
heihei
hello
ls: cannot access inexistent.txt: No such file or directory
function's status:2

We can see that the function executes three commands successfully, the last command reports an error, and the status value is not 0 to
change the position of the command in the function

#!/bin/bash
function functionname(){
    
         
	ls -l inexistent.txt          
	echo "haha"
	echo "heihei"
	echo "hello"
}
functionname
echo "function's status:$?"

Go to execute the script

[root@haha ~]# sh test.sh 
ls: cannot access inexistent.txt: No such file or directory
haha
heihei
hello
function's status:0

After changing the position, the status value is 0, so by default, the exit status of the function is the exit status returned by the last command of the function

return command

return is the Shell keyword, specifically used to return a value in a function

#!/bin/bash
function functionname(){
    
         
   echo "haha"
   echo "heihei"
   echo "hello"
   return    111         ##试着用下return
   ls -l inexistent.txt      
}
functionname
echo "function's status:$?"
[root@haha ~]# sh test.sh 
haha
heihei
hello
function's status:111

After verifying return, it is not difficult to see that return not only returns an exit value to the program that called the function, but also exits the function execution. If the error message of ls is not displayed, it means that it has not been executed.

Assign function to variable use

#!/bin/bash
functionname(){
    
    
	read -p "input a value:" VAR
	echo $[$VAR * 5]
}
NUM=$(functionname)           ##将函数赋值给变量
echo "current num is $NUM"      ##打印这个变量
[root@haha ~]# sh test.sh 
input a value:3
current num is 15

Pass parameters to the function

The first case: pass parameters inside the script

#!/bin/bash
functionname() {
    
    
    if [ $# -eq 0 ] || [ $# -gt 2 ]
    then
        echo "错误,需要一个或者两个位置参数"
    elif [ $# -eq 1 ]
    then
        echo $[ $1 + $1 ]
    else
        echo $[ $1 + $2 ]
    fi
}


echo -n "用两个参数的结果是:"
value=`functionname 100 200`     ##在脚本中用类似输入位置变量的形式
													对函数functionname传递参数
echo $value

echo -n "用一个参数的结果是: "
value=`functionname 100`
echo $value

echo -n "没有参数的结果是:"
value=`functionname`
echo $value

echo -n "用三个参数的结果是:"
value=`functionname 100 200 300`
echo $value
[root@haha ~]# sh test.sh 
用两个参数的结果是:300
用一个参数的结果是: 200
没有参数的结果是:错误,需要一个或者两个位置参数
用三个参数的结果是:错误,需要一个或者两个位置参数

The second case: manually transfer parameters in the form of position variables

#!/bin/bash
functionname(){
    
    
	echo $[ $1 * $2 ]     ##这里的$1,$2引用脚本中的位置参数
     echo $3
}

if [ $# -eq 2 ]
then
	value=`functionname $1 $2 haha`    ##这里的$1$2引用外面的位置
	                                   参数 ,haha传给函数中的$3
	echo "The result is $value"
else
	echo "Usage:$0 a b"
fi
[root@haha ~]# sh test.sh 
Usage:test.sh a b
[root@haha ~]# sh test.sh 3 
Usage:test.sh a b

[root@haha ~]# sh test.sh 3 6
The result is 18
haha

to sum up

The reference of the function can enable us to achieve the script effect we need in a concise and beautiful language in a complex script

Guess you like

Origin blog.csdn.net/weixin_52441468/article/details/112284159