Getting Started with shell scripts--Symbol Chapter

Source: Internet
Author: User

Shell
    1. The shell is a command-line interpreter whose function is to interpret the command that executes the user, and the user enters a command that the shell interprets to execute, which is called Interactive (Interactive).
    2. The shell also has a way of executing a command called batch , in which the user writes a shell script , which has a number of commands that allow the shell to execute the commands at once without having to hit the command one at a time. Shell scripts are similar to programming languages, and there are variables and process control statements, including loops and branches. But the shell script is interpreted and does not need to be compiled, and the shell program reads and executes the commands from a single line in the script, which is equivalent to a user knocking a line of commands from the script to the shell prompt for execution. As a programming language, it is not part of the kernel of the Linux system, but it invokes most of the functionality of the system kernel to execute programs, create documents, and coordinate the operation of individual programs in parallel.

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

Shell execution script is an explanatory language, batch processing language, greatly saves the work cost

The first line of the shell script must be #! Begins, which indicates that the script interprets execution using the following interpreter.

Give me a chestnut:

SCRIPT.SH Note: This is a text file

#!/bin/bashecho "This is a test" lsls-lecho "there Be all Files"

Execution Mode :

The first mode of execution: [[email protected] shell]$ chmod +x  script.sh[[email protected] shell]$./script.sh//the second way of execution: [[email Protected] shell]$/bin/bash  script.sh

  

Execution Process:

The shell will fork a child process and call exec to execute it./script.sh this program, the EXEC system call should replace the code snippet of the process with the code snippet of the./script.sh program and execute it from its _start. However, script.sh is a text file, there is no code snippet and _start function, how to do? Actually, Exec has another mechanism, if you want to execute a text file, and the first line specifies the interpreter, replace the current process with the code snippet of the interpreter program, and from the interpreter's _ Start executes, and the text file is passed to the interpreter as a command-line argument. Therefore, executing the above script is equivalent to executing the program!

After you enter the command to execute the shell script:

  1. An interactive process (parent process) creates a child process to execute the script, and the parent process waits for the child process to terminate
  2. Sub-process program replaces bash interpreter
  3. The command to read the shell script, passing it as a parameter to the bash interpreter
  4. Child Bash reads the arguments passed into the shell script, reads a line to recognize that it is a command, then creates a child process, and the child Bash waits for the new process to terminate
  5. The new process executes the command and gives the result to the child process after execution
  6. The child process continues to read the command, creates a new process, executes the command, and returns the result to the child process until the last command is executed
  7. The child process terminates, returning the result to the interactive parent process

Note: Like export, CD, env, set these built-in commands, after typing the command line, the interactive process does not create child processes, but instead calls the functions inside bash to execute these commands, changing the interactive process.

If, at the command line, multiple commands are enclosed in parentheses and separated by semicolons, the interactive process will still create a child shell to execute the commands in parentheses:

If you do not add parentheses, it is another case, CD: Commands are executed directly under the interactive shell:

. or the source command is the shell's built-in command, which does not create a child shell, but rather executes the commands in the script line-by-row under the interactive shell.

Example:

script.sh

#!/bin/bashlsecho "#################" CD. Ls

Shell variables

Shell variables do not need to be declared, they are defined directly, because the value of the shell variable is actually a string (the default is an empty string for a variable that is not defined). When defined, the shell variable is composed of uppercase letters and underscores, and when defined, there can be no spaces on either side of the equals sign , otherwise it will be considered a command!

Types of Shell variables:

    • Environment variables: The environment variables of the shell process can be passed from the current shell process to the forked child process.
    • Local variable: exists only in the current shell process

The printenv can be used to display the environment variables of the current shell process, and the SET command can be used to display all variables (including environment variables and local variables) and functions defined in the current shell process.

A shell variable is defined to exist only in the current shell process and is a local variable. You can export local variables to environment variables using the Export command. You can use the unset command to delete a defined environment variable or local variable.

For example:

  define and export Count=5export count//step by step defining and exporting environment variables export count=5  //delete already defined environment variables unset COUNT

Variable references:

The $ sign is used to reference the shell variable, and the {} can prevent ambiguity.

For example:

Count=5echo $COUNTecho ${count}911

  

Wildcard Globbing, command substitution, single quotation marks, double quotes

1. Common wildcard characters:

* : Match 0 or more of any characters

? : Matches an arbitrary character

[several characters] : Matches one occurrence of any character in square brackets

2. Command substitution:
Anti-quote "' or $ ()

Script.sh:

The shell executes the command in the anti-quote or $ () and substitutions the result to the current command line!

arithmetic substitution :$ (())

Example: Assigning a 2-1 result to a sub and displaying the local variable sub

sub=$ ((2-1)) echo $SUB

3. Single and double quotation marks

The single and double quotation marks in a shell script are the same as the delimiter of the string , and the single quotation marks are used to keep the literal values of all the characters in the quotation marks, while the double quotation marks are special. Use double quotation marks if there are special characters in the string that need to be processed.

Note: Single quotes can no longer appear in the characters of single quotation marks.

Give me a chestnut:

#!/bin/bashecho ' \ \ ' echo ' \ \ ' echo ' Date ' echo ' ' Date '

  

Getting Started with shell scripts--Symbol Chapter

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.