Marco Linux Learning (Bash Shell learning)

Source: Internet
Author: User
Tags shebang

    1. Shell principle

      In the operating system, the bottom is the hardware, and the hardware is the kernel (kemel), the kernel is the library, the library is the application, in many applications, there is a shell application, commonly known as the shell, the user can not directly interact with the kernel, then you must use the shell to implement system calls. Figure:

      650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/B6/wKiom1YEzceBf2TpAAB9Q0XEPZM200.jpg "/>

The shell is the outermost of the operating system, and the kernel (kernel) is working in the innermost layer of the operating system, because the kernel does not understand human language, and we do not remember the kernel language, and the communication between the two requires shell support.

2. Bash: Programming

Program control structure, call the machine command program files to write programs;

External commands: Each application is provided;

Program: Instruction + data

Algorithms + Data Structures

Program programming: Command-centric, design algorithms, data services to algorithms;

Object-Oriented Programming: Data-centric, design-based data structures (classes), programs to serve data structures;

Bash Procedural programming:

Sequential execution: Execute one by one

Select execute: Only one of the branches is executed

Loop execution: A piece of code to execute 0,1 or multiple times

Programming elements: variables, processes, functions

Variable: Changeable amount, name memory space

Bash Environment:

Local variables: current shell process;

Environment variables: The current shell process and its child processes;

Local variables: a function execution process;

Positional parameter variables: references to parameters passed to the script in the script, and arguments passed to the function in the function;

Special variables: $?, $*, [email protected], $#, $$

Variable type:

numeric, character;

Numerical:

Integer

Floating point number

Character:

Ascii

120:

Characters: 1, 2, 0

Value: +--

The role of variable types:

Storage space

Operation

Storage format

Language support for variable types:

Strong type:

Weak types: Variable types are not strictly differentiated;

Default storage mechanism: Bash is a character

Bash's variable use characteristics: weak type, without prior declaration;

Local variables:

Name=value

Name: variable name

=: Assignment Symbol

Value: Values

Variable name: can only contain numbers, letters and underscores, and cannot start with a number;

Reference variable: ${name}, $name

Reference:

Weak reference: "", its internal variable reference will be replaced by the value of the variable;

Strong reference: ', the variable reference of its variable will keep the original character;

Command reference: ' Command ', $ (command), the execution result of the reference command;

Declared as integral type:

Declare-i Name[=value]

Let Name=value

View all variables: Set

Life cycle:

Create

Destroyed:

Automatic destruction: Shell process termination;

Manual destruction: unset name

Environment variables:

Local variables that are "exported"

Export Name[=value]

Declare-x Name[=value]

View all environment variables: env, PRINTENV, export

Destroyed:

unset name

Script: Text file

Run the script: In fact, run a bash process, which is responsible for reading an execution logic from the script file, which is then parsed and run by the bash process;

Startup script:

(1) # Bash/path/to/script_file

(2) A permission to execute,

#./path/to/script_file

Shebang:

#!/bin/bash

The first line: shelf gives Shebang

Comment Line: #

Common options for bash:

-N: Check for syntax errors in the script;

-X: Debug Execution script;

Command status results:

The bash process is used to track the status of the successful execution of a command:

0: Success

1-255: Failure

Special variables:

$?: The execution status result of the previous command;

Boolean type:

"True": Success

"False": failure

Custom Script Status results:

Exit [n]

Note: Executing the exit command at any location in the script terminates the current shell process;

Condition test:

Defining the process implementation environment;

(1) According to the status result of the running command;

(2) test expression

Test EXPRESSION

[EXPRESSION]

[[EXPRESSION]]

Integer test: Implicit in the comparison of numerical size, so do not give reference to the variable;

$A-gt $B: Whether it is greater than, or "true", otherwise "false";

$A-ge $B: is greater than or equal to;

$A-lt $B: is less than;

$A-le $B: is less than or equal to;

$A-eq $B: is equal to;

$A-ne $B: is not equal;

String test: The larger the ASCII value, the larger the value of the character comparison;

"$A" > "$B": is greater than;

"$A" < "$B": is less than;

"$A" = = "$B": is equal to;

"$A"! = "$B": Not equal;

-Z "$A": Empty, Empty is true, otherwise false

-N "$A": not empty, not empty "true", Empty is "false"

Note: You should use [[EXPRESSION]]

File testing: The existence of test files and properties;

-e $file: Exists or is true, otherwise "false";

-a $file: ibid;

-F $file: Whether the file exists and is a normal file;

-D $file: Whether the file exists and is a directory;

-H $file: exists and is a symbolic link file;

-L $file: Ibid.

-B $file: exists and is a block device file;

-C $file: Whether it exists and is a character device file;

-S $file: exists and is a socket file;

-P $file: exists and is a pipe file;

-R $file: Whether the current user has read access to the file;

-W $file: Whether the current user has write access to the file;

-X $file: Whether the current user has execute permission on the file;

-U $file: Whether the file has suid permissions;

-G $file: Whether the file has Sgid permissions;

-K $file

Marco Linux Learning (Bash Shell learning)

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.