Shell script (Learn note 1)

Source: Internet
Author: User
Tags echo command

Shell Scripting Learning Record
Why learn shell scripts?
In some complex Linux maintenance work process, a large number of repetitive input and interactive operation is not only time-consuming and difficult, and error-prone, however, writing shell script program, can batch processing, automate a series of maintenance work, greatly reducing the workload of administrators.
1. Script structure of shell script?
A qualified shell script should follow the standard shell scripting structure and be able to output friendly hints that are easier to read. For scripts with more code and more complex structures, the necessary text annotations are required. Such as: #/bin/bash indicates that the script is executed with the/bin/bash program, and the second acts as a hint for the script.

The execution script appears as shown in the information

2. Redirection of scripts
Learning redirect has to learn about three interactive device files associated with it
Standard input: The default device is the keyboard, the file number is 0, the command will read in the standard input file the input data required during the execution
Standard output: The default device is a monitor with a file number of 1
Standard ERROR: The default device is the monitor, the file designator is 2, is the error message generated in the command execution
REDIRECT Operation
redirect Input (<): reads data from the specified file instead of typing from the keyboard
REDIRECT Output (>): The output is saved to the specified file, and (>>) The result is appended to the original file
Standard error Output (2>): Save the error message to the specified file; (2>>) Append
Mixed output (&>): Save standard error and standard output in one file at the same time
3. Pipeline operation
Pipe (|) The function of the left side of the command to the right command to operate again. The left-hand command is to filter out rows that do not start with "#" in a file, and the right side is the number of rows, plus the count of how many rows in a file do not begin with "#".
4. Variables
Shell variables are used to store the specific parameters (values) that the system and the user need to use, and these parameters can vary depending on the user's settings or the changes in the system environment. By using variables, shell programs can provide more flexible functionality and are more adaptable.
Common shell variables include custom variables, environment variables, positional variables, and predefined variables. Custom variable: The basic format of a variable is "variable = variable value" and no space on either side of the equal sign. Variable names need to begin with an underscore or letter, and the name cannot include special characters. You can use the Echo command to view and reference variables. Special operations that assign values to variables include

1) Double quotation mark "": double quotation marks are mainly used to define the function of the character
2) Single quote "'": characters enclosed in single quotation marks cannot reference variables, such as "$" will be treated as ordinary characters
3) Anti-apostrophe "'": Mainly used for command substitution

4) Read command: Read can be used to prompt the user for information, thus enabling a simple interactive process. Execution will read a line of content from the standard input device, separated by a space, to assign the read-in fields to the specified variable. Read-p You can enter a hint message.

5, set the scope of the variable
By default, the newly defined variable takes effect only in the current shell environment, and if you switch a child shell at this point, the variable will not be used, which is called a local variable. If you want a variable to be valid in all child shell environments, you need to set the variable to a global variable. The export command can change a local variable to a global variable, and you can assign a value directly to the variable. In other words, the Export command can either change the original local variable to a global variable, or define a global variable directly.

6. Operation of numerical variables
Several commonly used operators
+: addition operation
-: Subtraction operation
*: multiplication operation
/: Division operation
%: modulo operation
The operation of integer values is performed mainly through internal command expr, the basic format is as follows
Expr variable 1 operator variable 2 [operator variable 3] ...
7. Environment variables
Environment variables refers to a class of variables that are created in advance by the Linux system for operational needs, primarily for setting up the user's working environment, including the user's host directory, command lookup path, user's current directory, login terminal, and so on. Environment variables are automatically maintained by the Linux system and change as the user state changes. Use the ENV command to view environment variables in the current working environment, the global profile for environment variables is "/etc/profile", and each user has its own standalone profile "~/.bash_profile".


8. Position variable
Bash introduces the concept of positional variables in order to facilitate the use of command-action programs to provide operational parameters when using shell scripts. When performing a command operation, the first field represents the command word or the script program name, and the remaining string parameters are copied from left to right into the positional variable.



9. Pre-defined variables
Predefined variables are pre-defined by bash and can be used by users, but cannot be added and cannot be assigned a value. Predefined variables consist of "$" and other symbols. Here are a few common predefined variables:
$#: Indicates the number of positional parameters in the command
$*: Represents the contents of all positional parameters
$?: Indicates the return state after the previous command was executed
$ A: Indicates the name of the currently executing command or script

Shell script (Learn note 1)

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.