Day 31st: Shell script (i)

Source: Internet
Author: User
Tags processing text
Xiao Q: A person's career success is only% 15 due to his professional skills, and the other% 85 depends on his interpersonal

     Relationship. Extraterrestrial skills; soft and hard are relative, professional skills are hard skills, and based on dealing with interpersonal relationships

     The communication skills are soft skills --- Carnegie

1: Introduction
Two: shell script format
Three: shell execution mode
Four: shell view
Five: shell variables and examples

1: Introduction

Script languages: Script languages are computers created to shorten the traditional writing-compiling-linking-running process

      Programming language; is a scripting language higher than HTML hypertext markup language and lower than c · java programming language;

      Generally exists in text form, similar to a command, does not require compilation;

Features: The program code is both a script program and a final executable file.

Classification: Independent: completely depends on the interpreter during its execution;

       Embedded: Usually used in programming languages (such as C, C ++, VB, Java, etc.).

For example:

If you create a program, you can open the file with the extension .aa. When someone writes the .aa file, the script can automatically operate on it.


1. Work control language and shell

2.GUI script: a professional control computer language; interaction between the user and the graphical interface.

3. WEB programming script: Application-specific scripting language for providing custom functions of WEB pages.

4. Application-specific scripting languages: Many large-scale applications customize customary scripting languages based on user needs.

5. Text processing language: Processing text-based records is one of the earliest uses of scripting languages. awk perl

6. Universal dynamic language: Some languages, such as Perl, have evolved from a scripting language to a more general programming language.

7. Extension / embeddable languages: A few languages are designed to replace application-specific scripting languages by embedding applications.

Common scripting languages:

1. C shell: Bill Joy developed the C shell at the University of California, Berkeley in the early 1980s.

2.JavaScript: A translatable web scripting language that has been widely used in web application development, and its interpreter is called

              JavaScript engine as part of the browser

3.Python: is an object-oriented, interpreted computer programming language, Guido van Rossum in 1989

           Invented, nicknamed the glue language, it is convenient to connect programs such as c · c ++

4.PHP: is a universal open source scripting language. The syntax absorbs the characteristics of C, Java, and Perl, which is conducive to learning and makes

       Widely used, mainly applicable to the field of Web development

5.Perl: A feature-rich computer programming language that runs on more than 100 computer platforms; designed by Larry

       · Larry Wall, published on December 18, 1987

6. Nuva: It is an object-oriented dynamic scripting language that is used outside the field of code generation. The Nuva language can also be used for development.


7. Ruby, a scripting language created for simple and fast object-oriented programming (object-oriented programming)


Shell: Commonly known as the shell (used to distinguish it from the core), refers to software that "provides a user interface" (ie, a command parser)

       It is similar to command under DOS and later cmd.exe.

Two categories

Graphical interface shell: (ie GUI shell), applied to Microsoft's Windows series operating system and Linux shell package

               Including X window manager and more powerful CDE, GNOME, KDE, XFCE;

Command-line shell: (ie CLI shell) such as bash / sh / ksh / csh (Unix / linux systems) and MS-DOS


Command line shell

1.bash: GNU Bourne-Again Shell, generally the default shell in Linux;

2.csh: c shell, invoke c interpreter;

3.ksh: korn shell, AIX command; shell, invokes the default shell.


Nowadays, all work is turned to automation, and the same is true for operation and maintenance. For automated operation and maintenance, scripting is essential. Now most of Linux uses bash by default. He is relatively excellent in programming and compatible with several others. bsh, we mainly learn bash.

650) this.width = 650; "src =" "title =" 1.PNG "alt =" wKiom1YfV- yz-Q26AACrrtO7zAg685.jpg "/>

======================================================= ============================================

2: Shell script structure

As a qualified staff member, you must first archive your own scripts and categorize them, that is, place them in a specific directory, and then categorize your own script categories. The names are easy to understand and suffixed with .sh


#! / bin / bash

## Script creator

## Script creation date

## A few additional notes

## Script functions

The original text ...


1. Enter a series of commands in the file, can they be executed directly?

Yes; I think the shell used by the current user is called by default. If one of the commands is wrong

Will continue to execute

2. What is the purpose of adding # / bin / bash, since it is not necessary?

This line is not a comment, but rather the shell under which the script will run and execute the script in that shell environment.

Simple examples to understand: write scripts in the AIX, that is, ksh environment

#! / bin / ksh

##instruction manual

export LOG = / tmp / test.log

exec >> $ LOG 2> & 1

echo "a test message!"

exit 0

After executing the script, a test.log file is generated under / tmp, and the file content is "a test message!"

If you change #! / Bin / ksh to #! / Bin / bash or #! / Bin / bash

When executing the script, the following error will be reported:

export is not a shell command, that is, there is no export command in csh.

======================================================= ============================================

Three: shell execution mode

1. Switch to the directory where the script is located (that is, the working directory) and execute the shell script:

  cd / data / shell >>> ./

2. Execute bash shell script with absolute path

  /data/shell/ >>> Enter

3. Use bash or sh directly to execute the -x option to see the process

  cd / data / shell >>> bash / sh

   This method does not need to set the shell's execution permissions in advance, and does not even need to write the first line in the shell file (specify bash

   Path), because it was specified by bash;

4. Execute in the current shell environment

   cd / data / shell >>>.

The first three methods are to start a child shell environment to execute the script in the current shell (called the parent shell).

The child shell closes and returns to the parent shell;

The fourth method is to execute the script in this subshell environment, and it will remain in place afterwards.


======================================================= ============================================

Four: shell view

echo $ SHELL / $ o to see the default shell

cat / etc / shells View available shells

export environment variables exported as user environment variables

set shows all environment variables set by the shell

env list environment variables for the current user

======================================================= ============================================

Five: shell variables and examples

Shell defines the rules that variables need to follow:

1. No need to declare before using variables

2. The first character must be a letter (a-z, A-Z)

3. There must be no spaces in the middle, you can use underscores (_)

4. Cannot use punctuation

5. Can't use keywords in bash

In addition to the above explicit expression assignment, you can also use statement assignment, such as for file in ‘ls / etc’

Note: To take the value of a variable, you need to add a $ in front of the variable name; when assigning a value to a variable, you cannot use "="

      Leave space on both sides

For example:

1.a = 1 >>> $ a >>> display 1;

2. #! / Bin / bash

  read -t 5 -p "please input a number:" n user interaction command, -t timeout limit -p print

  echo $ n print n -t before -p

After execution, please input a number: >> Enter characters will be printed,

                                             Exit without typing for five seconds

3. #! / Bin / bash #! / Bin / bash

  a = 1; b = 2 a = 1; b = 2

  c = $ a + $ b c = $ 【$ a + $ b】

  echo $ c echo $ c


Display 1 + 2 after execution Display 3 after execution

This is a format for mathematical expressions, and you will remember it after more practice;

Day 31: Shell Script (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: 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.