Shell function knowledge points

1. Shell function

1. Write the command sequence together in a format
2. It is convenient to reuse the command sequence

Two, Shell function definition

method one:

function 函数名 {
    命令序列
}

Way two:

函数名 () {
      命令序列
}

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. Get 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 when it exceeds More than

Example:
Method 1:

[root@gcc jiaoben1]#vim test3.sh
#!/bin/bash
function abc {                           #使用function进行函数定义
   read -p "请输入:" a
   a=$[$a*2]
   return $a                             #return表示退出函数并返回一个退出值,脚本中可以用 $? 变量显示该值
}
abc
echo $?                                  #退出状态码必须是0-255,超出时值将为除以256取余
---------------------------------------------------------------------------
[root@gcc jiaoben1]#chmod +x test3.sh 
[root@gcc jiaoben1]#./test3.sh 
请输入:3
6

Way two:

[root@gcc jiaoben1]#vim test4.sh

#!/bin/bash
abc () {                              
    read -p "请输入:" a
    a=$[$a*2]
    echo $a
}
abc                                            #直接使用函数名进行运算
或者
result='abc'
echo $result
----------------------------------------------------------------
[root@gcc jiaoben1]#./test4.sh 
请输入:400
800

Four, function parameter transfer

In Shell, you can pass parameters to a function when calling 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 use of positional parameters to achieve parameter transfer.

method one:

[root@gcc jiaoben1]#vim test5.sh
#!/bin/bash
sum1 () {
   sum=$[$1 + $2]
   echo $sum
}

read -p "请输入第一个参数:" first
read -p "请输入第二个参数:" second
sum1 $first $second
----------------------------------------------------------------------------
[root@gcc jiaoben1]#chmod +x test5.sh 
[root@gcc jiaoben1]#./test5.sh 
请输入第一个参数:2
请输入第二个参数:3
5

Way two:

[root@gcc jiaoben1]#vim test5.sh
#!/bin/bash
sum1 () {
   sum=$[$1 + $2]
   echo $sum
}

sum1 $1 $2
---------------------------------------------------------------------------
[root@gcc jiaoben1]#./test5.sh  10 20
30

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.
Limit variables to functions. Use local commands.

[root@gcc jiaoben1]#vim test5.sh
#!/bin/bash
abc () {
   a=5
   b=6
   echo "c等于$c"
}

a=8
c=9
abc
echo "a等于$a"
echo "b等于$b"
---------------------------------------------------------------
[root@gcc jiaoben1]#./test5.sh
c等于9
a等于5
b等于6
[root@gcc jiaoben1]#vim test5.sh
#!/bin/bash
abc () {
   local i
   i=8
   echo "inside $i"
}

i=9
abc
echo "outside $i"
------------------------------------------------------------------------
[root@gcc jiaoben1]#./test5.sh
inside 8
outside 9
[root@gcc jiaoben1]#vim test5.sh
#!/bin/bash
abc () {
   echo "inside1 $i"
   local i
   i=8
   echo "inside2 $i"
}

i=9
abc
echo "outside $i"
----------------------------------------------------------
[root@gcc jiaoben1]#./test5.sh
inside1 9
inside2: 8
outside 9
[root@gcc jiaoben1]#vim test5.sh
#!/bin/bash
abc () {
   echo "inside1 $i"
   let i++
   local i
   i=8
   echo "inside2: $i"
}

i=9
abc
echo "outside $i"
----------------------------------------------------------------
[root@gcc jiaoben1]#./test5.sh
inside1 9
inside2: 8
outside 10

Six, recursion

Function calls its own function

1. Factorial

[root@gcc jiaoben1]#vim test6.sh
#!/bin/bash
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"
-----------------------------------------------
[root@gcc jiaoben1]#./test6.sh 
请输入阶乘数:3
6

2. Recursive directory

[root@gcc jiaoben1]#vim test7.sh
#!/bin/bash
function 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" ""

Seven, function library

The function library only contains the definition of the function, and the script contains both the definition of the function and the executable code.

[root@gcc jiaoben1]#vim test8.sh
#!/bin/bash
jiafa () {
  result=$[$1 + $2]
  echo $result
}

jianfa () {
   result=$[$1 - $2]
   echo $result
}

chengfa () {
   result=$[$1 * $2]
   echo $result
}
chufa () {
    if [ $2 -ne 0 ]
    then
        result=$[$1 / $2]
        echo $result
    else
      echo "$2不能等于0!"
    fi
}

----------------------------------------------------------------------------
[root@gcc jiaoben1]#vim test9.sh
#!/bin/bash
. /opt/jiaoben1/test8.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
--------------------------------------------------------
[root@gcc jiaoben1]#chmod +x test9.sh 
[root@gcc jiaoben1]#./test9.sh  
输入第一个参数值:20
输入第二个参数值:10
30
10
200
2

Guess you like

Origin blog.csdn.net/Gengchenchen/article/details/111564071