Shell Programming Notes

Source: Internet
Author: User
Tags add numbers

#! /bin/sh
Look for Shell interpreter/bin/sh is a path
#! /usr/bin/python
Just looking for a Python interpreter.


Ways to execute a Linux program:
Enables the file to execute directly with executable permissions
Invoke the interpreter to run
Use Source to run
The shell has internal commands and external command built-in commands that are the command of the shell program itself.
The creation and extinction of processes is not included when running internal commands
However, when running external commands, there is a process that is created and extinct. At the same time
The steps for the external command to run are as follows:,
Create a child process
Find a path
Child process runs parent process hibernation
The child process runs complete. The parent process reads the next command from the terminal


The run of the source command does not create a process more without the demise of the process No child processes
Only runs in the parent process.


For example: echo.sh
#! /bin/sh
Cd/tmp
echo "Hello World"
The way to run the method is to assign permissions./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 run the external command at this time the child process sets its own environment variable the CD command changes only the folder of the child process Does not change the folder of the parent process. When the child process is finished running, the parent process waits for the next command to run, so the Run CD command in this mode is invalidated.




So selecting the Run as source form will run the source and will not create the child process just in the parent process.




Shell variables:
Global variables environment variables define their own variables
Local variables must be declared with local or globally visible
Export to set the current environment variable


Computers do not directly understand the need for high-level languages to translate high-level languages into machine language. The definite translation is the way there are two kinds of: The compilation is: explanation
Compiled language is required before the program to run a special compilation process, just do a compile, run without the need to compile the operation of high efficiency
The interpreted language is less efficient at running a translation once the program runs.




Passing of script parameters
The pass of the parameter can pass the external value to the inside function of the script, improve 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 last background process executed by the shell
$?
The end code (return value) of the last executed 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
$*
List of all references. such as "$*" in the Case of "" ", in the form of" $ $ ... $n "output all the parameters.


[Email protected]
List of all references.

such as "[email protected]" with "" "in the case, with" $ "" $ "... Output all the parameters in the form "$n".
$#
Number of parameters added to the shell
$
The file name of the shell itself
$1~ $n
The number of parameters added to the shell. $ is the 1th, and the number is the 2nd ....


Startup file:
/bin/login read/etc/passwd file after successful login, start an interactive shell
/etc/environment Environment variables
Assume that you need to make a global setting for the shell and that you can load the command into the startup file each time you launch it.


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
}
Run:
SOURCE add.sh can be called directly after the function is read from the file
Add 3 7
echo $?

$? Save is the return value of the last command run
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" The inverse of a single argument is the result of returning the command as a string
Do
mpg123 $file
Done


Infinite loops
Path= $PATH
While True
Do
If [-Z $path]
Then
Break
Fi
Ls-ld ${path%%:*}List the first folder in path
path=${path#*:}Intercept the first folder and colon in path
Done


Tips:
Position parameters can be shifted left with the shift command. For example, shift 3 means that the original $4 now becomes $ $. The original $ $ now turned into a $, and so on, the original $, $, $ discard, $ not move.


The shift command with no parameters is equivalent to shift 1.


Very useful Unix command: Shift.

We know. The number of positional or command-line arguments must be deterministic.
Or, when the shell program does not know its number, it can assign all of the arguments to the variable $*.
If the user requires the Shell to not know the number of positional variables, but also can be the number of arguments one by one processing,
That is, after the $ $, in the back of $ $, and so on. The shift command is not available after the shift command has been run before the variable value is running.




The Linux shell is a regular form:
Defined:
In short, the normal form is the code that records the text rules.
Normal table: metacharacters
\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 the normal form:
^: 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 random characters
[...] : 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 the maybe character
Regular Expressions: Basic regularization and extension of the normal form
\(\):
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 form
?

: Matches one or 0 instances of the preceding regular form
(): a regular form enclosed in parentheses
|: Match | The front or back of the normal form


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 the normal form:
\ (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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.