Linux shell programming study notes 24: function definition and use

*20231113 Added examples of using for loops to simplify code

In order to achieve modular design and code reuse, many programming languages ​​support functions or procedures, and the Linux shell also supports function definition and calling.

The functions in the Linux shell have many similarities with other programming languages, but also have their own unique features.

1 Definition of function

1.1 Standard format

function 函数名()
{
    语句或命令1

     ……

    语句或命令n
    [return 返回值]
}

in:

  • Function is a keyword in Shell, specially used to define functions;
  • The part surrounded by { } is called the function body. Calling a function actually executes the code in the function body.
  • return is a Shell keyword, specifically used to return a value in a function; if a return value is not needed, this sentence does not need to be written.

Is this format similar to JavaScript's function definition format?
 

1.2 Simplified format

In actual practice, there are two simplified formats:

1.2.1 Simplified format 1—omitting the function keyword

We can omit the function keyword and have the following simplified format:

函数名()
{
    语句或命令1

     ……

    语句或命令n
    [return 返回值]
}

1.2.2 Simplified format 2 - omit the parentheses after the function name

If we write the function keyword, we can also omit the parentheses after the function name, and we will have the following simplified format:

function 函数名
{
    语句或命令1

     ……

    语句或命令n
    [return 返回值]
}

1.3 Suggestions

It is recommended to use the 1.1 standard format to maintain writing compatibility with other programming languages.

2 function parameters

2.1 Functions cannot specify parameters when defining them

Although the function definition format in Linux Shell is very similar to the function definition format of JavaScript, it also has its own special feature, that is, the function cannot specify parameters when it is defined, but it can pass parameters when calling, and It receives whatever parameters it passes.

2.2 How to use parameter information in the function body

The format for obtaining parameters in the script within the function body is:

${n}

where n represents a number, 0 is the command line command to execute the script, 1 is the first parameter to execute the script, 2 is the second parameter to execute the script, and so on And so on...

  • When n > 10, {} can be omitted, abbreviated as $n.
  • When n>=10, be sure to use ${n}

In addition, there are several special parameter processing information:

Parameter handling illustrate
$# The number of parameters passed to the script
$* Displays all parameters passed to the script as a single string.
For example, if "$*" is enclosed by "", all parameters will be output in the form of "$1 $2 ... $n".
$@ is the same as $*, but is used in quotes and returns each parameter in quotes.
If "$@" is enclosed by """, all parameters will be output in the form of "$1" "$2" ... "$n".
$$ The current process ID number of the script running
$! The ID number of the last process running in the background
$- Displays the current options used by the shell
$? Displays the exit status of the last command. 0 indicates no errors, any other value indicates an error.

3 Function execution (call)

Format:

Function name [Parameter 1 Parameter 2...Parameter n]

Like JavaScript, Linux Shell does not place restrictions on the order of function definition and call. You can place the definition before the call, or conversely, place the definition after the call.

However, the difference with JavaScript:

  • No () is used after the function name.
  • If parameters are to be passed, the parameters are separated by spaces.

4. Function definition and calling examples

We define a function x:

function x()
{
	echo cmd: $0; 
	echo param number:$#; 
	echo they are $@; 
	echo 1-$1; 
	echo 2-${2}; 
	echo 3-$3; 
	echo 4-$4; 
	echo 5-$5; 
	echo 6-$6;
	echo 7-$7; 
	echo 8-$8;
	echo 9-$9;
	echo 10-${10};
	return 0;
};

In the function body:

  • We use the echo command to first display the command line command ($0)
  • Then display the number of command line parameters ($#)
  • Then we display all command line parameters ($@)
  • Then we show each parameter one by one
  • Finally, we use the return statement to return a value of 0

4.1 in bash

csdn @edu bash $ function x() {echo cmd: $0; echo param number:$#; echo they are $@; echo 1-$1; echo 2-${2}; echo 3-$3; echo 4-$4; echo 5-$5; echo 6-$6;echo 7-$7; echo 8-$8; echo 9-$9; echo 10-${10}; return 0;}; x a1 b2 c3 d4 e5 f6 h7 i8 j9 k10; echo exit code: $?
bash: syntax error near unexpected token `{echo'
csdn @edu bash $ function x() { echo cmd: $0; echo param number:$#; echo they are $@; echo 1-$1; echo 2-${2}; echo 3-$3; echo 4-$4; echo 5-$5; echo 6-$6;echo 7-$7; echo 8-$8; echo 9-$9; echo 10-${10}; return 0;}; x a1 b2 c3 d4 e5 f6 h7 i8 j9 k10; echo exit code: $?
cmd: bash
param number:10
they are a1 b2 c3 d4 e5 f6 h7 i8 j9 k10
1-a1
2-b2
3-c3
4-d4
5-e5
6-f6
7-h7
8-i8
9-j9
10-k10
exit code: 0
csdn @edu bash $ 

In the command line, we first define function x, then call function x and pass 10 parameters:

x a1 b2 c3 d4 e5 f6 h7 i8 j9 k10

Finally we use the command

echo exit code: $?

Show that the return value of function x is 0

In the bash command line, the { at the beginning of the function body and the following commands or statements must be separated by spaces.


4.2 zsh

csdn @ edu zsh $ function x() {echo cmd: $0; echo param number:$#; echo they are $@; echo 1-$1; echo 2-${2}; echo 3-$3; echo 4-$4; echo 5-$5; echo 6-$6;echo 7-$7; echo 8-$8; echo 9-$9; echo 10-${10}; return 0;}; x a1 b2 c3 d4 e5 f6 h7 i8 j9 k10; echo exit code: $?
cmd: x
param number:10
they are a1 b2 c3 d4 e5 f6 h7 i8 j9 k10
1-a1
2-b2
3-c3
4-d4
5-e5
6-f6
7-h7
8-i8
9-j9
10-k10
exit code: 0

The code is the same as in bash, but the zsh command line does not require the { at the beginning of the function body and the following commands or statements to be separated by spaces.

4.3 Use for loops to simplify code

In the above example, to output the parameters one by one, we can also use the for statement to simplify it.

function x()
{
	echo cmd: $0; 
	echo param number:$#; 
	echo they are $@; 
	j=1;
	for i in $@
	do
  		echo $j-$i; 
		((j++));
	done
	return 0;
};

4.3.1 In bash

csdn @ edu bash $ function x(){ echo cmd: $0; echo param number:$#; echo they are $@; j=1; for i in $@; do echo $j-$i; ((j++));done;return 0; };x a1 b2 c3 d4 e5 f6 h7 i8 j9 k10; echo exit code: $?
cmd: bash
param number:10
they are a1 b2 c3 d4 e5 f6 h7 i8 j9 k10
1-a1
2-b2
3-c3
4-d4
5-e5
6-f6
7-h7
8-i8
9-j9
10-k10
exit code: 0

4.3.2 In zsh

csdn @ edu zsh $ function x(){ echo cmd: $0; echo param number:$#; echo they are $@; j=1; for i in $@; do echo $j-$i; ((j++));done;return 0; };x a1 b2 c3 d4 e5 f6 h7 i8 j9 k10; echo exit code: $?
cmd: x
param number:10
they are a1 b2 c3 d4 e5 f6 h7 i8 j9 k10
1-a1
2-b2
3-c3
4-d4
5-e5
6-f6
7-h7
8-i8
9-j9
10-k10
exit code: 0

Guess you like

Origin blog.csdn.net/Purpleendurer/article/details/134344889