Create functions in Linux command line and shell script programming

Source: Internet
Author: User
Linux command line and shell script programming Daquan create function basic script function create function 1. use the function keyword, followed by the function name [plain] functionname {commands} 2. the function name is followed by an empty parentheses, indicating that a function is being defined [plain] name ()... create functions in Linux command line and shell script programming
Basic script function creation function 1. use the function keyword, followed by the function name [plain] function name {commands} 2. the function name is followed by an empty parentheses, indicating that a function is being defined [plain] name () {commands}. if the names of the two functions are the same, the latter will overwrite the previous one, and there is no prompt. Use the function name. It must be defined before use. [Plain] #! /Bin/bash f1 () {echo "this is f1."} f1 function f1 {echo "this is f1111."} f1 f2 f2 () {echo "this is f2."} f1 is defined first, then executed, then f1 is defined, and then executed. The second execution is the new f1 function. Then call f2. because f2 is not defined yet, the call will fail. [Plain] $ function_test this is f1. this is f1111 .. /function_test: line 12: f2: command not found the return value bash shell treats the function as a small script and returns an exit status code at the end of the operation. There are three methods to generate the function exit status code. the default exit status code is the exit status code returned by the last command in the function. after the function is executed, you can use $? Variable to determine the exit status code of the function [plain] #! /Bin/bash fun () {ls nothing echo $? Echo "done"} fun echo "fun: $? "Because ls cannot find the nothing file or directory, the returned status code is 2, but the echo of the fun function is successfully executed. Therefore, the status code returned by fun is 0 [plain] $ function_test ls: cannot access nothing: No such file or directory 2 done fun: 0 it is dangerous to use the function's default exit status code ...... Run the return command bash shell to exit the function and return a specific exit status code. The return command allows you to specify an integer to define the exit status code of the function. Note: When the function ends, the return value is obtained. the exit status code must be between 0 and. You can use function output to save the function output to a variable. for example: result = 'F1' bash shell does not use read to read the input part as STDOUT. when a function is specified in the script by passing parameters to the function using variables in the function, you must put the parameters and functions in the same row. then, the function can use the parameter environment variables to obtain the parameter values. global variables and local variables are used to process variable functions in the function. All variables defined by global variables in the script are global variables. [Plain] #! /Bin/bash var = 1 fun () {echo "var:" $ var var2 = 2} fun echo "var2:" $ var2 output: [plain] $ function_test var: 1 var2: 2 any variable in the local variable function can be declared as a local variable. you only need to add the local keyword before the variable. The local variables defined in the script cannot be accessed in the function. When an array variable or function is passed to a function, the value of the array variable must be decomposed into a single value and used as a function parameter. Inside the function, you can combine them into an array. [Plain] #! /Bin/bash array = (a B c d) fun () {newarray = ($ @) # reassemble it into an array echo "newarray is: "$ {newarray [*]} # print The array echo" The 3rd element is: "$ {newarray [3]} fun $ {array [*]} # split it into multiple values and input the output: [plain] $ function_test newarray is: a B c d The 3rd element is: d if you try to pass The array as a parameter to The function, The function will only read The first value of The array variable. [Plain] #! /Bin/bash array = (a B c d) fun () {echo "We have" $ # "parameter (s ). "echo" They are: "$ @ echo" They are: "$ *} fun $ array fun function only recognizes the first element in the array [plain] $ function_test We have 1 parameter (s ). they are: a returns the array [plain] from the function #! /Bin/bash fun () {local array = (a B c d) echo $ {array [*]} array = ('fun ') echo $ {array [2]} is similar to the normal string returned. echo is output in sequence and placed in parentheses. The simplest method of function recursion is to calculate the factorial of N: [plain] #! /Bin/bash fun () {local result = 1; if [$1-eq 1] then echo 1 else result = $[$1 * 'fun $ [$1-1] '] echo $ result fi} fun $1 is not new knowledge Point, if ''is used, the echo in the function will not be output to STDOUT, just like other examples above. You can run the source command in the current shell context instead of creating a new shell to execute the command. You can use the source command to run the library file script in the shell script. The source command is equivalent to the dot operator, which defines a plus function [plain] # in a file named lib_test. /Bin/bash plus () {if [$ #-ne 2] then echo "I need 2 numbers! "Return fi echo $[$1 + $2]} then we use it in another script [plain] #! /Bin/bash. lib_test # The Script path plus $1 $2 is easy to use, this is equivalent to importing lib_test to the current file. [plain] $ function_test 4 5 9. use a function on the command line to create a function on the command line. as the shell will explain when you type the command command, therefore, functions can be defined directly on the command. [Plain] $ plus () {echo $[$1 + $2];} $ plus 3 1 4 note: add spaces on both sides of curly brackets, each command is separated by a semicolon. The following method looks more practical and works better [plain] $ plus () {> echo $[$1 + $2] >}$ plus 4234 23 4257 requires no semicolons or spaces. In. the function defined in the bashrc file can be defined directly here, or you can use the source command to import the function to the database. bashrc must be restarted before it takes effect. if it is not executed every time it is started, we can also directly use source in shell. in many cases, this method may be better.
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.