Shell script description, shell script structure and execution, date command usage, Introduction to variables in shell scripts

Source: Internet
Author: User
Tags python script

Note content:

    • 20.1 Shell Script Introduction

    • 20.2 shell script Structure and execution

    • 20.3 Date Command usage

    • 20.4 variables in a shell script

Note Date: 2017-11-21

20.1 Shell Script Introduction

650) this.width=650; "style=" width:552px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office53289691c197466f8012587091986554/5030 "alt=" 5030 "/>

Shell Script,shell Script and Windows/dos under the same batch processing, that is, with a variety of commands pre-placed into a file, convenient one-time execution of a program file, mainly for the convenience of administrator settings or management. However, it is more powerful than batch processing under Windows and is more efficient than editing programs with other programming programs, and it uses commands under Linux/unix.

In other words, shell script is a program written using the shell's function, which uses a plain text file, writes some shell syntax and instructions inside, and then uses formal notation, pipeline commands, and data flow redirection to achieve the purpose we want.

More clearly, shell script is like the early Dos era. bat, the simplest function is to write a lot of instructions together, so that the user can easily execute multiple commands in one operation, and shell script provides an array, loop, condition and logical judgment and other important functions , allowing users to write programs directly to the shell without having to use the syntax written by traditional programs like the C programming language.

Conceptual differences:

What is the difference between shell and shell scripts? To be exact, the shell is a command-line interpreter whose function is to interpret the input commands and pass them on to the system, following a certain syntax. It provides users with an interface system-level program that sends requests to Linux to run programs, allowing users to start, suspend, stop, or even write programs with the shell. The shell itself is a program written in C, which is a bridge for users to use Linux. The shell is both a command language and a programming language (what you call a shell script). As an imperative language, it interactively interprets and executes user-entered commands, and as a programming language, it defines variables and parameters and provides many control structures in higher-order languages, including loops and branches. Although not part of the Linux kernel, it invokes most of the functionality of the system kernel to execute programs, create documents, and coordinate the operation of individual programs in parallel.

One of the most important things about shell scripting is that you can implement commands in Linux in bulk, so that you can automate operations. For example, there is a requirement for you to make a backup of the data at 3 o'clock in the morning and compress the backup file larger than 50M. If there is no shell script, you can only climb up and knock commands 3 o'clock in the morning when you need to complete this requirement. In the case of Shell scripting, I just need to write the command into the script file, and then set a task schedule, so that the system automatically executes the script file at 3 o'clock in the morning. So the shell script not only improves operational efficiency, but also makes our work less painful.

20.2 shell script Structure and execution

650) this.width=650; "style=" width:553px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office183f1ac7b3eb4616b3ff18c62c69c9f0/5031 "alt=" 5031 "/>

The first line of the shell script must be: #!/bin/bash, "#!" is a contract tag, followed by/bin/bash is to tell the system that the script needs to be executed using the/bin/bash interpreter, even with which shell. For example, like a Python script, it is different: #!/usr/bin/env python, because the scripts written in various languages do not have the same interpreter, but all need to use "#!" as a token. Simply to tell the system this is a script written in what language, Python is written by the shell or other scripting language, then the system can use the corresponding interpreter to execute the script. However, if the shell script is executed natively, it can be omitted: #!/bin/bash, because the command can be recognized, but if the other machine will not be able to execute, so the first line of the file to write such a declaration.

The file name of the shell script is the suffix of. SH, which distinguishes it from a shell script, where the comment symbol in the shell is the pound sign "#", and as with other programming languages, the line of comments is not performed by the interpreter, but note the difference between "#!" and "#", the former is the tag, the latter is the comment.

Write the first shell script:

Now that you know the basic structure of a shell script, let's write the first shell script:


The script reads as follows:

650) this.width=650; "style=" width:236px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office0ec20a43518d4cfe926f03c441d4a33b/5032 "alt=" 5032 "/>

There are three ways to execute a shell script:


chmod a+x;./


650) this.width=650; "style=" width:553px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/officef2e30790583148bc9e81bfa39abaa801/5033 "alt=" 5033 "/>

The second way to do this is to add executable permissions to the script file, so it can be executed directly as a binary file:

650) this.width=650; "style=" width:506px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office7d6b2bb502414b558881fc7674261b72/5034 "alt=" 5034 "/>

SH and bash are actually the same command, because SH is a soft-connect file for bash:

650) this.width=650; "style=" width:553px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office56de4d3907d5415a81d25ed89fd27d5e/5035 "alt=" 5035 "/>

Note that #!/bin/bash is written in the first line of the file is declared, and if two lines of #!/bin/bash are written, then the second line is commented:

650) this.width=650; "style=" WIDTH:487PX; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office7960805992ce43a58ab9a6745fcb24af/5036 "alt=" 5036 "/>

Writing the system commands in the script will be executed in bulk:

650) this.width=650; "style=" width:553px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/officee8a835e81096465eaae04b09edf7eed4/5037 "alt=" 5037 "/>

Operation Result:

650) this.width=650; "style=" width:553px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office2a282bd1426f422684876fc654376b32/5038 "alt=" 5038 "/>

To view the script execution process bash-x

650) this.width=650; "style=" width:553px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/officec322d8b589f9465d8985abcae88ccb68/5039 "alt=" 5039 "/>

Each "+" represents the executed command followed by the execution result of the command.

See if the script is syntactically incorrect bash-n

650) this.width=650; "style=" width:496px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office1e59ea4222954daba1b821be2fc0a132/5041 "alt=" 5041 "/>

The absence of any output means that the script has no syntax errors.

For example, I have deliberately written the script syntax incorrectly:

650) this.width=650; "style=" width:484px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/officeee83b32450784498b8d16fba9a3cca10/5042 "alt=" 5042 "/>

, you will be prompted with a syntax error and can also tell you where the error occurred on the first line of the file.

However, it is important to note that this is just a syntax error to check the script, and it will not be checked if your system command is incorrectly written.

20.3 Date Command usage

650) this.width=650; "style=" width:553px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office6f877272d68f4fdc82b977d71d954a1b/5043 "alt=" 5043 "/>

Date is a time-date-related command that, if used directly, displays the current system's date and time:

650) this.width=650; "style=" width:400px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office0728ecfd51a244c9933f11e6fe4c2a73/5044 "alt=" 5044 "/>

The date command is very useful in shell scripts, such as when I'm backing up a file or logging some log information, you can use this command to mark a time for these backup files or log information.

The following are some common uses of the date command:

    1. Use a time symbol to specify a date format to output the current month and year date:

650) this.width=650; "style=" width:523px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office4d1795a7c6d8465397245e700a93ed5d/5045 "alt=" 5045 "/>

About interpreting the time notation:

%Y 4-bit years (year)

%y 2-bit year

%M represents the current minute (Minute)

%m represents the current month

%d Outputs the current month and date in 2-bit format

%d indicates the current date (day)

%F output current Month day, with-as delimited

%H output Current hour time

%h Output the current month

%s Output timestamp

%s Output seconds

%T output current time, separated by:

%W output This week is the first week of this year

%w output This week is the first week of the month

    1. Outputs the time separated by a colon:

650) this.width=650; "style=" width:418px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/officee41c9f88174d4b06820412d8f40a7c37/5046 "alt=" 5046 "/>

    1. Convert timestamp to current date:

650) this.width=650; "style=" width:450px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office9bfb8ae5e01f4c46a7246a054f8dc788/5047 "alt=" 5047 "/>

    1. Date after one day of printing:

Date-d "+1 Day" +%f

    1. Print the date of the day before:

Date-d "-1 day" +%f

    1. Print date one months ago:

Date-d "-1 month" +%f

    1. Print one minute ago:

Date-d "-1 min"

8. Print the date one year ago:

Date-d "-1 Years" +%f

9. Print an hour ago:

Date-d "-1 Hour" +%t

10. Convert a date to a timestamp:

Date +%s-d "2017-09-06 06:52:16"

Another time-related command: The cal,cal command can display the calendar:

650) this.width=650; "style=" width:388px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/officee84bfc3ae42746f3a8b58908bbd770c9/5048 "alt=" 5048 "/>

20.4 variables in a shell script

650) this.width=650; "style=" width:553px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/officeb34d607f0ea54411b9cc324084c3f2a4/5049 "alt=" 5049 "/>

Variables in shell scripts have the same meaning as variables in other programming languages, are used to store one data, and can be called using variable names, and the syntax of the shell script is similar to the syntax of other scripts, without the need to use semicolons as the end of the statement.

The shell script defines the rules for variables:

    • The first character must be a letter (a-z,a-z).

    • You can use an underscore (_) without spaces in the middle.

    • Punctuation cannot be used.

    • You can't use the keywords in bash (you can see the reserved keywords using the help command).

Define a variable:


Name= "Lisi"

To call a variable, precede the variable name with $:

Echo $a

Echo $name

Read-only variables:

Use the readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be changed.

The following example attempts to change a read-only variable, resulting in an error:

650) this.width=650; "style=" width:340px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office72e3164dc76f48f3b508953568bdf7c0/5050 "alt=" 5050 "/>

Run the script with the following results:

650) this.width=650; "style=" width:383px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/officebc6b8eb2ebd0430eaa9e09a70f568587/5051 "alt=" 5051 "/>

To delete a variable:

Use the unset command to delete data in a variable. Grammar:

unset name


650) this.width=650; "style=" width:333px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/officeeb371928761a4a99aca9f52c5d27959e/5052 "alt=" 5052 "/>

Operation Result:

650) this.width=650; "style=" width:458px; "src=" aafe5be659df1362210cddcacdb14763/xmlnote/office0b0f989212ae48c1950c3eeb21ed68a9/5053 "alt=" 5053 "/>

Because the contents of the variable are deleted, the output is empty.

Variable type:

When you run the shell, there are three different variables:

1) local variable local variables are defined in a script or command, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.

2) Environment variables all programs, including shell-initiated programs, can access environment variables, and some programs require environment variables to keep them running properly. Shell scripts can also define environment variables when necessary.

3) Shell variables shell variables are special variables that are set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation.

If the picture in the text is invalid, please click on the following link to view:

Hell script Description, shell script structure and execution, use of the date command, variables in shell scripts

This article is from the "zero" blog, make sure to keep this source

Shell script description, shell script structure and execution, date command usage, Introduction to variables in shell scripts

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: 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.