Steps to create a shell program:
First step: Create a script file. Sh.
Second step: Grant permissions so that it can execute chmod u+x. sh
Step three: Execute./example or SH example
Shell structure:
1. #! Specifies the shell that executes the script
2. #注释行
3. Command and control structure
Variables: Temporary variables and permanent variables (environment variables)
Set variables: use uppercase letters to name variables. Variable names can only begin with the characters in the alphabet and cannot be used as numbers.
variable Assignment: the assignment number "=" should have no spaces on either side
Assign values when defined, such as Num=1
Assigns the execution result of a command to a variable, such as: time= ' Date '
Assign a variable to another variable, such as: A= $B
Note:
Variable with multiple words: $NAME =mike Ron will error, use $NAME = "Mike Ron" or $name= ' Mike Ron '
The difference between single and double quotation marks:
The content between the single quotes is interpreted by bash and is assigned to the variable as is.
The contents of the double quotation marks can only be escaped when the "$,", double quotes,/and newline characters are decorated with five special character modifiers.
Delete variable: #unset NAME
To view variables:
List all variables: # set
Use the echo command to view variable values. For example: Echo $A
Positional variables:$n the nth parameter value of this program, n=1-9
For example:
Ls-l file1 file2 File3
$ A file name for this program ls-l
Special variables:$? Executes the return value of the previous command $* all parameters of this program
$# the number of parameters for this program
$$ the PID of this program
$! PID that executes the previous background command
Shell command: Read command: Reads data from the keyboard, assigns values to variables such as: Read USERNAMEEXPR command: Arithmetic operations on integer variables such as: $ expr 3 \*, var2 = ' expr ' expr 30/10 '/$ Var1 '
Attention:
Operators have spaces around them
When using multiplication sign, you must mask its specific meaning with a backslash. Because the shell may misunderstand the meaning of displaying asterisks.
Shell programming Syntax (i)