Use of Shell functions

Source: Internet
Author: User

Linux scripts are huge and powerful.

Definition:

Function Name () // function can be omitted

{

Command

}

Call: directly use the function name in the script. Of course, you can also include parameters such

Function Name parameters (separated by spaces)

 

Passing parameters to a function: Like using parameters in a general script, the original parameters are returned to the shell script after obtaining parameters using the special variable $1 $2... $9. It can be understood as a C function stack call.

 

Function Exit:

Commonly used exit methods of scripts include exit 0 (normal exit of the table) and Exit 1 (indicating an error exits)

The return 0 and return 1 functions are returned, but the return state is determined by the last instruction of the function.

 

 

Below is a small experiment script written by myself.

#! /Bin/bash

Try ()

{

While [$ #-GT 0]

# Until [$ #-LT 1] // both statements can be used. Note that the test statement is written in [Space]; otherwise, an error occurs.

Do

Echo $1

Shift

Done

}

Try I am a student

Echo $1

The running result is as follows:

ThinkPad @ thinkpad-ThinkPad-R400:/Media/personal/Ubuntu $./fff hello

I

AM

A

Student

Hello

Thinktablet @ thinkpad-ThinkPad-R400:/Media/personal/Ubuntu $

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.