#! /bin/sh
Look for Shell interpreter/bin/sh is a path
#! /usr/bin/python
Just looking for a Python interpreter.
How to run a Linux program:
Allow files to run directly with executable permissions
Invoke interpreter to execute
Use Source to perform
The shell has internal commands and external command built-in commands that are the command of the shell program itself.
Do not include the creation and extinction of processes when executing internal commands
But when executing an external command, there is a process that is created and extinct, which simultaneously
The procedure for external command execution is as follows:,
Create a child process
Find a path
Child processes perform parent process hibernation
The child process finishes executing and the parent process reads the next command from the terminal
The execution of the source command does not create a process more without the demise of a process with no child processes
Only executed in the parent process.
Example: echo.sh
#! /bin/sh
Cd/tmp
echo "Hello World"
The execution method chooses the way to give permission./echo.sh at this point the parent process accepts the command to discover that not the built-in command creates a child process (exactly the same as the parent process) to execute this external command at this time the child process sets its own environment variable the CD command changes only the subdirectories directory Does not change the directory of the parent process, the execution of the child process is dead, and the parent process waits for the next command to execute, so the Execute CD command in this way is invalidated.
So the choice of execution is in the form of source, and the source does not create child processes just in the parent process.
Shell variables:
Global variables environment variable Custom variable
Local variables must use local declaration or globally visible
Export to set the current environment variable
Computers do not directly understand high-level languages need to translate high-level languages into machine-language computers in order to see the understanding that translation is the way there are two kinds of: Compile one is: explain
A compiled language is a process that requires a specific compilation before the program executes, compiles only once, and does not need to be compiled at runtime to perform more efficiently.
The interpreted language is less efficient when the program executes a translation once.
Passing of script parameters
The passing of parameters can pass external values into the script's intrinsic functions, increasing the flexibility of the script.
TestFunc ()
echo "$# parameters";
echo "[email protected]";
TestFunc a b C
3 parameters
A b C
TestFunc a "b C"
2 parameters
A b C
The variables in the shell are non-differentiated types are all string types in shell variables with 3: User variable position variable environment variable
The meaning of shell variable $#,[email protected],$0,$1,$2 in Linux is explained:
Variable Description:
$$
The PID of the shell itself (ProcessID)
$!
PID of the Shell's last running background process
$?
The end code (return value) of the last run of the command returns a numeric value when the return value successfully returns 0 values exit status 0 indicates normal exit non 0 indicates an exception occurred
$-
Flag at a glance using the SET command
$*
All parameter lists. such as "$*" in the Case of "" ", in the form of" $ $ ... $n "output all parameters.
[Email protected]
All parameter lists. such as "[email protected]" with "" "in the case, with" $ "" $ "... All parameters are output in the form "$n".
$#
Number of arguments added to the shell
$
The name of the shell itself
$1~ $n
Each parameter value added to the shell. $ $ is the 1th parameter, and the $ = is the 2nd parameter ....
Startup file:
/bin/login read/etc/passwd file after successful login, start an interactive shell
/etc/environment Environment variables
If you need to make a global setting for the shell and automatically load it at each startup, you can write the command to the startup file.
Type is used to show what type of command is an external command built-in command alias ...
Shell's functions
#! /bin/sh
# Add numbers
function Add () {
Let "sum=$1+$2"
Return $sum
}
Perform:
SOURCE add.sh The function can be called directly after it is read from the file.
Add 3 7
echo $?$? Save is the return value of the last command execution
7
Shell's conditional control and flow
If condition
Then
Statements
Elif contition
Then
Statements
Else
Statements
Fi
Case statements inside the shell
Case $ in
-f) statements;;
-d) statements;;
Esac
For Loop statement
for name [in list]
Do
....
Done
For file in ' find. -iname "*.mp3" Anti-single quotes function as a string for the result returned by the command
Do
mpg123 $file
Done
Infinite loops
Path= $PATH
While True
Do
If [-Z $path]
Then
Break
Fi
Ls-ld ${path%%:*}List the first directory in path
path=${path#*:}Intercept the first directory and colon in path
Done
Tips:
The position parameter can be shifted left with the shift command. For example, shift 3 means that the original $4 now become $ $, the original $ $ now becomes $ $, and so on, the original $, $, $ discard, $ not move.
The shift command with no parameters is equivalent to shift 1.
A very useful Unix command: Shift. We know that the number of positional variables or command-line arguments must be deterministic,
or when the shell program does not know its number, you can assign all parameters together to the variable $*.
If the user requires the Shell to not know the number of position variables, but also one by one to the parameter one by one processing,
That is, after the $ $, in the back of $ $, and so on. The value of the variable before the shift command executes is not available after the shift command executes.
Regular expressions inside the Linux shell:
Defined:
In short, a regular expression is the code that records a text rule.
Regular Expressions: meta-characters
\b Represents the beginning or the end of a word.
\d stands for numbers like 0\d{2}-\d{8} means 010-12345678.
grep find text
Metacharacters in regular Expressions:
^: line or String start
$: line or end of string
. : Matches a character that is not a line break
*: Matches 0 or more previous characters. * represents any character
[...] : the square bracket expression [0-9] matches a single number ^ at the beginning of the parenthesis expression means the opposite meaning [^0-9] is not a number between 0-9
\: Turn on or off subsequent characters
Regular Expressions: basic regular expressions and extended regular expressions
\(\):
N
x\{m,n\}: Interval expression x appears at least m times up to N times
+: Matches one or more instances of the preceding regular expression
?: matches one or 0 instances of the preceding regular expression
(): Regular expression enclosed in parentheses
|: Match | Regular expression in front or back
grep supported meta-characters
\<: The beginning of a word
/>: The end of a word
\w: Match text or numbers [: Alnum:]
\w: Match non-literal or numeric [[: Alnum:]_]
\b: The word's lock character
Character:
[: Alnum:]: Literal numeric character Set a-za-z0-9
[: Alpha:]: literal character set
[: Blank:]: space or positional characters
[:d igit:]: Numeric characters
[: Graph:]: non-null character
[: Lower:]: lowercase characters
[: Cntrl:]: Control character
[:p rint:]: non-null characters
[:p UNCT:]: Punctuation
[: Space:]: white space character
[: Upper:]: Uppercase characters
[: Xdigit:]: Hex digit 0-9 a-f a-f
A reverse reference inside a regular expression:
\ (ab\) \ (cd\) [efg]*\1\2 used to match: ABCDABCD ABCDEABCD abcdfabcd ABCDGABCD \1:ab \2:CD
\ (go\). *\1 used to match a row there were 2 go
Alternating | You|me is the lowest priority used to match you or me alternates.
Group () (GO) + matches a go or multiple go
Roman numerals:
I=1
V=5
x=10
L=50
c=100 cd=400 dc=600
d=500
m=1000 mcm=1900
Shell Programming Notes