[Shell] Shell functions

Source: Internet
Author: User
Functions allow us to divide a complex function into several modules, so that the program structure is clearer and the code reuse rate is higher. Like other programming languages, shell also supports functions. Shell functions must be defined before use. The Definition Format of Shell functions is as follows:
[function] fun() {    list of commands    [ return value ]}
Note:
  1. Function fun () can be defined, or fun () can be defined directly without any parameters. Function keywords and () must have one of them.
  2. Return value, which can be added: return,If this parameter is not added, the result of the last command is used as the return value. Return followed by the value n (0-255 ).
  3. There is no parameter in the parameter (). It is not like the C language and can have a parameter in. Then everyone may be depressed. Some parameters are always required for function calls. How can these parameters be passed in? In fact, the parameter transmission method is fname; (No parameter is required) or fname AGR1 arg2 (two parameters need to be passed );
The return value of a shell function can only be an integer. It is generally used to indicate whether the function is successfully executed. 0 indicates that the function is successful. Other values indicate that the function fails. If you return other data, such as a string, you will often get an error message: "numeric argument required ". If the function must return a string, you can define a variable first.To receive the calculation result of the function. The script accesses this variable as needed to obtain the function return value. Shell functions return values in three ways:

  1. Directly return through $? To receive the return value;
  2. Defines global variables, and receives them using global variables. function return values are used.
  3. Echo method, which is returned through standard output.
#! /Bin/bashfunction sum () {if [$ #! = 2] Then ECHO "Arg num! = 2 "Return 0 fi return $($1 + $2)} sum 5 6 # To call a function, you only need to provide the function name, without brackets. Echo "5 + 6 = $? "# --> 5 + 6 = 11; the return value of the function is $? . Sum 100 200 echo "100 + 200 =$? "# --> 100 + 200 = 44, return followed by the value n (0-255 ). 300-256 = 44 # pass the array A = (1 2 3 4) sum () {local sum = 0 to the function; for V in [email protected] Do sum = $ [Sum + V] # sum = $ ($ sum + $ V); done echo $ sum; # --> 10} sum $ {A [@]} # The unset command can be used to delete a function just like deleting a variable. f option, # $ unset-F function_name # If you want to call a function directly from the terminal, you can define the function in the main directory. profile file, so that after each login, enter the function name after the command prompt to call immediately.
  • $0: The Script Name;
  • $ #: Indicates the number of parameters passed to the script;
  • [Email protected]: A list of all parameters passed to the script, which is extended to "$1" "$2" "$3" and so on;
  • $ *: Displays all parameters passed to the script as a single string. Different from the location variable, more than 9 parameters can be expanded to "$ 1C $ 2C $3 ", c is the first character of IFS;
  • $: ID of the current process running the script;
  • $? : Displays the exit status of the last command. 0 indicates no error, and others indicate an error;

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.