[9-13] Basic concept of Shell series

Source: Internet
Author: User

This paper aims to review the basic concepts of shell, programming model, variable type, script specification, language structure, script execution mode


Linux hierarchy and Shell basic concepts

The Linux hierarchy is roughly divided into layers

Note: Basically, the underlying differences are masked at all levels, and can be managed at the bottom of the call and at the same time as the previous layer [or multilayer] Service

Hardware Platform: Hardware layer has a variety of different manufacturers of various hardware interfaces, to provide services to the upper layer

OS Kernel: Kernel layer, shielding the underlying hardware differences, providing core functions, scheduling resources to run the command program, the upper layer can be system call

LIB: library file, service access point in kernel, for security, convenience and so on encapsulation into library file, upper layer can make library call

Shell: A bridge between user and Linux, divided into CLI shell[bash, zsh, tsh], GUI shell[gnome, KDE, etc.]


      • shell is the command language: a command that can be interactively interpreted and executed by user input, drawn to the kernel to execute

      • shell is a programming language: can define various variable parameters and provide control structures such as loops, branches, judgments, etc.

      • This series is designed to discuss some basic knowledge and concepts of shell programming

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/7D/wKiom1X_fi6xHntAAAnjXO5qzWc188.bmp "title=" Baidushurufa_2015-9-21_11-48-45.bmp "alt=" Wkiom1x_fi6xhntaaanjxo5qzwc188.bmp "/>


Shell programming Model

The high-level language programming model mainly has the process oriented, the object-oriented, the popular

Process-oriented: Analyze the steps to solve the problem, encapsulate the code into functions to implement these steps, using the time one by one call;

Object-oriented: the objects that make up the problem are decomposed into objects, and the object is not designed to complete each step, but to describe the behavior that occurs in the process of solving the whole problem.

Example: A chess process that illustrates process-oriented and object-oriented.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/7E/wKiom1X_j2DCv518AAwPUJYcRuQ561.bmp "title=" Baidushurufa_2015-9-21_13-2-12.bmp "alt=" Wkiom1x_j2dcv518aawpujycruq561.bmp "/>

Note: In general, shell programming is process-oriented, decomposition implementation, and final invocation complete automation. But the new concept proposes shell object-oriented programming


Shell Variable Type

There are roughly 5 types of variables in the shell

Environment variables: scope current shell process and its child shell process

local variable: scope only Current shell process

Local Variables: A local code fragment in the current shell process, usually a function context

Positional variables: Refer to grep back reference, reference script parameter, $ $ $

Special variables: Special variables set by the shell program. Some are environment variables, some of which are local variables that guarantee the shell's normal operation.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/7D/wKioL1X_q0_jYJ7zAA0WwLioUtI144.bmp "title=" Baidushurufa_2015-9-21_14-50-56.bmp "alt=" Wkiol1x_q0_jyj7zaa0wwliouti144.bmp "/>


Shell Scripting Specification

Any programming language, will follow a certain programming norms, such as C language include, etc., the shell also has its own programming specifications


Use a shell script example to illustrate the specification

1
2
3
4
5
6
7
8
9
10


#!/bin/bash

#Author: by Lance,2015-09-21 13:37:03
#Functions: Shell Script Specification

Cat "/etc/system-release"---> Fill commands
If [];then---> Fill judging conditions

Statement1

Fi

....

Source

Note the point:

<1> must specify shebang[Magic], that is, the format of the file, usually saved as *.sh

<2> Comment information: Usually author, time, function, description, etc., but not limited to this, do not recommend Chinese comments

<3> Content part: Can fill various commands, language structure, condition judgment, branch and so on, realize the logic strengthens

<4> Code indentation, easy to read, especially in pairs appear by indenting the same length, more visually readable

<5> symbols, Process Control recommendations once written, avoid omission, first overall after the details


Shell language Structure

With most high-level languages, there are 3 language constructs that can be used in combination

Sequential execution--default rule, execute statement by clause

Select Structure-to-branch, make conditional judgments, and meet the criteria of the branch to execute; non-conforming Skip

Loop Structure--repeats the same piece of code for a finite number of times until a bounce condition is met, otherwise it is a dead loop

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/7C/wKioL1X_nzbgEErUAAu0eEp29xE866.bmp "title=" Baidushurufa_2015-9-21_14-0-7.bmp "alt=" Wkiol1x_nzbgeeruaau0eep29xe866.bmp "/>

How shell scripts are executed

There are typically 3 ways to execute scripts

./test.sh execution, preconditions the process initiator has X permission on the file, essentially by invoking a new shell process to run, notice the variable change

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/7C/wKioL1X_oiCyMGnAAARu-OWW4Q4138.bmp "title=" Baidushurufa_2015-9-21_14-7-11.bmp "alt=" Wkiol1x_oicymgnaaaru-oww4q4138.bmp "/>

     bash test.sh execution, essentially invoking the bash command, executes test.sh as a parameter to the command, and the shell script can do without the X permission

    650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M00/73/7F/wKiom1X_ N9ocves-aamimakbgja135.bmp "title=" baidushurufa_2015-9-21_14-9-10.bmp "alt=" wkiom1x_n9ocves-aamimakbgja135.bmp "/

     source test.sh or. test.sh[. After the shell script is imported into the current shell process to execute, note that the variables in the script are also imported into the current shell process

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/7C/wKioL1X_of7zxq7jAAQV-Iz_wOY763.bmp "title=" Baidushurufa_2015-9-21_14-11-18.bmp "alt=" Wkiol1x_of7zxq7jaaqv-iz_woy763.bmp "/>

This article is from the "Blue Warehouse" blog, be sure to keep this source http://bluebox.blog.51cto.com/8852456/1696740

[9-13] Basic concept of Shell series

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.