Function of Shell programming

Source: Internet
Author: User
Tags function definition

First, function definition and invocation

A function is a series of commands that are customized in a shell script, and generally the function should have a return value (returning 0 correctly and returning a non-0 error). For error returns, you can define other non-0 positive values to refine the error. The biggest benefit of using a function is to avoid a lot of duplicate code, while enhancing the readability of the script: if you see Checkfileexist code (actually a function call) in a shell script, it's not hard to guess what the code does.

[email protected] ~]# cat sayhello.sh
#!/bin/bash
function SayHello () {#关键字function can be omitted

echo "Hello"

}
echo "Call function SayHello" #提示函数调用
SayHello #函数调用

[Email protected] ~]# sh sayhello.sh
Call Function SayHello
Hello

[email protected] ~]# cat countline.sh
#!/bin/bash
file=/etc/passwd
function Countline () {
Local i=0
While Read line
Do
Let ++i
Done < $FILE
echo "$FILE has $i line"
}
echo "Call Functino Countline"
Countline

[Email protected] ~]# sh countline.sh
Call Functino Countline
/ETC/PASSWD has line

Second, the return value of the function

The return value of a function is also called the exit state of a function, which is actually a means of communication. A function in the shell can use the return value to give the caller feedback (using the return keyword), and don't forget to get the last command return value by using $? This gets the primary way that the function returns a value.

[email protected] ~]# cat checkfileexist.sh
#!/bin/bash
File=/etc/notexistfile #定义一个不存在的文件
function Checkfileexist () {#定义checkFileExist函数
If [-F $FILE];then
return 0
Else
Return 1
Fi
}
Checkfileexist #调用函数

If [$?-eq 0];then
echo "$FILE exist"
Else
echo "$FILE not Exist"
Fi

[Email protected] ~]# sh checkfileexist.sh
/etc/notexistfile not exist

Three, the function with the parameter

The Checkfileexist function is defined in the checkfileexist.sh script, but you can see that the script actually writes down the file variable, which causes the file variable in the script to be modified if you want to determine whether the different files exist- That is to modify the contents of the code itself, which is typical of the code and data is not separated and caused by the problem. In fact, you can solve this problem by defining a function with parameters.

[email protected] ~]# cat checkfileexist_v2.sh
#!/bin/bash
function Checkfileexist () {
If [-F $];then
return 0
Else
Return 1
Fi


}


echo "Call function Checkfileexist"
Checkfileexist $

If [$?-eq 0];then
echo "$ exist"
Else
echo "not exist"
Fi

Execution Result:

[Email protected] ~]# sh checkfileexist_v2.sh/etc/not
Call Function Checkfileexist
/etc/not not exist

Iv. specifying positional parameter values

In addition to the positional parameters passed to the script when the script is run, you can use the built-in command set command to give the script a value (also called a reset) of the positional parameter. Once the value of the passed parameter is set by using set, the script ignores the positional parameters passed in at run time (the value of the positional parameter is actually reset by the SET command)

#!/bin/bash
Set 1 2 3 4 5 6
Count=1
For i in [email protected]
Do
echo "Here \$ $COUNT is $i"
Let "count++"
Done

The results of the implementation are as follows:

[[Email protected] ~]# sh set01.sh a b c d E F
Here $1 is 1
Here $2 is 2
Here $3 is 3
Here $4 is 4
Here $5 is 5
Here $6 is 6

Five, moving position parameters

Using the shift command in the shell to move the position parameter, the shift command allows the position parameter to be moved to the left of one

[email protected] ~]# cat shift_03.sh
#!/bin/bash

Until [$#-eq 0]
Do
echo "Now \$1 is: $, total parameter is:$#"
Shift
Done

Execution Result:

[[Email protected] ~]# sh shift_03.sh a b C
Now $ is:a, total parameter is:3
Now $ is:b, total parameter is:2
Now $ IS:C, total parameter is:1

Use shift to calculate the and of all parameters in the script:

[email protected] ~]# cat shift_04.sh
#!/bin/bash
Total=0
Until [$#-eq 0]
Do
Let "total=total+$1"
Shift
Done
Echo $TOTAL

Execution Result:

[[Email protected] ~]# SH shift_04.sh 10 20 30
60

Function of Shell programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.