How to use Shell functions

Source: Internet
Author: User
Tags define function function definition

A shell function is a set of command sets or statements that make up an available block. Using functions can simplify scripting. The function requires that the function be defined and reused, and that it is used directly when calling the functions. This article focuses on how to define functions in shell programming, how to use functions, how to pass arguments to functions, how to get input parameters and how to use them, how to return functions, and what to do.

function Definition

Functions consist of two parts: function name and function body. The form is as follows:

Function_name (){  function_body}

You can also precede the function name with a functions keyword. As follows

Fucntion function_name () {  function_body}

Let's look at the simplest example of a shell function that prints the specified output:

Print_msg () <-- definition function, function named  print_msg{echo"HelloWorld"  <-- function Body}

Here, Print_msg is the function name, and Echo line is the function body. It's simple. But what if you want the function to print what you specify? You need to consider passing the specified arguments to the function.

passing parameters to a function

We extend the PRINT_MSG function that specifies the output fixed content above to implement functions that can be output according to the specified content. The code is as follows:

 #!/bin/bashprompt_msg ()  <-- define functions, Function name prompt_msg{msg_level  =$1  <-- Get the first parameter of the function input, assign to the variable msg_level msg_info  =$2  <--  Gets the second argument of the function input, assigns a value to the variable msg_info  echo   " ${msg_level}: ${msg_info}   " }prompt_msg   " error   "  " the Directory not exist   " <--uses function names prompt_msg call functions, passing 2 parameters to the function:" Error "," The Directory not exist " 

Here, we notice that the function body has a special character of $ $, $ $. What does that mean? This is also the main part of this chapter that you want to describe. Let's look at the special characters that are commonly used in functions like $, $, and what they mean and how to use them.

$ A: Represents the function name.

$, $ , $ ... $n: Represents the 1th argument, 2nd parameter, and 3rd parameter of the passed-in function ... The nth parameter.

$#: Indicates the total number of parameters passed in.

[email protected]: "$", "$", "$" ... "$n", each variable is independent, often used.

$*: "$ $ $ ... $n", each variable is not independent.

Below we will be for the $, $, $#, [email protected], $* and other special symbols to be introduced individually. Using these symbols, we continue to refine the above examples.

#!/bin/bashprompt_msg ()<--define function, function named prompt_msg{if[$#-ne2]; Then<--$ #表示传入参数总数, that is, if the total number of arguments does not equal 2, the function exits with an errorEcho " $"<--name of output functionEcho "[email protected]"<--$ #输出所有传入函数的参数Echo "Please input 2 parameters,please check"Exit1  fiMsg_level=$1<--get the first parameter of the function input, assign the value to the variable msg_level msg_info=$2<--get the second parameter of the function input, assign the value to the variable msg_infoEcho "${msg_level}: ${msg_info}"}prompt_msg"ERROR" "The Directory not exist"<--uses function name prompt_msg to call functions, output error:the Directory not exist
using function return values

It is common to use a function to complete a logical process or to determine if the function return value is true for the next process. The return statement is typically used in a function that returns the result of a script processing, 0 is typically used to indicate proper handling of no-error returns, and not 0 for handling exception returns. Let's look at how to use a function that determines whether an odd number is used. The code is as follows:

#!/bin/bashis_odd () {num=$1  if[num%2-ne0]; Thenreturn0  Elsereturn1  fi}number=3is_odd"${number}"if[$?-ne0]; Then<--$?represents the return of the previous command execution. Echo "$number is odd"Else  Echo "$number is not odd"fi

Description: The Is_odd function uses the return statement to return the judging result. The next command is judged by the $? Get the output return value of the function. Remember $? Use of the method, very common.

Summary

using functions can save a lot of scripting time and make the scripting structure clearer. For frequently used logical statements can be organized into a function saved to a file, it is convenient for subsequent scripting.

How to use Shell 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.