A comprehensive application of shell functions and scripts

Source: Internet
Author: User

1. Shell function: The shell allows a set of command sets or statements to form an available block called a shell function.

A function consists of two parts: a function title, a function body.

The title is the function name. A function body is a collection of commands within a function.

The title name should be unique, and if not, the result will be confused because the script will first search for a function to call the appropriate shell before viewing the calling script.

2, the format of the definition function is:

Function name () {

Command 1

...

}

If you prefer, you can precede the function name with a keyword function, depending on the user.

function functions name ()

{

Command 1

...

}

3, for example:

Example 1: Deleting a blank line in a file

This script (del.lines) can process one or more files. Each file must verify existence before using SED to delete empty lines.

The output of SED is imported into a temporary file with $ $ in the filename, and finally the temporary file is moved back to the original file.

The script uses the shift command to get all the file names and process all the files with the while loop until the process is complete

So far. You can use Del.lines--help to get a short help

Add Execute permissions to script files after saving: chmod +x del.lines

To execute a script for testing:

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.