Shell script Learning (III): Shell functions and some common symbols and commands

Source: Internet
Author: User
Shell script Learning (III): Shell functions and some common symbols and commands
I. Shell functions

Like C, Shell functions encapsulate commands and statements that have fixed functions and are usually used repeatedly in a single function, if you want to use this function, you can call this function directly.

1. Function Definition:

(1)

function_name(){Command 1...Command n}

(2)

Function function_name(){Command 1...Command n}
2. There are two ways to call a function:

(1)Return_fun = 'function _ name [arg1 arg2. ..]'

(2)Function_name [arg1 arg2. ..] echo $?

2. Some common symbols and commands in Shell 1. Trap

The trap command is used to capture signals in shell, and there are three processing methods:

(1) When shell receives a signal with the same value as the value in the signal list, the first form of Trap Command will execute the command string in double quotation marks: Trap "commands" signal-list

(2) default Signal Processing Method: Trap signal-list

(3) Ignore Signal Processing: Trap "" signal-list

In TRAP statements, single quotes and double quotes are different. When the shell program first encounters a trap statement, it will scan the commands in commands. If commands is enclosed in single quotes, shell will not replace the variables and commands in commands. Otherwise, the variables and commands in commands will be replaced with the specific values at that time.

2. to prevent wildcard Extension: single quotation marks (''), double quotation marks (" "), backslash (\) 3. the pipeline (|) uses the output of a command as the input of another command. For example, grep "hello" file.txt | WC-l4.> write the file and overwrite the old file. 5.> Add it to the end of the file to keep the content of the old file.
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.