about Linux For users , Command line operations are very familiar to us . Unlike other popular operating systems , stay Linux In the community , Using the command line is compared to performing similar tasks using a graphical user interface , The command line can usually provide more elegant , A more effective solution .
With Linux The community's dependence on the command line is growing ,UNIX shell( Such as bash and zsh) Has developed into an extremely powerful tool , Can complement UNIX shell Experience . Use bash And other things like that shell, There are many powerful features available , For example, pipes , File name wildcards and the ability to read commands from a file called a script .
Let's take a look at a real-world example to demonstrate the functionality of the command line . Every time a user logs in to the service , The user name will be recorded in a text file . For this example , Let's find out how many unique users use the service .
The sequence of commands in the following example links together smaller building blocks , Shows the functionality of more complex Utilities :
$ cat names.log | sort | uniq | wc -l
Pipe symbol (|) Used to pass the standard output of one command to the standard input of the next . In the example here ,cat names.txt The output of is passed to sort In command .sort The output of the command is to rearrange each line of the file in alphabetical order . Then pass it on to uniq command , This command will remove all duplicate names . Last ,uniq The output of is passed to wc command .wc It's a count command , And set up -l sign , It returns the number of rows . This allows you to link many commands together .
however , Sometimes the required content can become very complex , And linking commands together can be clumsy . under these circumstances ,shell Script is the answer .Shell The script was created by Shell List of commands that are read and executed sequentially .Shell Scripts also support some programming language basics , For instance variable , Flow control and data structure .Shell Scripts are useful for batch jobs that will run repeatedly . Unfortunately ,shell Scripts have some drawbacks :
Shell Scripts can easily become too complex , And it's unreadable for developers who want to improve or maintain them .
these shell Scripts and interpreters are usually not intuitive and clumsy . The more clumsy the grammar is , For developers who have to use these scripts , The less readable .
This code is usually not available in other scripts . Code reuse between scripts is often difficult , And scripts are often very specific to a problem .
For advanced features ( for example HTML Resolve or HTTP request ) The library is not as easy to get as modern programming and scripting languages .
These questions will make shell Scripts are hard to handle , And often leads to a large number of developers waste time . In its place ,Python Programming languages can be used as very powerful alternatives . Use Python Instead of Shell Scripts have a lot of benefits :
By default , All the major Linux All distributions have Python. Open the command line and type python, Will bring you into Python Interpreter . This universality makes it a wise choice for most scripting tasks .
Python Having grammar that is very easy to read and understand . Its style emphasizes simplicity and simplicity of code , It also allows developers to fit shell The script is written in a quasi system style .
Python It's an interpretative language , This means there is no compile phase . This makes Python Become the ideal language for scripting , It allows you to quickly try new code in an interpretive way . This allows developers to quickly modify , You don't have to write the entire program to a file .
Python Is a fully functional programming language . Code reuse is simple , because Python Modules can be easily imported and stored in any Python Use in script . Scripts can easily extend or build .
Python Excellent standard libraries and thousands of third-party libraries can be used to handle a variety of advanced Utilities , For example, parsers and request Libraries . for example ,Python The standard library includes date time library , The library allows you to parse dates into any format you specify and easily compare them with other dates .
but Python Not all should be replaced bash command . Write to UNIX Way to run Python Program ( Read in standard input and write standard output ) And for the existing shell command ( Such as cat and sort) To write Python Alternatives are as powerful as .
Let's build on the problems that have been solved earlier in this article . In addition to the work that has been done , Let's find out how many times a user has logged into the system .uniq Command to delete only duplicate items , But it doesn't provide information about how many repetitions there are . Instead of uniq,Python The script can be used as another command in the chain . This is the one that does this Python Program ( In my example , I call this document namescount.py):
#!/usr/bin/env python
import sys
if __name__ == "__main__":
# Initialize a names dictionary as empty to start with.
# Each key in this dictionary will be a name and the value
# will be the number of times that name appears.
names = {}
# sys.stdin is a file object. All the same functions that
# can be applied to a file object can be applied to sys.stdin.
for name in sys.stdin.readlines():
# Each line will have a newline on the end
# that should be removed.
name = name.strip()
if name in names:
names[name] += 1
else:
names[name] = 1
# Iterating over the dictionary,
# print name followed by a space followed by the
# number of times it appeared.
for name, count in names.iteritems():
sys.stdout.write("%d\t%s\n" % (count, name))
Let's take a look at this Python How does the chain of commands fit . First , It's from through sys.stdin
Read input from standard input exposed by object . Any output will be written to sys.stdout
object , This is Python How to realize standard output in .Python Dictionaries ( In other languages, it's often called a hash map ) Used to get the mapping from the user name to the repeat count . To get the number of all users , Do the following :
$ cat names.log | python namescount.py
Shows the number of times the user appears and the user name count . The next thing to do is to show the most frequently used users of the system in order . This can be done in Python Level completion , But let's use the core UNIX Utility has provided utilities to implement it . before , I use sort
Command to sort letters . If the command provides -rn
sign , It will sort the rows numerically in descending order . With Python The script is output as standard , Just pipe the command to sort
And retrieve the required output :
$ cat names.log | python namescount.py | sort -rn
This is going to be Python A powerful example to use as part of a command chain . Use in this case Python The advantages are as follows :
-
And cat and sort The ability to link tools such as . Simple Utilities ( Read files line by line and sort them numerically ) By tried and tested UNIX Command processing . These commands are also read line by line , This means that these features can be scaled to large files , And very fast .
-
When you need to do some heavy work in the chain , You can write a very clear , concise Python Script , The script will do what it needs , Then transfer responsibility to the next link in the chain .
- It's a reusable module , Although this example is specific to names , But if you include any input to this input that has duplicate lines , It will print out each line and the number of repetitions . By way of Python Code modularization , It can be applied to a variety of scenarios .
In order to show the combination of modularity and pipeline Python The power of scripts , Let's enlarge the problem further . Let's find the top five users of the service .head
It's a command , It allows you to specify a number of lines to display a given standard input . Add it to the command chain to get the following :
$ cat names.log | python namescount.py | sort -rn | head -n 5
This shows only the first five users , And ignore the rest of the users . Again , Make the service available to at least five users , have access to tail
command , The command takes the same parameters . take Python The result of the command printing to standard output allows you to build and extend its functionality .
The above is a brief introduction to , In practice, we can use it flexibly .
From the original :http://suo.im/5V4DNv