"Linux system Programming" shell Script Basics Learning Function (v) __mysql

Source: Internet
Author: User
Preface

Linux Shell Scripting Basics We are almost finished here, function believe that we are not unfamiliar with it, directly to the topic of it ~

Please indicate the original link:
http://blog.csdn.net/u011974987/article/details/52718164
This article is from: "Stromxu's blog" Body

After the last blog Shell function

The Linux shell can be user-defined, and can be invoked randomly in a shell script.
Functions in the shell are defined in the following format:

Function_name () {
    List of commands
    [return value]
}

Description: 1, the function name can also be preceded by a keyword function; 2, the return value of the functions, can be displayed plus: return returns, if not added, the last command will run as the result of the return value.

The Shell function return value can only be an integer, generally used to indicate whether the function execution is political, 0 for success, others for failure.

If you want the function to return a string, you can define a variable that receives the result of the function, and the script accesses the variable to get the return value of the function.

Like what:

  1 #!/bin/bash                                                            
  2 hello () {
  3         echo "My name is Xu Hao"
  4}
  5 
  6 #调用上面的函数
  7 Hello

Run Result:

root@iz28t5k4ny4z:/usr/xiho/shell#./06.sh my 
name is Xu Hao

calling a function requires only the name of the function and does not require parentheses.

Let's look at a function with the return statement:

After the example above, add:

Funwithreturn () {
 one         echo "This function is to get two digits and"         echo-n "Please enter the first number"         read anum;         echo-n "Please enter a second number"         read bnum;         echo "These two numbers are $aNum and $bNum!"
 Return   $ ($aNum + $bNum))
 funwithreturn #调用funWithReturn这个函数 
 Ret=$?
 echo "The sun of two numbers is $ret!"

Run Result:

root@iz28t5k4ny4z:/usr/xiho/shell#./06.sh my 
name is Xu Hao
this function is to get two digits and
Please enter the first digit
Please enter the second number
two numbers are 56 and respectively!
The sun of two numbers is 79!

The end code (the return value) of the last command to run, which is summarized later. function Arguments

In a shell, you can pass arguments to a function, within the function body, by $n in the form of a parameter, for example, to represent the first argument, and $ for the second argument

Let's look at an example of a function with parameters:

Funwithparam () {echo "the         first param is $         echo" The second param is $ "the         tenth param is $
 "         echo "Tenth param is ${10}"         echo "parameter has a total of $#"         echo "as a string to output all parameters $*!"
 #调用函数并传递参数
 funwithparam 1 2 3 4 5 6 23 45 22 9 25

The result of running the script output is:

Note here that $10 cannot get the 10th parameter, and when n>=10, ${n} is required to get the parameters.

In addition: the parameters to be processed by supplementing several special characters

$# the number of parameters passed to the script
$* displays all parameters passed to the script in a single string
$$ the    current process ID number of the script run
$! the ID number of the last process running in the background is $@ the
same as $*, but with quotes , and returns each parameter in quotation marks. The
$-displays the current options used by the shell, as is the SET command function.
$? Displays the exit status of the last command. 0 indicates no errors, and any other value indicates an error.

In the afternoon to organize the two blog, is the Linux shell script based on the theoretical study finished, more details only when we met in slowly pondering it. Eat ~ More related series article Portal: "CSDN" Linux Shell Scripting Basics Learning Series

"Linux system Programming" Shell Scripting Basics Learning (i)
"Linux system Programming" shell command and Flow control (ii)
"Linux system Programming" shell process Control loop and quotation marks (c)
"Linux system Programming" shell input/output Redirect and Heredocument (d)
"Linux system Programming" shell Script Basics Learning Function (v)

This article by the blogger painstakingly sorted down the notes;
I hope we can point out or put forward valuable opinions and learn together, thank you.
Reprint please indicate the source: http://blog.csdn.net/u011974987/article/details/52718164
Welcome to my social networking site
Personal blog: xuhaoblog.com
Sina Micro Blog: Http://weibo.com/xuxiho
Github:https://github.com/git-xuhao

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.