shell script calling other functions within a function

In shell scripts, you can call other functions within functions just as you can in other programming languages. Here is an example:

#!/bin/bash

# 定义函数 func1
function func1() {
  echo "This is func1."

  # 调用函数 func2
  func2

  echo "Func1 is done."
}

# 定义函数 func2
function func2() {
  echo "This is func2."
}

# 调用函数 func1
func1

The script defines two functions: func1 and func2. In the func1 function, first output a message, then call the func2 function, and finally output a message. In the func2 function, only one message is output.

On the last line, the script calls the func1 function, so when you run the script on the command line, you see the following output:

This is func1.
This is func2.
Func1 is done.

At the same time, multiple other functions can be called in one function, or other functions can be called again in the called function, and so on. This makes scripts more readable and maintainable.

Guess you like

Origin blog.csdn.net/2203_75758128/article/details/129661642