Python and Shell are often used in the scripting language, usually Python is mainly used to write some small tasks, the shell in the use of LIUNX system deployment tasks when used more, because the two have some similarities, time is easy to mix, So here the composition of some basic requirements of the grammar summary, comparison, induction, thought after use.
Variable
Python definition and use only requires the use of variable names, such as
var=“hello”print var# hello
The shell definition variable requires only the variable name, and the value of the variable requires a $ sign, for example
var="hello"echo $var# hello
Definition of an array
There is no concept of arrays in Python, but list,tuple,dict can be used instead of functions, declaring a list and using one of the following elements
numbers=[1,2,3,4,5]print numbers[0]# 1
There is only one-dimensional array in the shell, which is declared and used as follows
numbers=(1 2 3 4 5)print ${numbers[0]}# 1#返回所有的值用print ${numbers[*]}# 1 2 3 4 5
Comments
Both Python and shell can add a single line of comments with the # sign, and Python can add multiple lines of comments using ' notes '
Definition of logical judgment condition
In Python, except that all null values are considered false and all values are interpreted as true, the null values here include: none,0, "", (), {},[], and false, below is an example
Other comparisons are supported by comparison operators, where the more specific operators are
* x is y x and Y are the same object
* x is not y
* x in Y x is in the Y set
* x not in Y
Logical budgeting method with And,or,not.
The shell concept is very different, it uses the test command for conditional testing, it is usually written in [condition] form, note that there are spaces on both sides of the square brackets, it supports three types of tests:
* Numerical comparison, need to use-EQ,-GT to replace ==,>
* String comparison, can be used =, need to use a short command-n, such as-n str to determine whether STR length is nonzero
* File comparison, you need to use a short command, for example,-e file to determine whether the files exist
Logical operator &&,| | To connect the different [condition]
In addition, advanced mathematical expressions recommend the use of (expression) logic to determine, you can use the self-increment + +, the displacement operator <<, the bitwise operator &, logic and &&, etc.
Advanced string determination using [[expression]] logic to determine a string that starts with r like a regular expression such as [[$TESTSTR = = r*]]
The shell's numerical operations are recommended using the $[expression] method, and the calculation of floating-point numbers uses echo "Expression" | BC Way to get calculated values
If Else
If else in Python is used as follows, the conditional statement is preceded by a colon, and the statement block is divided into indents
if condition: statementelif condition: statementelse: statement
The shell is different, if else is used as follows, it uses then and ElseIf or fi to define the statement block, in theory do not need to indent
if [ condtion ]then statementelif [condtion]then statementelse statementfi
For loop
Python's for loop uses the following, and still divides the statement block with indentation
for item in sequence: statement
In the shell, the for is used in a similar way
for item in listdostatementdone
Where the shell takes item delimiter global variable is IFS (internal field separator), its default value is a space, tab, newline character, so if you need to line processing and ignore the space, you need to change the value of IFS
In addition, the shell supports the C-language for loop, which is done with double parentheses, in the following form
for (( i=1; i<=10;i++))do statementdone
While loop
Python's while is spelled as follows
while condition: statement
The shell's while is spelled as follows
while [ conditon ]do statementdone
Exception handling
How Python handles exceptions
try: statementexcept: statementfinally: finalStatement
There is no corresponding exception handling mechanism in the shell, only each named error output descriptor 2, you can redirect the error output to standard output via 2>&1
File operations
Python uses the Open function, which has read-write mode, reads R, writes W only, adds a
myfile=open("filename","r")myfile.readlines()myfile.close()with open("finename","w") as myfile: myfile.write("line\n")
The shell reads the contents of the file using commands to read, then uses the pipeline or redirects for input processing, and the commonly used commands are
1. Cat filename reads the entire contents of the file
2. Tail-n 2 filename reads the tail two lines
3. head-n 2 filename Read the first two lines
A classic question, read each line of a file to do the processing, using Python is very simple
with open("filename","r") as myfile: for line in myfile.readlines(): print line
Using the shell
cat filename|while read linedoecho $linedone#或者IFS=‘\n‘for line in `cat filename`doecho $linedone
Function
The python functions are in the form
def functionname(params): ‘函数文档‘ statement return value
Where the function document can be obtained by Functionname.doc
function form in shell
function functionname { commands return value}
Using a variable to get the return value, the argument is passed through the function functionname param1 param2
, the first second argument is fetched by $1,$2, $ #为参数数量, and $* is all parameters.
Python vs. Shell