[Reproduced] BASH script programming speed

xiaoxiao2021-03-06  42

basis

-

All scripts must tell Linux what to use to make this script interpretation. So the first line of the BASH script should be:

#! / bin / bash

If you want to turn this script to an executable, you need to enter the command line:

~ Chmod x filename

Variables

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

1. Create a variable and assign a value:

Varname = value

A variable is no data type, which can be a string, a number, and the like, so it is not necessary to define it.

2. To use a variable, we must add $: in front of the variable name:

Echo $ Varname

3. If you want to use the parameters on the command line in the script, we use the $ #, "#" here indicates the serial number of the parameters on the command line. For example, there is a command:

~ Command Var1 Var2 ... VARX

Then call the parameters in the above command line in the script should be:

Echo var1 is $ 1, VAR2 IS $ 2 ...

Built-in variables in BASH

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

$ 1 - $ N

Save the parameters from 1 to N on the command line

$?

The end value after the end of the last command is saved

$ 0

Save the first word on the command line (the name of this scriptor)

$ *

Save the command line from 1 to n all parameters ($ 1 $ 2 ... $ n)

"$ @"

The command line is saved from 1 to n, and placed in the middle of the double quotes ("$ 1" "$ 2" ... "$ n")

Quote Marks

-----------

Double quotes ("Like this")

You can let the interpreter ignore the space inside, treat all the characters inside as a value, but will not ignore some special characters.

Single number ('like this')

You can let the interpreter ignore all special characters inside, including spaces, and treat all characters in it as a value.

Back single quotes (`Command`)

Unlike the two quotes, this can be used to indicate the result of another command run, such as:

Contents = `ls`

The contents can then indicate the list of file directory lists.

Logic and comparison

-----

A command called Test can be used to determine if a condition is TRUE, as if the if-the state is used to determine if a loop is endless:

Test Expression

or

[expression]

Mathematical comparison

-----

INT1-EQ INT2

Return true if INT1 is equal to INT2

INT1 -GE INT2

Return true if INT1 is greater than or equal to INT2

INT1 -GT INT2

Return true if INT1 is greater than INT2

INT1 -LE INT2

Return true if INT1 is less than or equal to INT2

INT1 -LT INT2

Return true if INT1 is less than INT2

INT1-NE INT2

Returns true if INT1 is not equal to INT2

Character comparison

-----

Str1 = STR2

Returns True if str1 and str2 are equal

STR1! = STR2

Return true if str1 and str2 are not equal

Str

Return true if the Str is not empty

-N STR

Returns true if the Str is greater than zero-z STR

Returns True, if the length of the STR is equal to zero (Note: The string length is different from zero and the string is empty)

File comparison

-----

-D filename

Return true if filename is a directory

-f filename

Return true if filename is a standard file

-r filename

Returns true if filename can be read

-s filename

Return true if filename is a file that is not zero

-w filename

Returns true if filename can be written

-x filename

Returns true if filename is executable

Expression comparison

--------

Expression

Return true if Expression is not true

EXPR1 -A EXPR2

Return true if expr1 and expr2 are true (&&, and)

EXPR1 -O EXPR2

Return true if expr1 or expr2 is true (||, or)

Logical sentence

----

hint:

The end word of each statement is the reverse spelling of the start word of this statement. For example, the end word of the IF is Fi. The end word of Case is ESAC.

IF ... THEN

IF [expression]

THEN

Commands

Fi

2. IF..Then ... Else

IF [expression]

THEN

Commands

Else

Commands

Fi

3. IF..THEN ... ELSE IF ... ELSE

IF [expression]

THEN

Commands

Elif [Expression2]

THEN

Commands

Else

Commands

Fi

4. Case SELECT

Case string1 in

STR1)

Commands ;;

STR2)

Commands ;;

*)

Commands ;;

ESAC

String1 is compared to STR1 and STR2. If one is in line with String1, then ;; The corresponding command will be executed. If both are all inconsistent, the commands corresponding to the asterisk will be executed. This is the default condition execution command because the asterisk is in line with all strings.

loop statement

----

For var1 in list

DO

Commands

DONE

In this loop, each item in the list will be executed once. The items in this list can be some commands separated by spaces (such as LS or CAT), or some specific values. When each cycle, the variable var1 will be given the current item and has been executed until the last one.

While [expression]

DO

Commands

DONE

Until [Expression]

DO

Commands

DONE

These two and other languages ​​are similar in words, and there is not much introduction here.

function

-

1. Create a function:

FName () {

Commands

}

If you want to call this function, you can use the FNAME directly.

2. Create a function with parameters

FName2 (arg1, arg2 ... argn) {

Commands

}

You can call it with fname2 arg1 arg2 ... argn.

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

New Post(0)