[Reserved] SHELL script beginner entry knowledge

xiaoxiao2021-03-06  43

1. Establish and run the shell program

What is the shell program? Simple SHELL program is a row

SHELL or Linux command file.

Like a program to write a high-level language, write a shell program that requires a text editor. Such as VI, etc.

In the text editing environment, enter some shell / linux command line according to the syntax rules of the shell, form a complete

Program file.

There are three ways to perform the shell program file.

(1) #CHMOD X File (Add Export Path = $ {PATH}: ~ / YourPath, add export path = $ {path}: ~ / YourPath, you can run directly under the command line, just like performing a normal command)

(2) #SH file

(3) #. File

(4) #Source File

When writing shells, the first line must indicate that the system requires that shell explains your shell programs, such as: #! / Bash,

#! / bin / csh, / bin / tcsh, or #! / bin / pdksh.

2. Variables in SHELL

(1) Common system variables

$ #: Save the number of programs command line parameters

$?: Save the return code of the previous command

$ 0: Save program name

$ *: Save all input command line parameters in the form of ("$ 1 $ 2 ...")

$ @: Save all input command line parameters in the form of ("$ 1" $ 2 "...)

(2) Define variables

The shell language is a non-type interpreted language. It is not like programming the variable in advance when programming with C / Java language. give one

A variable assignment, actually defines a variable.

In all shells supported by Linux, you can use assignment symbols (=) to assign values.

Such as:

ABC = 9 (Bash / PDKSH cannot leave space on both sides of the equal sign)

SET ABC = 9 (TCSH / CSH)

Since the variable of the shell program is not type, the user can store the character when the same variable is stored.

Integer.

Such as:

Name = ABC (Bash / PDKSH)

Set name = ABC (TCSH)

After the variable is assigned, you only need to add a $ set in front of the variable.

Such as:

Echo $ ABC

(3) position variable

When running a shell program that supports multiple command line parameters, the values ​​of these variables will be stored in position variables, respectively.

Where the first parameter is stored in the position variable 1, the second parameter is stored in the position variable 2, and the secondary push ..., shell reserved

These variables do not allow users to define them in a way. The same variable is quoted by the $ symbol.

3. How to use the quotation marks in the SHELL

Shell uses quotation marks (single / double quotes) and backslash ("/") to shield some special characters to the Shell interpreter.

The reverse quotation marks (") have special significance to the shell.

Such as:

ABC = "How are you" (Bash / PDKSH)

SET ABC = "How are you" (tcsh)

This command line consists of the string HOW Are you which consists of three words as an overall assignment to the variable ABC.

ABC1 = '@ logname, how are you!' (Bash / PDKSH)

SET ABC1 = '$ logname, how are you!' (tcsh)

ABC2 = "$ logname, how are you!" (Bash / PDKSH)

Set abc2 = "$ logname, how are you!" (TCSH)

The logname variable is to save the current user name's shell variable, assume his current value: wang. After executing two commands,

The content of ABC1 is: $ logname, how are you !. The content of ABC2 is; Wang, How are you !.

Like single quotes, the antilactors can block all special characters. But he can only block a character at a time. Not to block

A set of characters.

The function of the reverse rating is different from the above three symbols. He does not have the function of blocking special characters. However, he can pass the running results of one command to another command.

Such as:

Contents = `LS` (Bash / PDKSH)

SET Contents = `LS` (TCSH)

4. Test command in the SHELL program

In Bash / PDKSH, the command TEST is used to calculate a value of a conditional expression. They often in the conditional statements and loops

The statement is used to determine if certain conditions are met.

The syntax format of the TEST command:

Test Expression

or

[expression]

In the Test command, you can use a lot of SHELL internal operators. These operators are described below:

(1) String operator is used to calculate string expressions

TEST command | meaning

-----------------------------------------

STR1 = STR2 | When STR1 is the same as STR2, return TRUE

Str1! = STR2 | When STR1 is different from STR2, return TRUE

Str | When Str is not empty character, return TRUE

-n str | Returns true when the Str is greater than 0

-z Str | When the length of the STR is 0, return TRUE

-----------------------------------------

(2) Integer operators have functions similar to character operators. But their operation is for integers

TEST expression | meaning

---------------------------------------------

INT1-EQ INT2 | When INT1 is equal to INT2, return TRUE

INT1 -GE INT2 | When INT1 is greater than / equal to INT2, return TRUE

INT1 -LE INT2 | When INT1 is less than / equal to INT2, return TRUE

INT1-GT INT2 | When INT1 is greater than INT2, return TRUE

INT1-NE INT2 | When INT1 is not equal to INT2, return TRUE

-----------------------------------------

(3) The operator used for file operations, they can check: if the file exists, file type, etc.

TEST expression | meaning

------------------------------------------------

-d file | Returns true when File is a directory

-f file | Returns true when File is a normal file

-r file | Returns true when File is a read file

-s file | Returns true when the File file is greater than 0

-w File | Returns true when File is a writable file

-x file | Returns true when File is an executable file

------------------------------------------------

(4) SHELL logical operator is used to modify the expression of integer, string, file operator

TEST expression | meaning

-------------------------------------------------- ------------

! EXPR | Returns true when the value of EXPR is False

EXPR1 -A EXPR2 | When EXPR1, EXPR2 value is TRUE, return TRUE

EXPR1 -O EXPR2 | When the value of EXPR1, EXPR2 is at least one of True, return TRUE

-------------------------------------------------- ---------

note:

TCSH shell does not use Test commands, but the expression in TCSH can also bear the same function. TCSH

Supported expressions are the same in the expression in C. Usually in the IF and While commands.

TCSH expression | meaning

-------------------------------------------------- ----- INT1 <= INT2 | When INT1 is less than / equal to INT2, return TRUE

INT1> = INT2 | When INT1 is greater than / equal to INT2, return True

INT1

INT1> INT2 | Returns true when INT1 is greater than INT2

STR1 == STR2 | When STR1 is the same as STR2, return TRUE

Str1! = STR2 | When STR1 is different from STR2, return TRUE

-r file | Returns true when File is a readable file

-w File | Returns true when File is a writable file

-x file | Returns true when File is an executable file

-e file | Returns true when File exists

-o file | Returns true when the owner of the File file is the current user

-z file | When the FILE length is 0, return true

-f file | Returns true when File is a normal file

-d file | Returns true when File is a directory

EXP1 || EXP2 | Returns true when EXP1 and EXP2 value is at least one TRUE

EXP1 && EXP2 | Returns True when the value of EXP1 and EXP2 is TRUE

! EXP | Returns true when EXP's value is false

转载请注明原文地址:https://www.9cbs.com/read-59066.html

New Post(0)