This is I learn to make notes or to make notes on their own easy to remember the format is very important examples are reference I also made the color on the impression notes to distinguish it on the top of it.
read for interactive scenarios is to put the user input information to a variable-p is the information that will be displayed on the screen
Format: READ-P "hint information" variable name
#!/bin/bash
Echo-n "Enter Your Name:"//Parameter-n is not wrapped, echo defaults to line wrapping
Read name//assign the input on the keyboard to name
echo "Hello $name, Welcome to my program"//Display information
Exit 0//Quit Shell program.
The script above can be simply written in the following script:
#!/bin/bash
Read-p "Enter Your Name:" Name first give a variable
echo "Hello $name, Welcome to my program"
Exit 0
8052604
Variables : Flexible completion parameters for work receiving users
#var1 =centos #把centos赋值给var1
Variable requirements
Cannot have spaces on either side of the equals sign
You can't start with numbers, don't use keywords and special characters.
Can be abx_123 with letters/numbers/underscores
Cancel a variable
unset Cancel variable Settings unset SCHOOL
or exit the shell environment that defines the variable
Variable type
Environment variables
PWD PATH USER LOGNAME SHELL HOME PS1 PS2
Pre-defined variables
- echo $//script name
- echo $//First parameter
- echo +//second parameter
- echo $*//All parameters
- echo $#//with several parameters
- echo $$//process number of the current process
- echo $? Return status code 0 for the previous program indicates normal 1 or another value represents an exception
Ps:
9.$ () is equivalent to "execute first
10.$ (()) $[] integer operation
Positional variables
$ $
Sh-x Debug script See which commands are executed
Global environment variable/etc/profile user file is ~/.bash_profile
"" will calculate a result for the variable
"What's inside is what?"
"and $ () are required to execute the inside put command
Operation
Category +-*/% (take surplus)
Integer arithmetic 1.expr 2.$[], $ (())
format: Expr integer 1 operator integer 2 needs to be escaped *
$[integer 1 operator integer 2]
#expr 1 + 2, expr $x + $y
#echo $ ((x-21)), $[x*21] $ (()) $[] operator can save space multiplication operation province escaped reference variable province $ can be combined with echo
22,903
Self-increment/decrement of variables
Format:
#i =43
#echo $[i++]
or #let I++;echo $i
i++ i=i+1
i--i=i-1
i+=2 i=i+2
i-=2 i=i-2
I=2 i=i2
i%=2 i=i%2
i/=2 I=I/2
Decimal Operations BC
Format:
#echo "Operation" |BC
#bc
12.3456.78
Scale=4 defines the number of decimal digits
#echo "$A56.789" |BC
#echo "scale=4; $A *56.789;5/4" |BC multiple expressions semicolon interval
Comparison
Small value comparison > < >= <= = = = =
Format: echo "Numeric comparator value" #表达式成立返回1 not valid return 0
#A =12.34;b=34.56
#echo "$A > $B" |BC #A是否小于B
Integer value Comparison
Format: [numeric operator value]
-eq equals equal.
-ne not equal to not equal
-ge is greater than or equal to greater equal
-le is less than or equal to lesser equal
-GT greater than greater than
-lt less than Lesser then
Check if the number of logged-in users is less than 5
#who |wc-l
#[$ (WHO|WC-L)-le 5] && echo "OK"
$ () Execute first
File Status Test
Format: [operator file or directory] #表达式不成立返回1成立返回0
-E to determine if an object exists exist
-D to determine whether the object is directory directories
-F to determine if the object is a generic file
-R to determine if the object has Read permissions
-W Write permission
-X Execute Permissions
#[-d/etc/hosts]
#echo $?
1
string comparison
Format: [string 1 operator String 2]
= = Two strings same
! = Two strings different
#[$USER = = "root"] && echo "Super User"
Logical separation
Format: Command 1 operator Command 2
[Condition 1] operator [condition 2]
&& Then
|| Or
Cases:
The current user is root and is in the root directory
#[$USER = = "Root"]&&[$PWD = = "/root"]
#[$-eq 0] && Echo YES
Create this directory when the/opt/testdir directory does not exist
#[-D "/opt/testdir"]| | Mkdir/opt/testdir
Shell easy to confuse rules