Learning the basic concepts of the shell (b) shell script

providing a shell for the user-level system interface program sends a request to a Linux kernel. Benefits shell script is the ability to perform specified tasks at a set time. These tasks can be classified files, insert into the text, to migrate files, delete files, and so on. For some tasks cycling or consumption of resources through shell script can be better addressed.

1. write and execute scripts

At least a shell script consists of two parts, taking into account the maintenance of late, a relatively complete shell script usually consists of three parts shell parser, notes and script components. Among them, the role of the parser is to parse the script code; comments on the role of the script, writers and important comments relevant code; the script is the most important part, is a manifestation of the script function.

 Description: Specifies the first line, is script interpretation of the shell; Effect 2 to line 6 of the basic information described in the script; 8-9 lines of the script code, which is the most meaningful content script.

 For named shell script file, the suffix ".sh" (do not use extension is also possible). Execution of the script, there are two ways (set script file named hello.sh).

  • Execution uses a shell: bash hello.sh
  • The right to grant the script executable: chmod command to add the use of x permissions on the script, and then executed by ./hello.sh both ways.

2. Tracking script execution

In order to be able to find the exact script problems when testing process can use the set command to execute the script for tracking

 After using the set command, the process of implementation of the script will be output to the front, it is displayed in front of the user.

 Wherein, with a "+" output line is set to the background tracking command, but not with "+" is entered manually or script information to be output.

3.shell Variables Overview

The shell variables are usually divided into local variables and environment variables. Influencing variables are stored in the file name, file storage path, and some numbers and variables. The introduction of such variable reference to a variable shell program storage and simple and effective.

There was a variable name of contiguous memory, it is a temporary holding place program data. In the program, to apply by defining variables and named variables of storage space. Variables defined in the shell is no form data, and therefore need to define before using variable, the variable value is defined to be temporarily stored in memory, ready to be called when needed.

The value stored in the variable character string is referred to, the data string is the only form the shell.

Variables in the Linux operating system is usually divided into two types of environment variables and local variables,

Local variables (1) shell a

Local variables (also known as local variables) is the existence of variable life cycle. Local variables are defined only visible in the local process, when the process exits will disappear.

In order to log on to the user of the system to provide a work environment initialization, the operating system needs to have its own variables. You can get the initial system variables defined by the set command.

(2) shell environment variables

To ensure the system is functioning properly, Linux system needs to define some permanent variables. The values ​​of these variables will not end the program or reboot the system failure, mainly used for storing session information and work environment.

(3) shell special variables

Generally refers to special variable special symbols are represented by a specific value. More common are mainly special variables $ * and $ @ parameters indicating all locations. Where $ * indicates the currently specified all parameters, $ @ is indicated by all parameters specified in a parameter. String $ * $ @ and each parameter is referenced by an independent double quotation marks, which enables each argument in the argument list are treated as an independent value, but this value is not to be interpreted and expanded.

 

In general, the use of a variable defined earlier, just add a dollar sign in front of the variable name, like this:

your_name="qinjx"
echo $your_name
echo ${your_name}

Variable name outside the curly braces are optional, plus without anything, add braces to help identify the boundary interpreter variables, such as the following case:

for skill in Ada Coffe Action Java; do
    echo "I am good at ${skill}Script"
done

If you do not add braces to skill variables, written echo "I am good at $ skillScript", the interpreter will put $ skillScript as a variable (its value is empty), code execution is not the result we expect the same again.

Recommended for all variables braces, this is a good programming practice.

Defined variables, can be re-defined, such as:

your_name="tom"
echo $your_name
your_name="alibaba"
echo $your_name

Write is legal, but note that when the second assignment can not write $ your_name = "alibaba", using variables when it added a dollar sign ($).

 

Guess you like

Origin www.cnblogs.com/bjlssj/p/11787607.html