A brief analysis of the symbolic text of shell script knowledge _unix Linux

Source: Internet
Author: User

shell

The shell also has a way to execute commands called batch processing. The user writes a shell script (Script) in advance, which contains many commands, so that the shell can execute these commands at once without having to type commands one by one. Shell scripts are very similar to programming languages, and there are variables and flow control statements, including loops and branches. However, the Shell script is interpreted and executed without compilation. The Shell program reads and executes these commands line by line in the script, which is equivalent to a user typing the commands in the script line by line to execute the Shell prompt. As a programming language, although it is not part of the Linux system kernel, it calls most of the functions of the system kernel to execute programs, create documents, and coordinate the operation of various programs in a parallel manner.
Common shells in Unix systems are: sh, csh, ksh, tcsh, bash

Use the command: vim / etc / shells to view the bash supported by the current system



shell execution script

The shell execution script is an interpreted language and batch processing language, which greatly saves the work cost

The first line of the shell script must start with #! At the beginning, it means that the script is interpreted and executed using the following interpreter.

Give a chestnut:

//script.sh Note: This is a text file

#! / bin / bash
echo "this is a test"
ls
ls -l
echo "there are all files"
Implementation modalities:

// The first way of execution:
[admin @ localhost Shell] $ chmod + x script.sh
[admin @ localhost Shell] $ ./script.sh
 
// Second execution method:
[admin @ localhost Shell] $ / bin / bash script.sh


 

Implementation process:

The shell will fork a subprocess and call exec to execute the ./script.sh program. The exec system call should replace the subprocess code segment with the ./script.sh program code segment and start execution from its _start. However, script.sh is a text file, there is no code segment and _start function, what should I do? In fact, exec has another mechanism. If you want to execute a text file, and the first line specifies the interpreter, then The code segment of the interpreter program replaces the current process and executes from the interpreter's _start, and this text file is passed to the interpreter as a command line parameter. Therefore, executing the above script is equivalent to executing the program!

After entering the command to execute the shell script:

     1. The interactive process (parent process) creates a child process for executing scripts, and the parent process waits for the child process to terminate
     2. Subprocess program replaces bash interpreter
     3. Read the commands of the shell script and pass them to the bash interpreter by means of parameter passing
     4. The sub-bash reads the parameters passed by the shell script, reads a line and recognizes that it is a command, then creates a sub-process, and the sub-bash waits for the new process to terminate
     5. The new process executes the command, and after the execution, the result is handed to the child process
     6. The child process continues to read the command, create a new process, the new process executes the command, and returns the result to the child process until the last command is executed
     7. The child process terminates and returns the result to the interactive parent process
Note: Built-in commands like export, cd, env, set, etc. After typing the command line, the interactive process will not create a child process, but call bash internal functions to execute these commands, the interactive process is changed.

If you execute multiple commands in parentheses on the command line and separate them with semicolons, the interactive process will still create a subshell to execute the commands in parentheses:



If you do not add parentheses, it is another case. The cd .. command will be executed directly in the interactive shell:



. Or the two commands source are built-in shell commands, this way will not create a sub-shell, but directly execute the commands in the script line by line in the interactive shell.

illustration:

script.sh

#! / bin / bash
ls
echo "#################"
cd ..
ls






shell variables

 Shell variables do not need to be declared, they can be defined directly, because the values of shell variables are actually strings (the default for undefined variables is an empty string). When defined, shell variables consist of capital letters and underscores, and there must be no spaces around the equal sign when they are defined, otherwise they will be considered commands!

Types of shell variables:

     1. Environment variables: The environment variables of the shell process can be passed from the current shell process to the child process of the fork.
     2. Local variables: only exist in the current shell process
Use printenv to display the environment variables of the current shell process; use the set command to display all the variables (including environment variables and local variables) and functions defined in the current shell process.

A shell variable only exists in the current Shell process after being defined, and is a local variable. Use the export command to export local variables as environment variables. Use the unset command to delete the defined environment variables or local variables.

E.g:

// Step by step, define before exporting
COUNT = 5
export COUNT
 
// Define and export environment variables in one step
export COUNT = 5
 
// Delete the environment variables that have been defined
unset COUNT
 Variable reference:

 To quote shell variables, use the $ symbol, and add {} to prevent ambiguity.

E.g:

COUNT = 5
echo $ COUNT
echo $ {COUNT} 911



Wildcards Globbing, command substitution, single quotes, double quotes

1. Common wildcards:

     *: Match 0 or more arbitrary characters

     ?: Matches an arbitrary character

     [Several characters]: Match one occurrence of any character in square brackets

2. Command substitution:

Backticks, or $ ()

script.sh:



The shell will first execute the command in backticks or $ (), and substitute the result into the current command line!

The difference between backticks and $ ():

    The backquote itself escapes \ and retains its meaning. If we want to play the special meaning of \ in the backquote, we must use 2 \ to represent.

    In backticks: \\ = \

    $ (): \ = \

    Note: The backticks are the old usage, () is the new usage, whether it is in the learning test or in actual work, () is the new usage, whether in the learning test or in actual work, ( ) Are recommended.

Arithmetic substitution: $ (())

For example: assign the result of 2-1 to SUB and display the local variable SUB

SUB = $ ((2-1))
echo $ SUB
3. Single and double quotes

Single quotes and double quotes in shell scripts are the delimiters of the string. Single quotes are used to maintain the literal value of all characters in quotes, and double quotes are special in some cases. If there are special characters in the string that need to be processed, double quotes are used.

Note: Single quotes can no longer appear in the characters marked with single quotes.

Give a chestnut:

#! / bin / bash
 
echo '\\'
echo "\\"
echo '`date`'
echo "` date` "


The above is the whole content of the symbol chapter about shell script knowledge. I hope that the content of this article will be helpful to everyone's 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.