Introduction to LinuxShell functions

Source: Internet
Author: User
LinuxcCentOSDebianFreeBSD1 defines the function in the shell file and references :[~ Shellfunction] catfactorialsh! Binbashfunctionfactorial {factorial1for (i1; I & lt; $1; I Linux Shell function method introduction linuxcCentOSDebianFreeBSD 1. define the function in the shell file and reference it :[~ /Shell/function] # cat factorial. sh #! /Bin/bashfunction factorial {factorial = 1for (I = 1; I <= $1; I ++ )) do factorial = $ [$ factorial * $ I] the factorial of doneecho $1 is: $ factorial} echo 'Program name': $0, used to calculate the factorial $1 [~ /Shell/function] #. /factorial. sh 10 program name :. /factorial. sh, used to calculate the factorial of factorial 10: 36288002. return value function return code refers to the status code of the last function command. it can be used for the return value of the function. use the return command to manually specify the return value :[~ /Shell/function] # cat return. sh #! /Bin/bashfunction fun1 {read-p "enter a:" a echo-n "print 2a:" return $ [$ a * 2]} fun1echo "return value $? "[~ /Shell/function] #./return. sh enter a: 100 print 2a: return value 200 because the maximum shell status code is 255, an error occurs when the return value is greater than 255 [~ /Shell/function] #. /return. sh enter a: 200 print 2a: return value 1443. function output to return a number greater than 255, floating point number, and string value, it is best to use the function to output to the variable :[~ /Shell/function] # cat./fun_out.sh #! /Bin/bashfunction 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: 8004. pass parameters to the function (using location parameters ):[~ /Shell/function] # cat./parameter. sh #! /Bin/baslif [$ #-ne 3] then echo "usage: $0 a B c" exitfifun3 () {echo $[$1*$2 * $3]} result = 'fun3 $1 $2 $3 'echo the result is $ result [~ /Shell/function] #./parameter. sh 1 2 3the result is 6 [~ /Shell/function] #. /parameter. sh 1 2 usage :. /parameter. sh a B c5. by default, global variables and local variables are global variables created in functions and shell bodies and can be referenced from each other, when the shell body and function have the same name variables, they may affect each other. for example :[~ /Shell/function] # cat./variable. sh #! /Bin/baslif [$ #-ne 3] then echo "usage: $0 a B c" exitfitemp = 5 value = 6 echo temp is: $ tempecho value is: $ valuefun3 () {temp = 'echo "scale = 3; $1*$2*$3 "| bc-ql 'result = $ temp} fun3 $1 $2 $3 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: 6the result is 72 temp is larger in this case, it is best to use local variables inside the function to eliminate the impact [~ /Shell/function] # cat./variable. sh #! /Bin/baslif [$ #-ne 3] then echo "usage: $0 a B c" exitfitemp = 5 value = 6 echo temp is: $ tempecho value is: $ valuefun3 () {local temp = 'echo "scale = 3; $1*$2*$3 "| bc-ql 'result = $ temp} fun3 $1 $2 $3 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: 6the result is 72 temp is still smaller6. pass the array variable to the function :[~ /Shell/function] # cat array. sh #! /Bin/basha = (11 12 13 14 15) echo $ {a [*]} function array () {echo parameters: "$ @" local factorial = 1 for value in "$ @" do factorial = $ [$ factorial * $ value] done echo $ factorial} array $ {a [*]} [~ /Shell/function] #./array. sh 11 12 13 14 14 15 parameters: 11 12 13 14 153603607. the function returns the array variable [~ /Shell/function] # cat array1.sh #! /Bin/basha = (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 12 13 14 15 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.