Shell Learning 31-day----function problem

Source: Internet
Author: User

Function

Case One :

#!/bin/bash

Hello () {

echo "Hahahah"

}

Hello

Executes the function with the result : hahaha

Case two :

#!/bin/bash

Funwithreturn ()

{

echo "The function is to get the sum of the number"

Read-p "Input first number" NUM1

Read-p "Input second number" num2

echo "The number is $num 1 and $num 2!"

Return $ (($num 1+ $num 2))

}

Funwithreturn

Ret=$?

echo "sum = $ret"

Executes a function that gets two numbers of the and .

Case Three :

#!/bin/bash

Number_one () {

echo "Number_one"

Number_two

}

Number_two ()

{

echo "Number_two"

}

Number_one

Execute command : [[email protected] tmp]# sh function2.sh

Output result :

Number_one

Number_two

Parsing : The syntax for defining Shell functions is

[Funciton] funname [()]

{

Action

[Return int]

}

Description :

1. Can be defined with function fun () or directly fun () , without any parameters ( the keyword function can be omitted ).

2. function return , can display add : return returns , if not added , The return value will be the result of the last command's run .

The shell function is similar to a shell script, which contains a series of instructions, but the shell function exists in the memory, not the hard disk file, so it is fast and the other ,theShell can also preprocess the function, so the function starts faster than the script.

The statement part can be any Shell command, or other functions can be called.

If you use the Exit command in a function , you can exit the entire script, which normally returns the part of the calling function to continue after the function ends.

You can use The break statement to interrupt the execution of a function.

Declare –F can display a list of defined functions

Declare –F can display only defined function names

Unset –F can Remove functions from Shell memory

Export –F outputs the function to the Shell

In addition, the definition of a function can be placed in a . bash_profile file or in a script that uses a function, can also be placed directly on the command line, and the function can be deleted using the internal unset command. Once the user logs off,theShell will no longer hold these functions.

To add, it is:

$: Is the name of the script itself;

$#: Is the number of arguments passed to the script;

[Email protected] : is a list of all parameters passed to the script, which is extended to "$" "$" "$" and so on;

$*: is to display all parameters passed to the script in a single string, different from the position variable, the parameter can be more than 9 , that is, is expanded to "$1c$2c$3", where C is The first character of the IFS;

$$: Is the current process ID number for the script to run ;
$? : The exit status of the last command is displayed . 0 indicates that there are no errors, others indicate errors;

For example,

The script name is called test.sh three : 1 2 3

after running test.sh 1 2 3

$* is "1 2 3"(enclosed in quotation marks)

[Email protected] to be "1" "2" "3" (to be wrapped separately)

$# is 3(number of parameters)

Where exit is used to end the execution of a program, and return is only used to return from a function.

Exit (0) indicates the normal exit execution program, if plus other values:1,2,... . can indicate an exit due to a different error cause .  

So,1,2,3 , How to correspond to different reasons? What you want it to mean, what it means .

But generally there are commonly used, general meaning: for example, 0 generally indicate normal return, exit.

Therefore , exit (0) in the main function is equivalent to return 0.

Global variables

This is similar to a global variable ( or environment variable ) in the C language

Case :

#!/bin/bash

G_var=

function mytest ()

{

echo "MyTest"

echo "Args $"

G_var=$1

return 0

}

MyTest 1

echo "return $?"

Echo

echo "G_var= $g _var"

Analysis : the main echo "return $?" when output 0 not good to understand , That 's the last line. mytest This function was executed successfully. , so the value is 0, but the value of the global variable is re-assigned inside the function. . so the result is :

MyTest

Args 1

return 0

G_var=1

The function does not provide a local variable . so all the functions share the variables with the parent script ; that , you have to be careful not to change what is not expected in the parent script . , For example :p Ath. But it also means that the other states are shared , For example the current wood with capture signal .

Shell Learning 31-day----function problem

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.