python 安装pracpy_用 Python 替代 Bash 脚本

For Linux users, the command line is a celebrated part of our entire experience. Unlike other popular operating systems, where the command line is a scary proposition for all but the most experienced veterans, in the Linux community, command-line use is encouraged. Often the command line can provide a more elegant and efficient solution when compared to doing a similar task with a graphical user interface.

As the Linux community has grown up with a dependence on the command line, UNIX shells, such as bash and zsh, have grown into extremely formidable tools that complement the UNIX shell experience. With bash and other similar shells, a number of powerful features are available, such as piping, filename wild-carding and the ability to read commands from a file called a script.

译者信息

译者信息

enixyu

翻译于 11个月前

2人 顶 此译文

对于Linux用户来说,命令行的名声相当的高。不像其他操作系统,命令行是一个可怕的命题,但是对于Linux社区中那些经验丰富的大牛,命令行却是最值得推荐鼓励使用的。通常,命令行对比图形用户界面,更能提供更优雅和更高效的解决方案。

命令行伴随着Linux社区的成长,UNIX shells,例如 bash和zsh,已经成长为一个强大的工具,也是UNIX shell的重要组成部分。使用bash和其他类似的shells,可以得到一些很有用的功能,例如,管道,文件名通配符和从文件中读取命令,也就是脚本。

Let's look at a real-world example to demonstrate the power of the command line. Every time users log in to a service, their user names are logged to a text file. For this example, let's find out how many unique users use the service.

The series of commands in the following example show the power of more complex utilities by chaining together smaller building blocks:

$ cat names.log | sort | uniq | wc -l

The pipe symbol (|) is used to pass the standard output of one command into the standard input of the next command. In the example here, the output ofcat names.txtis passed into thesortcommand. The output of thesortcommand is each line of the file rearranged in alphabetical order. This subsequently is piped into theuniqcommand, which removes any duplicate names. Finally, the output ofuniqis passed to thewccommand.wcis a counting command, and with the-lflag set, it returns the number of lines. This allows you to chain a number of commands together.

译者信息

译者信息

enixyu

翻译于 11个月前

1人 顶 此译文

让我们在实际操作中来介绍命令行的强大功能吧。每当用户登陆某服务后,他们的用户名都被记录到一个文本文件。例如,我们来看看有多少独立用户曾经使用过该服务。

以下一系列的命令展现了由一个个小的命令串接起来后所实现的强大功能:

1$cat names.log |sort |uniq |wc -l

管道符号(|)把一个命令的标准输出传送给另外一个命令的标准输入。在这个例子中,把cat names.log的输出传送给sort命令的输入。sort命令是把每一行按字母顺序重新排序。接下来,管道把输出传送至uniq命令,它可以删除重复名字。最后,uniq的输出又传送给wc命令。wc是一个字符计数命令,使用-l参数,可以返回行的数量。管道可以让你把一系列的命令串接在一起。

However, sometimes what is needed can become quite complex, and chaining commands together can become unwieldy. In that case, shell scripts are the answer. A shell script is a list of commands that are read by the shell and executed in order. Shell scripts also support some programming language fundamentals, such as variables, flow control and data structures. Shell scripts can be very useful for batch jobs that will be run often and repeatedly. Unfortunately, shell scripts come with some disadvantages:

Shell scripts easily can become overly complicated and unreadable to a developer wanting to improve or maintain them.

Often the syntax and interpreter for these shell scripts can be awkward and unintuitive. The more awkward the syntax, the less readable it is for the developer who must work with these scripts.

The code is generally unusable in other scripts. Code reuse among scripts tends to be difficult, and scripts tend to be very specific to a certain problem.

Libraries for advanced features, such as HTML parsing or HTTP requests, are not as easily available as they are with modern programming and scripting languages.

译者信息

译者信息

enixyu

翻译于 11个月前

1人 顶 此译文

但是,有时候需求会很复杂,串接命令会变得十分笨重。在这个情况下,shell脚本可以解决这个问题。shell脚本就是一系列的命令,被shell程序所读取,并按顺序执行。Shell脚本同样支持一些编程语言的特性,例如变量,流程控制和数据结构。shell脚步对于经常重复运行的批处理程序非常有用。但是,shell脚本也有一些弱点:

shell脚本很容易变为复杂的代码,导致开发人员难于阅读和修改它们。

通常,它的语法和解释都不是那么灵活,而且不直观。

它代码通常不能被其他脚本使用。脚本中的代码重用率很低,并且脚本通常是解决一些很具体的问题。

它们一般不支持库特性,例如HTML解释器或者处理HTTP请求库,因为库一般都只出现在流行的语言和脚本语言中。

These problems can make shell scripting an awkward undertaking and often can lead to a lot of wasted developer time. Instead, the Python programming language can be used as a very able replacement. There are many benefits to using Python as a replacement for shell scripts:

Python is installed by default on all the major Linux distributions. Opening a command line and typingpythonimmediately will drop you into a Python interpreter. This ubiquity makes it a sensible choice for most scripting tasks.

Python has a very easy to read and understand syntax. Its style emphasizes minimalism and clean code while allowing the developer to write in a bare-bones style that suits shell scripting.

Python is an interpreted language, meaning there is no compile stage. This makes Python an ideal language for scripting. Python also comes with a Read Eval Print Loop, which allows you to try out new code quickly in an interpreted way. This lets the developer tinker with ideas without having to write the full program out into a file.

Python is a fully featured programming language. Code reuse is simple, because Python modules easily can be imported and used in any Python script. Scripts easily can be extended or built upon.

Python has access to an excellent standard library and thousands of third-party libraries for all sorts of advanced utilities, such as parsers and request libraries. For instance, Python's standard library includes datetime libraries that allow you to parse dates into any format that you specify and compare it to other dates easily.

Python can be a simple link in the chain. Python should not replace all the bash commands. It is as powerful to write Python programs that behave in a UNIX fashion (that is, read in standard input and write to standard output) as it is to write Python replacements for existing shell commands, such as cat and sort.

译者信息

译者信息

enixyu

翻译于 11个月前

1人 顶 此译文

这些问题通常会导致脚本变得不灵活,并且浪费开发人员大量的时间。而Python语言作为它的替代品,是相当不错的选择。使用python作为shell脚本的替代,通常有很多优势:

python在主流的linux发行版本中都被默认安装。打开命令行,输入python就可以立刻进入python的世界。这个特性,让它可以成为大多脚本任务的最好选择。

python非常容易阅读,语法容易理解。它的风格注重编写简约和干净的代码,允许开发人员编写适合shell脚本的风格代码。

python是一个解释性语言,这意味着,不需要编译。这让python成为最理想的脚本语言。python同时还是读取,演绎,输出的循环风格,这允许开发人员可以快速的通过解释器尝试新的代码。开发人员无需重新编写整个程序,就可以实现自己的一些想法。

python是一个功能齐全的编程语言。代码重用非常简单,因为python模块可以在脚本中方便的导入和使用。脚本可以轻易的扩展。

python可以访问优秀的标准库,还有大量的实现多种功能的第三方库。例如解释器和请求库。例如,python的标准库包含时间库,允许我们把时间转换为我们想要的各种格式,而且可以和其他日期做比较。

python可以是命令链中的一部分。python不能完全代替bash。python程序可以像UNIX风格那样(从标准输入读取,从标准输出中输出),所以python程序可以实现一些shell命令,例如cat和sort。

Let's build on the problem that was solved earlier in this article. Besides the work already done, let's find out know how many times a certain user has logged in to the system. Theuniqcommand simply removes duplicates but gives no information on how many duplicates there are. Instead ofuniq, a Python script can be used as another command in the chain. Here's a Python program to do this (in my examples, I refer to this file as 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 look at how this Python script fits into the chain of commands. First, it reads in input from standard input exposed through the sys.stdin object. Any output is written to the sys.stdout object, which is how standard output is implemented in Python. A Python dictionary (often called a hash map in other languages) is used to get a mapping from the user name to the duplicate count. To get a count of all the users, execute the following:

$ cat names.log | python namescount.py

译者信息

译者信息

enixyu

翻译于 11个月前

2人 顶 此译文

让我们基于文章前面提到问题,重新使用python构建。除了已完成的工作,还让我们来看看某个用户登陆系统到底有多少次。uniq命令只是简单的删除重复记录,而没有提示到底这些重复记录重复了多少次。我们使用python脚本替代uniq命令,而且脚本可以作为命令链中的一部分。以下是python程序实现这个功能(在这个例子中,脚本叫做namescount.py):

01#!/usr/bin/env python

02import sys

03

04if __name__== "__main__":

05# 初始化一个names的字典,内容为空

06# 字典中为name和出现数量的键值对

07names= {}

08# sys.stdin是一个文件对象。 所有引用于file对象的方法,

09# 都可以应用于sys.stdin.

10for namein sys.stdin.readlines():

11# 每一行都有一个newline字符做结尾

12# 我们需要删除它

13name= name.strip()

14if namein names:

15names[name]+= 1

16else:

17names[name]= 1

18

19# 迭代字典,

20# 输出名字,空格,接着是该名字出现的数量

21for name, countin names.iteritems():

22sys.stdout.write("%d\t%s\n" % (count, name))

让我们来看看python脚本如何在命令链中起作用的。首先,它从标准输入sys.stdin对象读取数据。所有的输出都写到sys.stdout对象里面,这个对象是python里面的标准输出的实现。然后使用python字典(在其他语言中,叫做哈希表)来保存名字和重复次数的映射。要读取所有用户的登陆次数,只需执行下面的命令:

1$cat names.log | python namescount.py

This displays a count of how many times a user appears along with the user's name using a tab as a separator. The next thing to do is display, in order, the users who used the system most often. This can be done at the Python level, but let's implement it using the utilities that are already provided by the core UNIX utilities. Previously, I used thesortcommand to sort alphabetically. If the command is provided with a-rnflag, it sorts the lines numerically, in descending order. As the Python script prints to standard out, you simply can pipe the command intosortand retrieve the output you want:

$ cat names.log | python namescount.py | sort -rn

This is an example of the power of using Python as part of a chain of commands. The advantages of using Python in this scenario are as follows:

The ability to chain with tools like cat and sort. Simple utilities (reading a file line by line and sorting a file numerically) are handled by tried-and-trusted UNIX commands. These commands also are reading line by line, which means these functions can scale to files that are large in size, and they are very quick.

When some heavy-lifting is needed in the chain, a very clear, concise Python script can be written, which does what it needs to do and then offloads the responsibility to the next link in the chain.

It is a reusable module, although this example is specifically about names, if you feed this any input that contains duplicate lines, it will print out each line and the number of duplicates. Making the Python code modular allows you to apply it in a range of scenarios.

译者信息

译者信息

enixyu

翻译于 11个月前

1人 顶 此译文

这里会输出某用户出现的次数还有他的名字,使用tab作为分隔符。接下来的事情就是,以用户登陆次数的降序顺序输出。这可以在python中实现,但是让我们使用UNIX的命令来实现吧。前面已经提到,使用sort命令可以按字母顺序排序。如果sort命令接收一个-rn参数,那么它就会按照数字的降序方式做排序。因为python脚本输出到标准输出,所以我们可以使用管道链接sort命令,获取该输出:

1$cat names.log | python namescount.py |sort -rn

这个例子使用了python作为命令链中的一部分。使用python的优势是:可以跟例如cat和sort这样的命令链接在一起。简单的工具(读取文件,给文件按数字排序),可以使用成熟的UNIX命令。这些命令都是一行一行的读取,这意味着这些命令可以兼容大容量的文件,而且它们的效率很高。

如果命令链条中某部分很难实现,很清晰,我们可以使用python脚本,这可以让我们做我们想做的,然后减轻链条一下个命令的负担。

python是一个可重用的模块,虽然这个例子是指定了names,如果你需要处理重复行的其他输入,你可以输出每一行,还有该行的重复次数。让python脚本模块化,这样你就可以把它应用到其他地方。

To demonstrate the power of combining Python scripts in a modular and piped fashion, let's expand further on the problem space. Let's find the top five users of the service.headis a command that allows you to specify a certain number of lines to display of the standard input it is given. Adding this to the command chain gives the following:

$ cat names.log | python namescount.py | sort -rn | head -n 5

This prints only the top five users and ignores the rest. Similarly, to get the five users who use the service least, you can use thetailcommand, which takes the same arguments. The result of the Python command being printed to standard output allows you to build and extend upon its functionality.

To demonstrate the modularity of this script, let's once again change the problem space. The service also generates a comma-separated value (CSV) log file that contains a list of e-mail addresses and the comments that each e-mail address made about the service. Here's an example entry:

"email@example.com", "This service is great."

译者信息

译者信息

enixyu

翻译于 11个月前

1人 顶 此译文

为了演示python脚本中结合模块和管道风格的强大力量,让我们扩展一下这个问题。让我们来找出使用服务最多的前5位用户。head命令可以让我们指定需要输出的行数。在命令链中加入这个命令:

1$cat names.log | python namescount.py |sort -rn |head -n 5

这个命令只会列出前5位用户。类似的,获取使用该服务最少的5位用户,你可以使用tail命令,这个命令使用同样的参数。python命令的结果输出到标准输出,这样可以允许你扩展和构建它的功能。

为了演示脚本的模块化特性,我们又来扩展一下问题。该服务同样生成一个以逗号分割的csv的日志文件,其中包含,一个email地址列表,还有该地址对我们服务的评价。如下是其中一个例子:

1"email@example.com","This service is great."

The task is to provide a way for the service to send a thank-you message to the top ten users in terms of comment frequency. First, you need a script that can read and print a certain column of CSV data. The standard library of Python provides a CSV reader. The Python script below completes this goal:

#!/usr/bin/env python

# CSV module that comes with the Python standard library

import csv

import sys

if __name__ == "__main__":

# The CSV module exposes a reader object that takes

# a file object to read. In this example, sys.stdin.

csvfile = csv.reader(sys.stdin)

# The script should take one argument that is a column number.

# Command-line arguments are accessed via sys.argv list.

column_number = 0

if len(sys.argv) > 1:

column_number = int(sys.argv[1])

# Each row in the CSV file is a list with each

# comma-separated value for that line.

for row in csvfile:

print row[column_number]

This script can parse the CSV data and return in plain text the column that is supplied as a command-line argument. It usesprintinstead ofsys.stdout.write, asprint, by default, uses standard out as its output file.

Let's add this script to the chain. The new script is chained with the others to print out a list of e-mail addresses and their comment frequencies using the command listed below (the .csv log file is assumed to be called emailcomments.csv and the new Python script, csvcolumn.py):

$ cat emailcomments.csv | python csvcolumn.py |

?python namescount.py | sort -rn | head -n 5

译者信息

译者信息

enixyu

翻译于 11个月前

1人 顶 此译文

这个任务是,提供一个途径,来发送一个感谢信息给使用该服务最多的前10位用户。首先,我们需要一个脚本读取csv和输出其中某一个字段。python提供一个标准的csv读取模块。以下的python脚本实现了这个功能:

01#!/usr/bin/env python

02# CSV module that comes with the Python standard library

03import csv

04import sys

05

06

07if __name__== "__main__":

08# CSV模块使用一个reader对象作为输入

09# 在这个例子中,就是 sys.stdin.

10csvfile= csv.reader(sys.stdin)

11

12# 这个脚本必须接收一个参数,指定列的序号

13# 使用sys.argv获取参数.

14column_number= 0

15if len(sys.argv) >1:

16column_number= int(sys.argv[1])

17

18# CSV文件的每一行都是用逗号作为字段的分隔符

19for rowin csvfile:

20print row[column_number]

这个脚本可以把csv转换并返回参数指定的字段的文本。它使用print代替sys.stout.write,因为print默认使用标准输出最为它的输出文件。

让我们把这个脚步添加到命令链中。新的脚本跟其他命令组合在一起,实现输出评论最多的email地址。(假设.csv 文件名称为emailcomments.csv,新的脚本为csvcolumn.py)

Next, you need a way to send an e-mail. In the Python standard library of functions, you can import smtplib, which is a module that allows you to connect to an SMTP server to send mail. Let's write a simple Python script that uses this library to send a message to each of the top ten e-mail addresses found already:

#!/usr/bin/env python

import smtplib

import sys

GMAIL_SMTP_SERVER = "smtp.gmail.com"

GMAIL_SMTP_PORT = 587

GMAIL_EMAIL = "Your Gmail Email Goes Here"

GMAIL_PASSWORD = "Your Gmail Password Goes Here"

def initialize_smtp_server():

'''

This function initializes and greets the smtp server.

It logs in using the provided credentials and returns

the smtp server object as a result.

'''

smtpserver = smtplib.SMTP(GMAIL_SMTP_SERVER, GMAIL_SMTP_PORT)

smtpserver.ehlo()

smtpserver.starttls()

smtpserver.ehlo()

smtpserver.login(GMAIL_EMAIL, GMAIL_PASSWORD)

return smtpserver

def send_thank_you_mail(email):

to_email = email

from_email = GMAIL_EMAIL

subj = "Thanks for being an active commenter"

# The header consists of the To and From and Subject lines

# separated using a newline character

header = "To:%s\nFrom:%s\nSubject:%s \n" % (to_email,

from_email, subj)

# Hard-coded templates are not best practice.

msg_body = """

Hi %s,

Thank you very much for your repeated comments on our service.

The interaction is much appreciated.

Thank You.""" % email

content = header + "\n" + msg_body

smtpserver = initialize_smtp_server()

smtpserver.sendmail(from_email, to_email, content)

smtpserver.close()

if __name__ == "__main__":

# for every line of input.

for email in sys.stdin.readlines():

send_thank_you_mail(email)

This Python script supports contacting any SMTP server, whether local or remote. For ease of use, I have included Gmail's SMTP server, and it should work, provided you give the scripts the correct Gmail credentials. The script uses the functions provided to send mail in smtplib. This again demonstrates the power of using Python at this level. Something like SMTP interaction is easy and readable in Python. Equivalent shell scripts are messy, and such libraries are not as easily accessible, if they exist at all.

译者信息

译者信息

葱油拌面

翻译于 11个月前

3人 顶 此译文

接下来,你需要一个发送邮件的方法,在Python 函数标准库中,你可以导入smtplib 库,这是一个用来连接SMTP服务器并发送邮件的模块。让我们写一个简单的Python脚本,使用这个模块发送一个邮件给每个top 10 的用户。

01#!/usr/bin/env python

02import smtplib

03import sys

04

05

06GMAIL_SMTP_SERVER= "smtp.gmail.com"

07GMAIL_SMTP_PORT= 587

08

09GMAIL_EMAIL= "Your Gmail Email Goes Here"

10GMAIL_PASSWORD= "Your Gmail Password Goes Here"

11

12

13def initialize_smtp_server():

14'''

15This function initializes and greets the smtp server.

16It logs in using the provided credentials and returns

17the smtp server object as a result.

18'''

19smtpserver= smtplib.SMTP(GMAIL_SMTP_SERVER, GMAIL_SMTP_PORT)

20smtpserver.ehlo()

21smtpserver.starttls()

22smtpserver.ehlo()

23smtpserver.login(GMAIL_EMAIL, GMAIL_PASSWORD)

24return smtpserver

25

26

27def send_thank_you_mail(email):

28to_email= email

29from_email= GMAIL_EMAIL

30subj= "Thanks for being an active commenter"

31# The header consists of the To and From and Subject lines

32# separated using a newline character

33header= "To:%s\nFrom:%s\nSubject:%s \n" % (to_email,

34from_email, subj)

35# Hard-coded templates are not best practice.

36msg_body= """

37Hi %s,

38

39Thank you very much for your repeated comments on our service.

40The interaction is much appreciated.

41

42Thank You.""" % email

43content= header+ "\n" + msg_body

44smtpserver= initialize_smtp_server()

45smtpserver.sendmail(from_email, to_email, content)

46smtpserver.close()

47

48

49if __name__== "__main__":

50# for every line of input.

51for emailin sys.stdin.readlines():

52send_thank_you_mail(email) 这个python脚本能够连接任何的SMTP服务器,不管是在本地还是远程。为便于使用,我使用了Gmail的SMTP服务器,正常情况下,应该提供你连接Gmail的密码口令,这个脚本使用了smtp库中的函数发送邮件。再一次证明使用Python脚本的强大之处,类似SMTP这样的交互操作使用python来写的话是比较简单易读的。相同的shell脚本的话,可能是比较复杂并且像SMTP这样的库是基本没有的。

In order to send the e-mails to the top ten users sorted by comment frequency, first you must isolate only the e-mail column of the output of column names. To isolate a certain column in Linux, you use thecutcommand. In the example below, the commands are given in two separate chains. For ease of use, I wrote the output into a temporary file, which can be loaded into the second chain. This simply makes the process more readable (the Python script for sending mail is referred to as sendemail.py):

$ cat emailcomments.csv | python csvcolumn.py |

?python namescount.py | sort -rn > /tmp/comment_freq

$ cat /tmp/comment_freq | head -n 10 | cut -f2 |

?python sendemail.py

This shows the real power of Python as a utility in a chain of bash commands such as this. Writing scripts that accept input from standard input and write any data out to standard out, allows the developer to chain commands such as these together quickly and easily with a link in the chain often being a Python program. This philosophy of designing a small application that services one purpose fits nicely with the flow of commands being used here.

译者信息

译者信息

showme

翻译于 11个月前

1人 顶 此译文

为了发送电子邮件给评论频率最高的前十名用户,首先必须单独得到电子邮件列的内容。要取出某一列,在Linux中你可以使用cut命令。在下面的例子中,命令是在两个单独的串。为了便于使用,我写输出到一个临时文件,其中可以加载到第二串命令中。这只是让过程更具可读性(Python发送邮件脚本简称为sendemail.py):

1$cat emailcomments.csv | python csvcolumn.py |

2?python namescount.py |sort -rn > /tmp/comment_freq

3$cat /tmp/comment_freq |head -n 10 |cut -f2 |

4?python sendemail.py

这表明Python作为一种实用工具如bash命令链的真正威力。编写的脚本从标准输入接受

数据并且将任何输出写入到标准输出,允许开发者串起这些命令,

链中的这些快速,简单的命令以及Python程序。这种只为一个目的设计小程序的哲学非常适用于这里所使用的命令流方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值