Redirection, environment variables, Shell functions, and echo command usage in advanced shell script Programming

Source: Internet
Author: User
Tags echo command

Redirection, environment variables, and Shell functions of advanced shell script Programming


Shell is not limited to simply executing commands line by line. One of the more powerful functions is the ability to create functions and function libraries. You want to customize the command line interaction mode or automate file processing tasks. Consider using shell scripts.

1. Input and Output redirection

Stdin

Stdout

Stderr

Redirection Operator

>
------- Redirect stdout to a file

<
------- Redirect stdin to a file

>
------- Add stdout to the end of a file

|
------- Get output from one program or process and send it to another program or process

<Delimiter associates the current input stream with stdin until it reaches the specified delimiter.

[Root @ localhost shell] # Cat <End
> The Cat
> Sat on
> Mat.
> End
The cat
Sat on
Mat.

[Root @ localhost shell] # Cat <End
> The Cat
> Sat on
> Mat.
> End
The cat
Sat on
Mat.

> The operator has several modifier tags that can change its behavior.

> & Stdout and stderr are redirected at the same time.

>! Will force the file to be created in append mode, or overwrite an existing file in normal mode.

> @ Open a file in binary mode

2. Command replacement: extension of quotation marks and parentheses

#! /Bin/sh
Lines = 'wc-l filelist'
Echo $ lines

[Root @ localhost shell] #./p206
28 filelist


Lines = $ (WC-l filelist) // symbol Extension

3. Use environment variables and shell Variables

The Export command is used to modify shell variables. Many environment variables are displayed without the option.

Export Path =/bin

SET command to view the shell variables set in the environment

4. Shell functions

<1>. Format

Name () {command ;}

Shell functions must be declared before they can be used. You can also return values.

#! /Bin/sh
Repeat (){
Echo-en "I don't know $1 $2 \ n"
Echo-en "Hello \ n"

Return 0

}
Repeat your name

<2> nested functions and Recursion

#! /Bin/sh
Number_one (){
Echo "this is first one func speaking ..."
Number_two
}
Number_two (){
Echo "this is second func speaking ...."
}
Number_one

<3> Scope

The scope includes the full and local regions: Use the local keyword to identify local variables

#! /Bin/sh
Scope (){
Local SOC = 1
Gblsoc = 2
Echo "Local SOC in func = $ Soc"
Echo "Global gblsoc in func = $ gblsoc"
}
Scope
Echo "Local SOC outside func = $ Soc"
Echo "gblsoc outside in func = $ gblsoc"

Exit 0


Supplement: Echo command usage:

The echo command is used to display a piece of text on the monitor, which generally serves as a prompt.

The general format of this command is Echo [-N] string.

Option n indicates that the output text is not followed by a line break. Strings can be enclosed by quotation marks or without quotation marks. When you use the echo command to output a string with quotation marks, the string is output as is. When you use the echo command to output a string without quotation marks, each word in the string is output as a string, each string is separated by a space.

Function Description: displays text.

Syntax: Echo [-ne] [String] or ECHO [-- help] [-- version]

Note: Echo sends the input string to the standard output. The output strings are separated by blank characters, and the line number is added at the end.

Parameter number:-N. Do not wrap the line at the end.

-E if the following characters appear in the string, it will be specially processed and will not be treated as a general

Text output:

\ A sends an alert;

\ B Delete the previous character;

\ C does not end with a line break;

\ F line feed, but the cursor remains at the original position;

\ N wrap and move the cursor to the beginning of the line;

\ R move the cursor to the beginning of the line without line breaks;

\ T Insert tab;

\ V is the same as \ F;

\ Insert \ characters;

\ NNN inserts the ASCII characters represented by NNN (octal;

-Help: Display help

-Version: displays version information.

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.