7 Ways to use shell functions

Source: Internet
Author: User

1. Define the function and reference inside the shell file:

Copy CodeThe code is as follows:
[~/shell/function]# Cat factorial.sh
#!/bin/bash
function factorial
{
Factorial=1
For ((I=1;i <= $1;i++))
Do
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

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

Copy CodeThe code is 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 255 maximum, an error occurs when the return value is greater than 255.

Copy CodeThe code is as follows:
[~/shell/function]#./return.sh
Enter a:200
Print 2a:return Value 144

3. Function output

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

Copy CodeThe code is 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 the function (using positional parameters):

Copy CodeThe code is 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 the function and shell bodies are global variables that can be referenced to each other, and may affect each other when the shell body part and the function part have the same name variables, for example:

Copy CodeThe code is 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 inside the function to eliminate the effect.

Copy CodeThe code is 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. Pass an array variable to the function:

Copy CodeThe code is as follows:
[~/shell/function]# Cat array.sh
#!/bin/bash
A= (11 12 13 14 15)
Echo ${a[*]}
function Array () {
echo Parameters: "[email protected]"
Local factorial=1
For value in "[Email protected]"
Do
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 CodeThe code is as follows:
[~/shell/function]# Cat array1.sh
#!/bin/bash
A= (11 12 13 14 15)
function Array () {
echo Parameters: "[email protected]"
Local newarray= (' echo ' [email protected] ')
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

From:http://www.jb51.net/article/57951.htm

Top

7 Ways to use shell functions

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.