Shell-basic functions

One, Shell function

1. Function

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

2. Definition

Format one

function 函数名 {
    命令序列
}
#!/bin/bash

function a {

read -p "请输入:" b
   echo $[$b * 3]

}
c=`a`
echo $c

Insert picture description here
Insert picture description here

Format two

函数名 () {
 命令序列
}
#!/bin/bash

a() {
read -p "请输入:" b
echo $[$b * 2 - 1]
}
c=`a`
echo $c

Insert picture description here
Insert picture description here

Second, the function return value

return means to exit the function and return an exit value. The $? variable can be used in the script to display the value
. Principles of use:
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, the value will be divided by 256 if it exceeds

#!/bin/bash

function a {
 
read -p "请输入:" b
   return $[$b * 2]

}
a
echo $?

Insert picture description here
Insert picture description here

Three, transfer parameters

1. Format

#!/bin/bash

sum() {
a=$[$1 + $2]
echo $a
}
sum $1 $2

Insert picture description here
Insert picture description here

2. The scope of function variables

Functions in Shell scripts are only valid in the current Shell environment.
Variables in Shell scripts are globally valid by default.
Limit the variables to the function and use the local
command①

#!/bin/bash

idsrs () {

echo $i
i=$[$i+1]

local i
i=88
i=$[$i+1]
echo $i

}
i=99
echo $i
i=$[$i+1]
idsrs

echo $i

Insert picture description here
Insert picture description here

Four, recursion

1. Factorial

#!/bin/bash

fact() {
  if [ $1 -ne 1 ];then
    local temp=$[$1 - 1]
    local result=$(fact $temp)
    echo $[$1 * $result]
  else
    echo 1
  fi
}
read -p "请输入阶乘数值:" n
fact $n

Insert picture description here
Insert picture description here

2. Recursive directory

method one

#!/bin/bash

listdir () {
for i in $1/*
do
  if [ -d $i ];then
    echo "$2$i: "
    listdir $i " $2"
  else
    echo "$2$i"
  fi
done
}

read -p "请输入目录名:"  dir
listdir $dir ""

Insert picture description here

Insert picture description here
Method Two

#!/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" ""

Insert picture description here

Insert picture description here

Five, create a library

1. Create a database with vim demo9.sh

#!/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 "除数不能为0!" 
fi
}

Insert picture description here

2. Vim demo10.sh calls the database of deme9.sh

. /root/demo9.sh

a=$1
b=$2
a1=`jiafa $a $b`
a2=`jianfa $a $b`
a3=`chengfa $a $b`
a4=`chufa $a $b`

echo "加法结果为:" $a1
echo "减法结果为:" $a2
echo "乘法结果为:" $a3
echo "除法结果为:" $a4

Insert picture description here

3. Verification result

Insert picture description here

Guess you like

Origin blog.csdn.net/s15212790607/article/details/114641914