Shell is a commonly used script for interpretation in Linux. It contains many types and is widely used in bashshell. Shell files can be seen everywhere in the android source code, for this reason, make a memo (for details, shell can be published separately), and record some common symbols and syntaxes as follows to facilitate searching
1. A simple shell script is as follows:
#! /Bin/bash
Echo "Hello shell"
Note:
1). The shell script must be added at the top #! /Bin/Bash to specify the shell type used
2) The shell script extension. Sh can be dispensable and used to be added to differentiate file types.
3) run the following command:
$. A. Sh
Or
$ Sh A. Sh
2. Common symbols in bash scripts
1) special symbols
Symbol |
Instructions for use |
Annotator # |
Description |
Double quotation marks "" |
All characters in the quotation marks are converted to common characters, except the backslash \ inverted quotation mark ('') dollar sign $ |
Single quotes'' |
Change all characters in quotation marks to normal characters |
Quotation marks'' |
The quotation marks are interpreted as shell. |
Backslash \ |
Escape characters to convert special characters into common characters |
Dollar sign $ |
Reference variable value |
2) file test symbols
Symbol |
Instructions for use |
-F |
File exists and common file |
-D |
Folder exists |
-X |
The file exists and is executable. |
-W |
The file exists and can be written. |
-R |
The file exists and is readable. |
-S |
File exists and the byte is greater than 0 |
-N |
The operand length is not 0. |
-Z |
The operand length is 0. |
- |
Logic and |
-O |
Logic or |
! |
Non-logical |
3) Comparison Operators
Symbol |
Instructions for use |
-EQ |
Is an integer, equal (can only be used to compare integers, cannot compare strings are equal) |
-NEQ |
Is an integer, not equal |
= |
Is a string, equal |
! = |
Is a string, not equal |
-Lt |
Is an integer, less |
-GT |
Is an integer greater |
-Le |
Is an integer, less than or equal |
-Ge |
Is an integer greater than or equal |
4) built-in symbols
Symbol |
Instructions for use |
$ @ |
All parameters |
$ # |
Number of parameters |
$? |
Result returned by the previous command |
$ |
Process ID of the current command |
$ N |
Indicates the nth input parameter. |
$0 |
Shell program name |
3. Condition judgment
#! /Bin/bash
If ["$1" = "normal"]
Then
Echo "this is normal"
Elif ["$" 2 = "active"]
Then
Echo "this is active"
Fi
Note:
1) A space must be added between "[" and "$1". "[" is an operator number. Therefore, it must be separated by spaces.
2) then must be added after if
3) end with Fi
4. While [] Do... Done
Note:
1) Pay attention to the meaning "["
2) end with done
5. Case esac selection statement
#! /Bin/bash
Case $1 in
1)
Echo "selected 1"
;;
2)
Echo "selected 2"
;;
Esac
Note:
1) In is also a Linux Command and cannot be lost.
2) End of each case Branch ;;
3) end esac with case
4) The break can jump out without executing the branch behind it.
6. For Loop
#! /Bin/bash
For X in 'seq 1 9'
Do
Echo 'expr $ x \ * 10'
Done
Note:
1) For is used with in to obtain elements from a collection.
2) 'seq 1 9' indicates the sequential sequence from 1 to 9.
3) expr: evaluate the value of the following expression
7. Functions
Note:
The function must be defined before being referenced. There can be parameters without parameters. It can be considered a set of commands.
========================================================== ======================================
PS: Shell brief introduction remarks above. If any error occurs, please reply and correct it.