Linux shell programming function use, linuxshell Programming

Source: Internet
Author: User

Linux shell programming function use, linuxshell Programming

In linux shell, you can define functions and call them in shell scripts. The following describes the definition method and the precautions for calling it.

Discuss with author: http://www.cnblogs.com/intsmaze/p/6675421.html

: Intsmaze

Function Definition

#! /Bin/sh # func1.shhello () # function definition {echo "Hello there today's date is'Date + % Y-% m-% d'"# Return 2 ### the returned value is actually a status code, which can only be in the range of [0-255]} hello # function call echo $? # Get the return value of the function, that is, get the status code of the previous command. If return 2 does not have a comment, is it to get the status of the return 2 command. Echo "now going to the function hello intsmaze"

Call scripts

[hadoop@centos-Reall-131 shtest]$ sh func1.sh Hello there today's date is 2017-04-060now going to the function hello intsmaze

Note:

1. You must declare the function before calling the function. The shell script runs row by row. It does not pre-compile like other languages.

2. Function return value, only through $? System variable acquisition, You can display the result of adding: return. If not added, the result will be run with the last command as the return value. Return followed by the value n (0-255)

Function Parameters
#! /Bin/bash # fun1.shfunWithParam () {echo "the first parameter is $1! The second parameter of "echo" is $2! "Echo": the tenth parameter is $10! "Echo": the tenth parameter is ${10 }! "Echo" 11th parameters: ${11 }! "Echo"The total number of parameters is $! "Echo"Output all parameters as a string $ *! "} FunWithParam 1 2 3 4 5 6 7 8 9 34 73

Note: $10 cannot obtain the tenth parameter. $ {10} is required to obtain the tenth parameter }. When n> = 10, you need to use $ {n} to obtain the parameter.

[Hadoop @ centos-Reall-131 shtest] $ sh fun1.sh the first parameter is 1! The second parameter is 2! The tenth parameter is 10! The tenth parameter is 34! 11th parameters are 73! There are 11 parameters in total! Output all parameters as a string 1 2 3 4 5 6 7 8 9 34 73!
Function return value
#! /Bin/bash # fun2.shfunWithReturn () {echo "This function adds two numbers to the input... "echo" Enter the first number: "read aNum echo" enter the second number: "read anotherNum echo" two numbers: $ aNum and $ anotherNum! "Return $ ($ aNum + $ anotherNum)} funWithReturnecho" returns the sum of the two numbers $? ! "# The previous command here is return $ ($ aNum + $ anotherNum ))

Call scripts

[Hadoop @ centos-Reall-131 shtest] $ sh fun2.sh this function will calculate the sum of the two numbers entered... enter the first number: 100 enter the second number: 200 two numbers are 100 and 200! The sum of the two numbers is 44! # Here, the previous command is return 300.

Call functions across scripts

Assume that the above script file fun1.sh is saved in this path:/root/fun1.sh

You can call the function in script fun1.sh in script fun_other.sh.

#! /Bin/bash # fun_other.sh./Root/fun1.sh #Note: there is a space between. And/# or source/root/fun1.shfunWithParam 11 22 33 44 55 66 77 88 99 100 101

Call scripts

[Hadoop @ centos-Reall-131 shtest] $ sh fun_other.sh the first parameter is 1! The second parameter is 2! The tenth parameter is 10! The tenth parameter is 34! 11th parameters are 73! There are 11 parameters in total! Output all parameters as a string 1 2 3 4 5 6 7 8 9 34 73! The first parameter is 11! The second parameter is 22! The tenth parameter is "110! The tenth parameter is "100! The 11th parameters are 101! There are 11 parameters in total! Output all parameters as a string 11 22 33 44 55 66 77 88 99 100 101!

 

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.