7 ways to use the Shell function introduction _linux Shell

Source: Internet
Author: User

1. Define functions within the shell file and refer to:

Copy Code code as follows:

[~/shell/function]# Cat factorial.sh
#!/bin/bash
function factorial
{
Factorial=1
For ((I=1;i <= $1;i++))
Todo
factorial=$[$factorial * $i]
Done
The factorial of ECHO is: $factorial
}
Echo ' program name ': $, for factorial
Factorial $
[~/shell/function]#./factorial.sh 10

Program name:./factorial.sh, used to find factorial
The factorial of 10 is: 3628800

2. Return value

The function return code refers to the status code of the last command of the function, which can be used for the function return value
Use the return command to manually specify the returned value:

Copy Code code as follows:

[~/shell/function]# Cat return.sh
#!/bin/bash
function Fun1 {
Read-p "Enter a:" A
Echo-n "Print 2a:"
Return $[$a * 2]
}
Fun1
echo "return value $?"
[~/shell/function]#./return.sh
Enter a:100
Print 2a:return Value 200

Because the shell status code is maximum 255, an error occurs when the return value is greater than 255.

Copy Code code as follows:

[~/shell/function]#./return.sh
Enter a:200
Print 2a:return Value 144

3. Function output

In order to return numbers, floating-point numbers, and string values greater than 255, it is best to output the function to a variable:

Copy Code code as follows:

[~/shell/function]# Cat./fun_out.sh
#!/bin/bash
function Fun2 {
Read-p "Enter a:" A
Echo-n "Print 2a:"
echo $[$a * 2]
}
result= ' fun2 '
echo "Return value $result"
[~/shell/function]#./fun_out.sh
Enter a:400
return value Print 2a:800

4. Pass parameters to function (using positional parameters):

Copy Code code as follows:

[~/shell/function]# Cat./parameter.sh
#!/bin/bash
If [$#-ne 3]
Then
echo "Usage: $ A b C"
Exit
Fi
Fun3 () {
Echo $[$ * $ $]
}
Result= ' Fun3 $ $ '
echo the result is $result
[~/shell/function]#./parameter.sh 1 2 3
The result is 6
[~/shell/function]#./parameter.sh 1 2
Usage:/parameter.sh a b C

5. Global variables and local variables

By default, variables created in functions and shell bodies are global variables that can be referenced to each other, and may interact with each other when the shell body part has the same variable with the same name as the function part, for example:

Copy Code code as follows:

[~/shell/function]# Cat./variable.sh
#!/bin/bash
If [$#-ne 3]
Then
echo "Usage: $ A b C"
Exit
Fi
Temp=5
Value=6
Echo Temp is: $temp
echo value is: $value
Fun3 () {
Temp= ' echo ' scale=3;$1*$2*$3 | Bc-ql '
result= $temp
}
FUN3 $ $
echo "The result is $result"
If [' echo ' $temp > $value ' | bc-ql '-ne 0]
Then
echo "Temp is larger"
Else
echo "TEMP is still smaller"
Fi
[~/shell/function]#./variable.sh 12 3 2
Temp Is:5
Value Is:6
The result is 72
Temp is larger

In this case, it is best to use local variables within the function to eliminate the impact.

Copy Code code as follows:

[~/shell/function]# Cat./variable.sh
#!/bin/bash
If [$#-ne 3]
Then
echo "Usage: $ A b C"
Exit
Fi
Temp=5
Value=6
Echo Temp is: $temp
echo value is: $value
Fun3 () {
Local temp= ' echo ' scale=3;$1*$2*$3 | Bc-ql '
result= $temp
}
FUN3 $ $
echo "The result is $result"
If [' echo ' $temp > $value ' | bc-ql '-ne 0]
Then
echo "Temp is larger"
Else
echo "TEMP is still smaller"
Fi
[~/shell/function]#./variable.sh 12 3 2
Temp Is:5
Value Is:6
The result is 72
Temp is still smaller

6. Passing an array variable to a function:

Copy Code code as follows:

[~/shell/function]# Cat array.sh
#!/bin/bash
A= (11 12 13 14 15)
Echo ${a[*]}
function Array () {
echo Parameters: "$@"
Local factorial=1
For value in "$@"
Todo
factorial=$[$factorial * $value]
Done
Echo $factorial
}
Array ${a[*]}
[~/shell/function]#./array.sh
11 12 13 14 15
Parameters:11 12 13 14 15
360360

7. function returns an array variable

Copy Code code as follows:

[~/shell/function]# Cat array1.sh
#!/bin/bash
A= (11 12 13 14 15)
function Array () {
echo Parameters: "$@"
Local newarray= (' echo ' $@ ')
Local element= "$#"
Local I
for ((i = 0; i < $element; i++))
{
newarray[$i]=$[${newarray[$i]} * 2]
}
echo New value:${newarray[*]}
}
result= ' array ${a[*]} '
Echo ${result[*]}
[~/shell/function]#./array1.sh
Parameters:11 New Value:22 24 26 28 30

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.