Shell script

Shell script (shell script), It's a kind of shell Scripted program .

What the industry says shell Usually it means shell script .

Shell variable

Using variables

Use a defined variable , Just add the dollar sign before the variable name

Curly braces outside variable names are optional , Add it or not , Curly braces are used to help the interpreter identify the boundaries of variables

read-only variable

use readonly Command to define a variable as a read-only variable , The value of a read-only variable cannot be changed

Delete variable

use unset Command to delete variables .

Variable type

function shell Time , There will be three variables at the same time :

(1) local variable   Local variables are defined in scripts or commands , Current only shell Valid in instance , other shell Started program cannot access local variables .

(2) environment variable   All procedures , include shell Program started , Can access environment variables , Some programs need environment variables to ensure their normal operation . When necessary shell Scripts can also define environment variables .

(3) shell variable  shell Variables are created by shell
Special variables for program settings .shell Some of the variables are environment variables , Some of them are local variables , These variables guarantee shell Normal operation of

Shell character string

String is shell The most commonly used and useful data types in programming ( Except numbers and strings , There's nothing else to use ), Strings can be quoted in single quotes , You can also use double quotes , You can also use no quotes .

Shell array

bash Supports one dimensional array ( Multidimensional arrays are not supported ), And does not limit the size of the array .

Similar to C language , The subscript of an array element is determined by 0 Start number . To get the elements in an array, use Subscripts , Subscripts can be integers or arithmetic expressions , Its value should be greater than or equal to 0.

Shell Pass parameters

We can execute Shell When scripting , Pass parameters to script , The format of getting parameters in the script is :$n.n  Represents a number ,1 Is the first parameter to execute the script ,2
Is the second parameter to execute the script , and so on ……

Shell Basic operators

Arithmetic operator

The following table lists the common arithmetic operators , Assumed variable a by 10, variable b by 20:

+

addition

`expr $a + $b` The result is  30.

-

subtraction

`expr $a - $b` The result is -10.

*

multiplication

`expr $a \* $b` The result is  200.

/

division

`expr $b / $a` The result is  2.

%

Take surplus

`expr $b % $a` The result is  0.

=

assignment

a=$b The variable will be b Value assigned to a.

==

equal . Used to compare two numbers , Return if same true.

[ $a == $b ] return  false.

!=

Unequal . Used to compare two numbers , Return if different true.

[ $a != $b ] return true.

Relational operators

Relational operators only support numbers , String not supported , Unless the value of the string is a number .

The following table lists the common relational operators , Assumed variable a by 10, variable b by 20:

-eq

Check whether two numbers are equal , Equal return true.

[ $a -eq $b ] return  false.

-ne

Check whether two numbers are equal , Unequal return true.

[ $a -ne $b ] return true.

-gt

Check whether the number on the left is greater than that on the right , If it is , Then return true.

[ $a -gt $b ] return false.

-lt

Check whether the number on the left is less than that on the right , If it is , Then return true.

[ $a -lt $b ] return true.

-ge

Check whether the number on the left is greater than or equal to that on the right , If it is , Then return true.

[ $a -ge $b ] return false.

-le

Check whether the number on the left is less than or equal to that on the right , If it is , Then return true.

[ $a -le $b ] return true.

Boolean operator

The following table lists common Boolean operators , Assumed variable a by 10, variable b by 20:

!

Non operation , Expression is true Then return false, Otherwise return true.

[ ! false ] return true.

-o

Or operation , There is an expression true Then return true.

[ $a -lt 20 -o $b -gt 100 ] return  true.

-a

And operation , Both expressions are true To return true.

[ $a -lt 20 -a $b -gt 100 ] return  false.

Logical operators

Here's how Shell Logical operators for , Assumed variable a by 10, variable b by 20:

&&

Logical AND

[[ $a -lt 100 && $b -gt 100 ]] return false

||

Logical OR

[[ $a -lt 100 || $b -gt 100 ]] return true

String operator

The following table lists common string operators , Assumed variable a by "abc", variable b by "efg":

=

Check if two strings are equal , Equal return true.

[ $a = $b ] return false.

!=

Check if two strings are equal , Unequal return true.

[ $a != $b ] return  true.

-z

Check whether the string length is 0, by 0 return true.

[ -z $a ] return false.

-n

Check whether the string length is 0, Not for 0 return true.

[ -n $a ] return true.

str

Check if the string is empty , Not null return true.

[ $a ] return  true.

Shell echo command

Shell Of echo Instructions are output for Strings .

Shell printf command

printf Command imitation C Library (library) Inside printf() program .

printf from POSIX As defined in the standard , So use printf Using echo Good transplantation .

printf Parameters separated by reference text or spaces , It can be outside printf Format string used in , You can also specify the width of the string , Left right alignment, etc .

Shell test command

Shell In test Command to check if a condition holds , It can be numerical , Character and file testing .

Numerical test

-eq

Equal is true

-ne

True if not equal

-gt

Greater than is true

-ge

True if greater than or equal to

-lt

Less than is true

-le

True if less than or equal to

String test

=

Equal is true

!=

True if unequal

-z character string

True if string length is zero

-n character string

True if string length is not zero

Shell Process control

if else

if

if Statement syntax format :
if condition then     command1     command2     ...     commandN fi
if else

if else Grammatical format :
if condition then     command1     command2     ...     commandN else    
command fi
if else-if else

if else-if else Grammatical format :
if condition1 then     command1 elif condition2 then     command2 else    
commandN fi
for loop

for The general format of the loop is :
forvarin item1 item2 ... itemN do     command1     command2     ...    
commandN done
while sentence
while condition do   command done

Technology
©2019-2020 Toolsou All rights reserved,
Bitcoin in ten years ,VDS Opportunity or fraud Don't annoy the panda with any cat !「 Kung Fu Panda 」20 It's the year of man 4 blood sort ( one ) bubble sort Random forest R Language implementation java Realize the function of grabbing red packets Python Basic knowledge and notes python To solve the problem of dictionary writing list in CSS architecture design Vue Common features ( one )2021 year 2 Monthly programmer salary statistics , average 15144 element