The Shell Foundation of Linux Learning

Source: Internet
Author: User
Tags shebang


1. Shell Introduction

The shell is the interface between the user and the Linux kernel, and if you think of the Linux kernel as the center of a sphere, the shell is around the outer layer of the kernel. When a command is passed to Linux from a shell or other program, the kernel responds accordingly.


2. How the Shell works


After the initial start of the system, the core establishes a process for each end user to execute the shell interpreter. Its implementation process is basically as follows:

(1) Read the command line entered by the user by the keyboard.

(2) Analyze the command, take the command name as the file name, and transform the other parameters into the form required by the system call EXECVE () internal processing.

(3) The terminal process calls fork () to establish a child process.

(4) The terminal process itself uses the system call WAIT4 () to wait for the child process to complete (not wait if it is a background command). When the child process is run, call Execve (), and the child process looks for the file (which is a file of the command interpreter) to the directory based on the file name (i.e., the command name), and calls it into memory to execute the program (explaining the command).

(5) If there is a & (Background command symbol) at the end of the command, the terminal process does not have to wait for the system call WAIT4 (), immediately sends the prompt, lets the user enter the next command, and goes to ⑴. If there is no & at the end of the command, the terminal process waits until the child process (that is, the process running the command) finishes processing, reports to the parent process (the terminal process), and when the terminal process wakes up, after making the necessary discrimination, the terminal process prompts the user to enter a new command


3. Procedure


The program is the instruction plus data, the function is through the instruction to process the specified data, and output results;

The program is written in two ways:

Process oriented: command-centric

Object-oriented: data-centric

Bash is an object-oriented programming language; Bash has three ways to control execution

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


4, program implementation of the classification


The program can be divided into two categories according to the way of compiling

Compile execution: Preprocessing----compile--------to-do, results: Binary Program Files

Programs developed in C, C + + and other languages

Explanation execution: The interpreter takes part in the running process, reads one row at a time, runs a line;

Python: Programming Library

Program control structure, call the programming library to complete the process of writing;

Library file: A function module, which can be called in programming, through its API;

Bash: Programming

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

External commands: Each application is provided;


5. Variables


Variables are the spaces in which variables are stored in memory;

Bash variables are divided into:

Environment variable: The valid range is the current shell process and its child processes

Local Variables: Current process

Local variables: The process of executing a function

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], $#, $$

The type of the variable (bash is the character type by default)

Numeric type

Integer

Floating point number

Character type

Ascii

such as; 120

Numeric type is 120

The character type is 1, 2, 0

Bash variables are not strictly differentiated by type, and do not have to be declared in advance

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

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/A6/wKiom1YC0V6A21XtAAY3IWSdaGM056.gif "title=" local variable. gif "alt=" Wkiom1yc0v6a21xtaay3iwsdagm056.gif "/>

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;

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/A3/wKioL1YC0lPTrTzDAAU20JHOPFI974.gif "title=" reference. gif "alt=" Wkiol1yc0lptrtzdaau20jhopfi974.gif "/>


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

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/A6/wKiom1YC08fx6iZWAAVX5YkoYpI521.gif "title=" Declaration variable. gif "alt=" Wkiom1yc08fx6izwaavx5ykoypi521.gif "/>


7. Environment variables:

Local variables that are "exported"

Export Name[=value]

Declare-x Name[=value]


View all environment variables: env, PRINTENV, export


Destroyed:

unset name

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/A3/wKioL1YC1YTw5uK4AAUHZqtyjsE961.gif "title=" 7.gif " alt= "Wkiol1yc1ytw5uk4aauhzqtyjse961.gif"/>


8. 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;

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/A6/wKiom1YC1paDJBpZAAQhyzUnrj8311.gif "title=" 8.gif " alt= "Wkiom1yc1padjbpzaaqhyzunrj8311.gif"/>


9. Command status Result:

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;

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/A3/wKioL1YC2M_CEgmXAAiKvRTvfCM306.gif "title=" 9.gif " alt= "Wkiol1yc2m_cegmxaaikvrtvfcm306.gif"/>


10. 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;

[[Email protected] ~]# cat test.sh#!/bin/bashdeclare -i x=10declare -i  y=100echo  "x= $x" echo  "y= $y" echo  "to determine the above two number is worth the size, is the output 0, otherwise the output is not 0" echo  "$x  -gt  $y: X is greater than Y "test  $x  -gt  $y;echo $?echo " $x  -ge  $y: X is greater than or equal to Y "test  $x  -ge   $y;echo $?echo  "$x  -lt  $y: x is less than Y" test  $x  -lt  $y; echo $?echo   $x  -le  $y: x is less than or equal to Y "test  $x  -le  $y;echo $?echo " $x  -eq  $y; X is equal to Y "test  $x  -eq  $y;echo $?echo " $x  -ne  $y; x is not equal to Y "test  $x  -ne   $y; echo $? [[email protected] ~]#[[email protected] ~]# bash test.shx=10y=100 judge the above two numbers worth the size, Is the output 0, otherwise the output non-010 -gt 100:x is greater than y110 -ge 100:x is greater than or equal to y110 -lt 100:x is less than y010 -le Whether the  100:x is less than or equal to y010 -eq 100;x is equal to y110 -ne 100;x is not equal to y0[[email protected] ~]#

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]]

[[email protected] ~]# cat test.sh #!/bin/basha=hellob=worldecho  "A is   $A "echo " b is  $B "echo " test string A, yes "true" (0), otherwise "false" (not 0); "  echo  $A  >  $B: is greater than; [[  $A  >  $B  ]];echo $?  echo  "$A"  <  "$B": is less than; [[  $A  <  $B " ]];echo $? echo   $A  ==  $B: Equals, [[ , $A  ==  $B  ]];echo $? echo  "$A".  !=  $B: Not equal to; [[  $A  !=  $B  ]];echo $? echo -z  $A : Empty, Empty is true, otherwise false, [[ -z  $A  ]];echo $? echo -n  $A: Not empty or true, empty is "false" or "no" "." [[ -n  "$A"  ]];echo $? [[email protected] ~]# bash test.sh    a is hellob is world test String A, B, is true (0), otherwise false (not 0);hello >  World: Whether it is greater than; 1hello < world: whether it is less than; 0hello == worlD: Whether it is equal to; 1hello != world: Whether it is not equal to; 0-z hello: Whether it is empty or "true", otherwise "false" 1-n hello: Whether it is not empty; "true", empty "false" 0 

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: Whether the file has sticky permissions;


-O $file: Whether the current user is the owner of the specified file;

-G $file: Whether the current user is a group of specified files;


Binocular operator:

$file 1-nt $file Whether 2:file1 is new to File2, File1 's last modification timestamp is later than file2;

$file 1-ot $file 2:file1 is older than File2, File1 's last modification timestamp is earlier than file2;

$file 1-ef $file whether the 2:file1 and File2 point to the same inode; test whether they are hard links to the same file;



Special Equipment:

/dev/null: Empty, bit buckets, swallow all data, and discard directly;

/dev/zero: Spit out a bunch of 0;


The Shell Foundation of Linux 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.