Shell Scripting Learning notes-functions

Source: Internet
Author: User

1. Basic Scripting functions

1.1 Definition of function

Function name {        commands}
Or

Name () {     commands}

1.2 Simple example

#!/bin/shfunction FUNC01 {echo "This is the function of FUNC01"}FUNC02 () {echo "This is function of FUNC02"}FUNC01FUNC02
Operation Result:


1.3 function return value and exit code

#!/bin/shfunction FUNC01 {echo "This is the function of FUNC01"}FUNC02 () {echo "This is function of Func02"}func03 () {echo "Thi S is function of Func02 "       #自定义退出码, exit codes need to be limited to 0-255        return 10}func01# output function run exit code, 0 means successful echo" $? " #运行一个未定义的函数, observe the exit code Func04echo "$?" #观察函数自定义的退出码Func03echo "$?"
Operation Result:


2. Variables used in functions

2.1 Variable parameter handling in function

#!/bin/shsum () {     if [$#-eq 2]     then         sum=$[$1+$2]         echo $sum     else          echo "Please input 2 args"          Return 1     fi   }value= ' Sum $ "if [$?-eq 0]then    echo $valueelse    echo" Err $value "fi
Operation Result:


2.2 Using array parameters in functions

#!/bin/sh# defines an array numberarray= (1 2 3 4 5 6 7 8 9) #定义一个求和函数sum () {   result=0 for   var in [email protected]   do
   
    result=$[$result + $var]   done   Echo $result} #输出数组的长度echo "Length of output array: ${#numberArray [@]}" #输出数组中指定的值echo " The value specified in the output array: ${numberarray[2]} "#修改数组指定的值numberArray [2]=4echo" to modify the value after the array specified: ${numberarray[2]} "#调用函数, using the array variable echo" to invoke the function, Use array variable: ' Sum ${numberarray[*]} ' "
   
Operation Result:


2.3 Defining local variables in functions with the local keyword

#!/bin/sh# defines an array numberarray= (1 2 3 4 5 6 7 8 9) #定义一个求和函数sum () {#未定义局部变量的情况   result=0 for   var in [email protected]      do result=$[$result + $var]   done   echo $result}echo "No local variables defined" sum ${numberarray[*]}echo "$result" Sum () {#使用local关键字将result变量定义为局部变量   local result=0 for   var in [email protected]   do      result=$[$result +$ var]   done echo $result}echo "The local keyword was used to define the case of the" sum ${numberarray[*]}echo "$result"
Operation Result:


3. Referencing function libraries

3.1 By using the source FilePath command to reference the library of other scripts in the script, source filepath can also be abbreviated as ". FilePath "in the form of

3.2 Simple example, write two shell scripts, reference shellscript functions in the newshell.sh script, e.g:


Shell Scripting Learning notes-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.