How to combine Python and bash in a friendly way

osc_ yq7pxbpa 2020-11-14 15:28:48
combine python bash friendly way

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 .

 How to be friendly Python and Bash Bind together

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

#!/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
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

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 | 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 | 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 :

本文为[osc_ yq7pxbpa]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database