Shell
The shell is a command interpreter, which is actually a program, and all the commands in/bin/bash,linux are explained by it and have their own syntax
shell Script
End With. sh
Shell Syntax +linux command
Comments:
Single-line Comment: #
Multiline Comment:: <<c c
Shell Execution Mode
Bash/path/to/script-name or/bin/bash/path/to/script-name (highly recommended)
/path/to/script-name or./script-name (Execute script under current path)
SOURCE Script-name or. Script-name (note ".") There are spaces behind the dot number)
The first two executions will open a new process execution script
Source does not open a new process
When using source, you can share a process with the current terminal, sharing variables (emphasis)
variables
1, Name=itcast
2, name= ' Itcast$age ' will not parse the inside of the traversal
3, name= "Itcast$age" after parsing the variable, and then stitching into a new string
Command Variables
1. name= ' ls '
2, name=$ (LS)
The parentheses must be a Linux command that logs the result of the command execution to the variable
Global Variables
Method One:
Variable name = value
Export variable
Method Two: (most commonly used)
Export variable name = value
If the global variable is defined in the terminal, the scope is the current terminal and the child process
If you want to define a global variable that is valid in all environments as a system global variable, you need to define it in a ~/.BASHRC or/etc/profile file:
After modifying the ~/.BASHRC, the new terminal is opened directly, and the defined global variable takes effect, which is only valid for the current user.
When modifying/etc/profile, the operating system needs to be restarted, and the defined global variables will take effect, which is valid for all users
In both ways, if you want the global variable to take effect at the current terminal after modifying the file, you need to execute the command source ~/.BASHRC or Source/etc/profile
View Variables
Standard usage: "${variable name}"
built-in
$ A Gets the currently executing shell script file name
$$ Gets the process number that executes the shell script
$n gets the nth parameter value of the currently executing shell script, n=1..9, which represents the file name of the script when n is 0, and if n is greater than 9 is enclosed in curly braces ${10}
$# get the total number of parameters in the current shell command line
$? Gets the return value that executes the last instruction (0 for success, not 0 for failure)
$?: The previous command executes a file and returns the result of the last command in the file
Default Value
1.
Variable A If there is content, then output A's variable value
? Variable A If there is no content, then output the default content
? Format:
???? ${variable Name:-Default Value}
2.
Output default value regardless of whether variable a has content
? Format:
???? ${variable name + default value}
Test Statement
There must be a space around the equals sign, a space around the brackets
Test A = 1
[a = 1]
Logical Expressions
&& symbols
Command 1&& Command 2
If command 1 executes successfully, execute command 2
If command 1 fails, then command 2 is not executed
|| Symbol
Command 1 | | Command 2
1 if command 1 executes successfully, do not execute command 2
2 If command 1 fails to execute, then execute command 2
file Expression
Determine if an executable file
[-F ABC] && [-X ABC]
Evaluating Expressions
a=$ ((a+1))
Let A=a+1
redirect
Commands > Documents
The result returned by the command is correct and error two, 1 is correct, 2 is error
Bash Chongdingxiang.sh>/dev/null 2>&1 &
Output the correct and incorrect results to a black hole file, and the command executes in the background
Linux Four Swordsman
grep
GREP-NR keyword.
Find
Find. -name "*sh"
sed
Line editing Tools
-I real modification
S: replace
A: Append
I: Insert
D: Delete
Sed-i "S#sed#sed#g" Sed.txt
Each line finds the SED, replacing all of them with SED
Sed-i "2s#sed#sed#2" Sed.txt
The second sed in the second row is replaced by SED
Sed-i "1,4a\hello2" Sed.txt
Append a row after each row from 1 to 4 rows
Sed-i "1i\hello3" Sed.txt
Insert a row before line 1th
Sed-i "2d" sed.txt
Delete Line 2nd
awk
Analysis tools, row-by-line processing
awk ' begin{fs= ': "; ofs="--"} {print $, $NF} ' Awk2.txt
The default is to split the data by a space or TAB key, specifying the delimiter for the output by OFS
Process Control
If statement
if [condition]
Then
Directive 1
elif [Condition 2]
Then
Directive 2
Else
Directive 3
Fi
# !/bin/bash " Please enter gender:" sexif"$sex"" nan"" input is male "fi
Case statement
#!/bin/bash Case" $" inch "Start") echo"Start" ;; "Stop") echo"Stop" ;; "Restart") echo" Restart" ;; *) echo".... " ;; Esac
For loop
Traverse Folder
# !/bin/bash mkdir . /bak for file with $ (LS)do# echo "file: $file" "$file" . /bak/"${file}-bak" Done
seq Command
# !/bin/bash for in $ (seq 5 )does"num: $num" Done
While loop
# !/bin/bash Count=1 While [$count-lt 5 ]do "count: $count " Let Count=count+1done
Until cycle
# !/bin/bashcount=1until [$count-ge 5 ] do "count: $count" Let count=count+1done
Function
# !/bin/bash echo " jiaoben: $1,$2,$3 "" Span style= "COLOR: #000000" >dayin () { if [$ # = 3] then echo " hanshu: $1,$2,$3 "" Span style= "COLOR: #0000ff" >else echo " Requires 3 parameters fi " # dayin E F G Dayin $ $
Scripts are executed using source, and variables and functions defined in the script can be called at the current terminal
Allows the root user to use the Python virtual environment
exportworkon_home=/home/python/.virtualenvs/
source/usr/local/bin/virtualenvwrapper.sh
Shell Entry Basics & Common commands and usage