Linux Review Four

Source: Internet
Author: User
Tags echo command stdin

Fourth shell programming I-Getting Started

I. Basic concepts of shell scripts

A shell script is an executable plain text file that consists of multiple shell commands.

Execution of commands from top to bottom, left to right analysis and execution

Multiple whitespace between commands and parameters is ignored

Blank lines and whitespace caused by the [tab] key are also ignored

Start execution of commands by reading to the first [enter] symbol

\[enter] escaping if a line break is required in the script

#是注释, any commands and text that are added to the # symbol are treated as comments

#! Used to specify the shell name used by this shell script

Shell scripts typically have. sh as the suffix name

Second, how to execute shell script

1. Direct command line execution

Must have readable and executable permissions for the shell script file (RX)

The path should be correct: a. Absolute path/home/dmtsai/myscript.sh

B. relative path./myscript.sh

C. Place the myscript.sh in the directory specified by path by using the variable path

2. Execute in bash process

As long as you have the Read permission (r) of the shell script file

# bash myscript.sh

or # sh myscript.sh

Type: View the types of commands-T: Three possible scenarios are displayed: File---External command, alias-----command, BUILTIN-----built-in command function

-A: Lists all commands with the name XXX in the path defined by the system path.

Echo, unset: The display and setting of variables

ENV, export: Related Operations for environment variables

READ: Input of variables

Declare/typeset: Arrays and declarations

Third, the use of variables in the script

1. Display and setting of variables

Use the echo command to display the value of a variable:

# echo $ variable name

# echo ${variable name}

Set Variable: # variable name = variable Value

Cancel variable: # unset variable name

2. Environment variable function

ENV: View Environment variables

Set: View all variables: Include environment variables and custom variables (main difference: scope not used)

Environment variables are valid throughout the bash run, and custom variables are only valid in the current process

Export: Converting a custom variable to an environment variable

# Export Variable Name

3. Reading variable values from the keyboard

READ: Reading variable values from the keyboard, and user interaction, often used in shell script

# read [-pt] variable name-P: Follow-up prompt statement

-T: Number of seconds to wait after

4. Declaring variable types

Use declare or typeset to declare the type of a variable

# declare [-AIXR] variable name-I: Declared integer

-x: Declared environment variable

Iv. Data flow redirection and piping

1. Data Flow Redirection

Standard input (stdin): code 0, Symbol < or <<

Standard output (STDOUT): Code 1, Symbol > or >>

Standard error Output (STDERR): Code 2, Symbol 2> or 2>>

To input or output data from a specified stream, called data redirection

Standard output stream stdout data is output to device by default

Use > to redirect data output to a file

> overrides the file content by default, and if you want to append it, use >>

2>: An overridden way to output error information to a specified file or device

/dev/null: Ignore error messages and output to black holes

<: Standard input stream default input from keyboard

<<: The keyword used to set the end input

2. Piping

The defining symbol for a pipe command is "|"

The pipeline command will only handle stdout, and stderr will ignore

The pipeline command must be able to receive data from the previous command as StdIn

Select command Cut, grep

Cut: decomposes the same row of data-D: followed by delimited characters, used with-f

-C: Remove a fixed character range in units of characters

-F: Separates a piece of information according to-D and specifies which segment to take

Cut is a piece of information that is taken out of a row, and grep is fetching a row on condition

Sort commands: Sort, WC, uniq

Sort: For sorting input content

Uniq: Used to remove duplicate rows from the result

WC: For statistical characters, characters, lines-L: column-only travel

3. Bidirectional redirection

Tee: Can output data stream to file and screen-a: Append content to File

printf: Formatted output command for a file

Fifth Shell Programming ii-advanced

A classic case of shell scripts

Second, the use of structured control statements

if-then Statements

1.if [expression]; then

Statement

Fi

2.if [expression]; Then

Statement

Else

Statement

Fi

3.if [expression]; Then

Statement

Elif

Statement

Else

Statement

Fi

Case ... ESAC Statements

1.case $ variable name in

"Variable Value")

Statement

;;

"Variable Value")

Statement

;;

*)

Statement

;;

Esac

While Loop

1.while [Conditional expression]

Do

Statement

Done

2.until [Conditional expression]

Do

Statement

Done

For Loop

1.for variable name in value1 value2 value3

Do

Statement

Done

2.for (initial value; termination condition; execution Step))

Do

Statement

Done

Third, the use of functions

Function name ()

{

Program Segment

}

#调用函数语法为:

Name of function

Iv. Debugging the Script

SH: Supports syntax checking and debugging of scripts

# sh [-NVX] scripts.sh

Linux Review Four

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.