Shell Programming Basics

Source: Internet
Author: User
Tags shebang

Take a look at the logic operations in bash before you learn the basics of Shell programming

Bash features multi-command execution

Command;command1 ... Sequential execution structure

Select the execution structure:

Logical operation:

VS: Logical multiplication,&& (equivalent to concatenation in a physical circuit)

0: Successful execution of-->true

1~255 Failure-->false

T&&t-->t

T&&f-->f

F&&f-->f

F&&t-->f

"and" short-circuit logic operations: As long as the first operand is false, the result of its logical operation must be false, if COMMAND1 can succeed, then COMMAND2 will be executed, if COMMAND1 execution fails, COMMAND2 will not be executed

Or: logical addition | | (equivalent to parallel in the physical circuit)

Only two false is False

t| | T-->t

t| | F-->t

f| | T-->t

f| | F-->f

"or" short-circuit logic operation: As long as the first operand is true, the result of its logical operation must be true

command1| | COMMAND2

As long as the COMMAND1 execution succeeds, COMMAND2 will not have to execute the necessary, only if COMMAND1 execution fails, cOMMAND2 will be executed

Non: logical inverse monocular operator!

!true=!false

!false=!true

Priority of Logical programming:!>&&>| |

Demogan Law:! (a&&b) =! a| |! B

! (a| | b) =! a&&! B

XOR: Binocular operator

is different than the two operands, if different, the result of the logical operation is true, if the same, the result of the logical operation is False

Next, Shell script programming. The point you need to know is program = instruction + data

Programming: Simply write the program source code.

In order to enable users to use the computer, you can let the computer do some tasks in a non-interactive way, you need to weave these tasks into a file, so that the computer read sequentially, so that the function

And programming language is divided into high-level language and low-level language

High-level languages are classified according to how the source code is handled

Compiled run language: source code--compiler (compile processing)--assembler (complete assembly operation)-->[Linker (link to library)]--> compiled into binary files that can be executed directly

Explain the running language: source code--and directly start the interpreter program corresponding to the source code, run by interpreter side interpretation

Depending on whether the implementation of the functionality in the programming process is calling the library or calling an external program file

Full programming language: Programming with libraries or programming components

Script programming Language: Interpreter

Shell script: Call the shell program, next call the required external command file, command interpreter,

Provides a single functional programming interface, support for over-programmed programming logic

According to the program's writing standard is divided into

Over-programming languages

The instruction (algorithm) centered around the implementation of the instruction (algorithm) design data and data structure, the information for the instruction service

Implementation forms of algorithms and directives

Sequential execution

Select Execute

Loop execution

Object-Type programming language:

Data-centric, instantiating data into classes, and deploying directives around data requirements (algorithms)

Classes, properties, methods

Low-level languages: programming languages

Shell scripting is a procedural programming language that interprets running languages and relies on external program files to run

Shell script:

1. Plain text documents-data stored in a file is stored in a character unit

2. A combination of numerous commands to solve user problems based on user needs

3. Many commands do not have "idempotent", and in shell scripts it is necessary to use a large number of program logic to determine whether a command satisfies the operating conditions to avoid serious errors during operation

Here's an explanation of the power of execution: The result of any command executing multiple times is consistent

Write the shell script, also have a certain writing specification to the content

Code content in a shell script

1. The first line must be the shebang (interpreter path), the shebang must occupy an absolute beginning, and at execution time the corresponding interpreter has explained many commands in the script;

#!/bin/bash

#!/bin/sh

#!/usr/bin/python

#!/usr/bin/per

2. In the shell script, except for Shebang, where the content of the absolute beginning of the # occupies a comment line, the interpreter ignores the contents of such a line

3. Explanation ignores all blank lines in the script

4. Lots of commands and keywords (if,else,then,do,while,)

Note: Once the shell script is running, it is in the current shell to open an interpreter (child shell) explaining the execution of the code according to Shebang's instructions, and the shell script content is implemented in a child shell process

There are many scripts to write the shell, we can use Nano, VI, Vim, Emacs and so on to write, the recommended use of Vim, the contract under the name of the script file suffix. sh, also can not add

How the script works:

1. Assign execute permissions to the script file and run the file directly;

chmod +x/path/to/scripi_file

If the script file name does not give the path when the script is executed, you must ensure that the script files are found in the path stored in the paths variable

2. Run the script directly with the interpreter and use the script as a parameter to the interpreter command

Bash/path/to/scripi_file




















Shell Programming Basics

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.