Shell script 1: shell script

Source: Internet
Author: User

Shell script 1: shell script

In some complex Linux maintenance work, a large number of Repeated input and interaction operations are time-consuming, labor-consuming, and error-prone. scripts are required at this time.

The advantages of script writing: batch processing, automated maintenance, reduce the burden on administrators.

Linux shell scripts are a special type of applications. There are many common shell interpreters that use internal commands in different shell periods: cat/etc/shells

[Root @ xiaolyu ~] # Cat/etc/shells

/Bin/bash is the default shell interpreter in most linux systems. All subsequent scripts are written as bash scripts.

 

I. Write the first Shell script

Put the operation commands in normal order to the file to grant the execution permission for one execution.

Let's write the first script first. sh.

Linux does not differentiate files by extension names. For convenience, I end with. sh.

[Root @ xiaolyu ~] # Vim first. sh

 

Note:

#! /Bin/bash mainly serves to declare that all I write is the bash language (I use the bash interpreter)

The second behavior comments the line. The comments do not take effect.

When writing a large script without a good comment, no one can understand what it means.

The subsequent command.

Execution Process:

 

[Root @ xiaolyu ~] # Ll first. sh # Check whether the execution permission is available
-Rw-r --. 1 root 62 Aug 30 22:58 first. sh
[Root @ xiaolyu ~] # Chmod + x first. sh # Add the execution permission to the script
[Root @ xiaolyu ~] # Ll first. sh # Check whether the script has the execution permission
-Rwxr-xr-x. 1 root 62 Aug 30 22:58 first. sh
[Root @ xiaolyu ~] #./First. sh # Run the script

Execution result:

 

E-Different Script Execution Methods

First, use absolute path for execution.

The second method is relative path execution, such ./.

The third method is to use the sh command to execute the format sh script name without the execution permission-x parameter.

Fourth, you do not need to execute the. first. sh command by using the. (Space) Script Name.

Fifth, the execution permission is not required to use the source Script Name (mainly used to take effect the configuration file)

# We recommend that you use the last three methods. do not grant executable permissions to files in the production environment;

2. variables in the script.

A variable is defined as a space that can store a variable value.

You can change the value in different environments.

By default, each shell can be considered as a different execution environment in Linux. Therefore, the variable values of the same variable name in different variable execution environments are different.

Common shell variable categories:

Custom variables, environment variables, location variables, and pre-defined Variables

Variable output

Generally, echo $ is used to output the variable echo $.

1. Custom Variables

Example 1: Define a variable named Linux with a value of 7.2

[Root @ xiaolyu ~] # Linux = 7.2 # assign a value to the variable Linux

[Root @ xiaolyu ~] # Echo $ Linux # output variable Linux Value

7.2

[Root @ xiaolyu ~] # Linux = 6.5 # assign a value to the variable linux

[Root @ xiaolyu ~] # Echo $ linux # output variable linux Value

6.5

You can define a variable directly on the command line and assign it a value. The output variable $ through echo is a special character that references the variable (the $ symbol must be used)

Note:EchoThere must be a space between it and the called variable.

The values of case variables are different.

Example 2:When two groups of variables need to be called together

[Root @ xiaolyu ~] # Echo $ Linux $ linux

7.2 6.5

Directly use echo to separate multiple variables called by $ with spaces.

Example 3: Use {} to enclose the variable names when the variable names and subsequent characters are confusing.

[Root @ xiaolyu ~] # Echo system $ {Linux}

System7.2

[Root @ xiaolyu ~] # Echo $ {Linux} system

7.2. system

Other special operations

Double quotation marks ("")

When a space is assigned to the right of the = sign, use double quotation marks to expand it.

[Root @ xiaolyu ~] # Webserver = "nginx' 1.1"
[Root @ xiaolyu ~] # Echo $ webserver
Ngnix 1.1

# Other variables can be referenced within the range of double quotes, so that existing variables can be assigned to new variables.

 

[Root @ xiaolyu ~] # Linux = 7.2
[Root @ xiaolyu ~] # System = "RHEL $ Linux"
[Root @ xiaolyu ~] # Echo $ system
RHEL7.2
[Root @ xiaolyu ~] #

 

Single quotes ('')

Special characters with other meanings, such as "$" and "\", should be enclosed in single quotes;

Other values cannot be referenced within the single quotation marks. Any character is treated as a common character. However, the value must be escaped using the \ 'symbol to avoid conflict.

[Root @ xiaolyu ~] # Kernel = 3.10 $ Linux
[Root @ xiaolyu ~] # Echo $ kernel
3.107.2 # this result is not what we want. The result we want is $3.10 $ Linux
[Root @ xiaolyu ~] # Kernel = '3. 10 $ Linux'
[Root @ xiaolyu ~] # Echo $ kernel
3.10 $ Linux
[Root @ xiaolyu ~] #

Anti-apostrophes ('')

The button at the bottom of the esc keyboard.

The anti-code is mainly used for command replacement. It allows you to assign the screen output result of a command to a variable.

Example: Find detailed information about a program in the command line

[Root @ xiaolyu ~] # Rpm-qf 'which pwd'
Coreutils-8.22-15.el7.x86_64
[Root @ xiaolyu ~] # Rpm-qf $ (which pwd)
Coreutils-8.22-15.el7.x86_64
[Root @ xiaolyu ~] # Which pwd
/Usr/bin/pwd
[Root @ xiaolyu ~] # Rpm-qf/usr/bin/pwd
Coreutils-8.22-15.el7.x86_64
[Root @ xiaolyu ~] #

# Executable commands must be included in the range of anti-apostrophes. Otherwise, an error occurs.

It should be noted that it is difficult to implement nested commands in a command by using the anti-marker, which can be replaced by the anti-marker by $ ().

An error occurs if you use anti-apostrophes for nesting !!

[Root @ xiaolyu ~] # Rpm-q 'rpm-qf' which pwd''

Rpm: no arguments given for query

Which-2.20-7.el7.x86_64

Package pwd is not installed

We recommend that you use $ () for nesting.

[Root @ xiaolyu ~] # Rpm-q $ (rpm-qf $ (which pwd ))

Coreutils-8.22-15.el7.x86_64

 

ReadCommand

In addition to the preceding value assignment, you can also use the read command to assign values. The read command is used to prompt users to enter information, to implement a simple interactive process (in fact, the command we input is an interactive process)

Read a line from the keyboard of the standard input device and use a space as the separator.

[Root @ xiaolyu ~] # Read kernel Linux#Define two variable operations at the same time
4.7.2 7.2-->Manually entered variable value
[Root @ xiaolyu ~] # Echo $ kernel
4.7.2
[Root @ xiaolyu ~] # Echo $ Linux
7.2

 

Add the-p option to set the prompt information for more interactive images and ease of use.

 

[Root @ xiaolyu ~] # Read-p "input your password:" passwd #-p specifies the prompt information
Input your password: 123456 #123456 is the value of $ passwd.
[Root @ xiaolyu ~] # Echo $ passwd # output variable
123456

Bytes ---------------------------------------------------------------------------------------------------------------

The above operations only take effect in the current bash environment, and cannot take effect in other consoles or other shells. 

We enter the subshell verification of the current shell:

 

[Root @ xiaolyu ~] # Echo $ Linux # output variables in the Current shell Environment
7.2
[Root @ xiaolyu ~] # Bash # Switch the sub-shell
[Root @ xiaolyu ~] # Echo $ Linux # output variables in the sub-shell Environment

# No output.

[Root @ xiaolyu ~] #

You can also define a global variable and assign a value to it:

[Root @ xiaolyu ~] # Export website = www.baidu.com # set global variables

[Root @ xiaolyu ~] # Echo $ website # output variables in the current environment
Www.baidu.com
[Root @ xiaolyu ~] # Bash # enter the sub-shell
[Root @ xiaolyu ~] # Echo $ website # output the variables in the sub-shell
Www.baidu.com
[Root @ xiaolyu ~] #

 

Note that the names of variables are case sensitive.

Calculation of numeric variables:

Shell script numeric operations are mostly used for script program process control (such as loop times and usage comparison)

In the shell environment, you can only perform simple integer operations.

There must be a null value between the operator and the variable. The Integer Operation is mainly performed using the internal command expr.

 

Format variable 1 operator variable 2

Among them, variable 1, variable 2 ...... The following operators are commonly used for the corresponding numeric variable to be calculated (which requires the $ symbol to be called ):

Addition operation: +

Subtraction :-

Multiplication :\*

Division :/

Modulo (remainder) operation: %

 

This can be done if you want to assign the calculation result to other variables.

2. Environment Variables

Environment variables are a type of variables that need to be created in advance by the linux system during system operation.

It is mainly used for the user's work environment, including (the user's home directory, command search path, user's current directory, login terminal, etc.) the environment variable value is maintained by the operating system itself, changes as the user's status changes

Env calls the current environment variable

The environment variable configuration file is in/etc/profile (global)

User's home directory/home/berners/. bash_profile (local) # my non-root user is berners.

 

$ PWD

The pwd command can be output only when this variable is called.

$ PATH

Define the default search path of the command. When we talk about mysql executable program optimization, we directly write the program path to this variable to input it in any directory.

$ USER

Is the user currently logged on to the system

$ SHELL

Display the Current shell

$ HOME

Displays the Home Directory of the current user. Generally, the home Directory of a non-root user is located in the/home directory, with a directory named after the current user. The root user's home directory is under/root.

Determined by the penultimate domain of/etc/passwd

 

Put the script we wrote in the default search PATH of $ PATH.

1. Write the/root directory to the path environment variable, but it does not take effect permanently.

 

 

2. Refresh the/etc/profile configuration file and it will not take effect permanently, even in another window. You can only modify the configuration file directly.

3. the executable programs under/root can be directly executed in any directory. No command is required to execute them.

 

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.