Chapter 2 shell program design

Source: Internet
Author: User

Shell is a program that interfaces between users and Linux systems. It allows users to input the commands to be executed to the operating system. Many powerful functions can be implemented through shell.

1. Redirection

File descriptor: 0 indicates the standard input of a program, 1 indicates the standard output, and 2 indicates the standard error output.

You can use> file.txt to redirect the program standard output to a file. By default, the content of this file is overwritten.

> File.txt: add the program standard output to the file.txt file.

If you want to redirect the standard error output, add the file descriptor number you want to redirect to before the> operator.

$ Kill-HUP 1234> killout.txt 2> killerr.txt // redirect the standard output and standard error output to different files respectively.

$ Kill-l 1234> killouterr.txt 2> & 1 // redirect both standard output and standard error output to the same file

$ Kill-l 1234>/dev/null 2> & 1 // yes .... Redirect to the recycle bin (discard output information)

Similarly, the input can also be redirected.

$ More <killout.txt

2. Summary

Linux seldom uses the file extension to determine the file type.

3. Shell Syntax:

Variable: String, number, environment, and Parameter

Condition: Boolean value in Shell

Program Control: If, Elif, for, while, Case

Command list

Function

Shell built-in commands

Obtain the command execution result

Here document

3.1 Variables

In shell, you can add the $ symbol before the variable name to access its content.

$ Value = "Yes dear" // If a string contains spaces, it must be enclosed in quotation marks. In addition, there cannot be spaces at both ends of the equal sign.

$ Read value // use the READ command to assign a value to a variable.

Generally, parameters in the script file are separated by spaces (spaces, tabs, or line breaks). To include one or more blank characters in a parameter, you must add quotation marks to the parameters.

$ Myvar = "Hi there"

> Echo $ myvar

> Echo "$ myvar" // double quotation marks. The variable will be replaced.

> Echo '$ myvar' // single quotes. The variable is not replaced.

> Echo \ $ myvar // use \ to cancel the special meaning of the $ symbol

Hi there

Hi there

$ Myvar

$ Myvar

Environment Variable

$ Ifs // input domain delimiter. When the shell reads the input, it provides a group of characters used to separate words, usually spaces, tabs, and line breaks.

$0 // shell script name

$ # // Number of parameters passed to the script

Conditions

[There should be spaces between conditions (when assigning values to variables ...)

If [-F Fred. C]; then


The set-GID and set-UID flags do not work for shell script programs. They are only useful for executable binary files.


Shell considers all variables contain strings by default. Foo = 43 here, 43 is treated as strings.


Until statement

Until conditions // execute the following statements to know that conditions are true

Do

Statements

Done

Case statement // case will use the first matching mode. Even if there is a more precise matching in the subsequent mode, case does not look for the best match.

Case variable in
Pattern [| pattern]...) statements; // note that it ends with a double semicolon.
Pattern [| pattern]...) statements ;;
...
Esac

Note * wildcard characters do not work in quotation marks

And list

Statement1 & statement2 & statement3 &&... // The next statement is executed only when all the previous statement is successfully executed. // It checks the return value of the previous command.

Note that executing another script in a script program is much slower than executing a function.








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.