开心六月综合激情婷婷|欧美精品成人动漫二区|国产中文字幕综合色|亚洲人在线成视频

    1. 
      
        <b id="zqfy3"><legend id="zqfy3"><fieldset id="zqfy3"></fieldset></legend></b>
          <ul id="zqfy3"></ul>
          <blockquote id="zqfy3"><strong id="zqfy3"><dfn id="zqfy3"></dfn></strong></blockquote>
          <blockquote id="zqfy3"><legend id="zqfy3"></legend></blockquote>
          打開APP
          userphoto
          未登錄

          開通VIP,暢享免費電子書等14項超值服

          開通VIP
          用 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用戶來說,命令行的名聲相當(dāng)?shù)母?。不像其他操作系統(tǒng),命令行是一個可怕的命題,但是對于Linux社區(qū)中那些經(jīng)驗豐富的大牛,命令行卻是最值得推薦鼓勵使用的。通常,命令行對比圖形用戶界面,更能提供更優(yōu)雅和更高效的解決方案。

          命令行伴隨著Linux社區(qū)的成長,UNIX shells,例如 bash和zsh,已經(jīng)成長為一個強大的工具,也是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 此譯文

          讓我們在實際操作中來介紹命令行的強大功能吧。每當(dāng)用戶登陸某服務(wù)后,他們的用戶名都被記錄到一個文本文件。例如,我們來看看有多少獨立用戶曾經(jīng)使用過該服務(wù)。

          以下一系列的命令展現(xiàn)了由一個個小的命令串接起來后所實現(xiàn)的強大功能:

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

          管道符號(|)把一個命令的標(biāo)準(zhǔn)輸出傳送給另外一個命令的標(biāo)準(zhǔn)輸入。在這個例子中,把cat names.log的輸出傳送給sort命令的輸入。sort命令是把每一行按字母順序重新排序。接下來,管道把輸出傳送至uniq命令,它可以刪除重復(fù)名字。最后,uniq的輸出又傳送給wc命令。wc是一個字符計數(shù)命令,使用-l參數(shù),可以返回行的數(shù)量。管道可以讓你把一系列的命令串接在一起。

          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 此譯文

          但是,有時候需求會很復(fù)雜,串接命令會變得十分笨重。在這個情況下,shell腳本可以解決這個問題。shell腳本就是一系列的命令,被shell程序所讀取,并按順序執(zhí)行。Shell腳本同樣支持一些編程語言的特性,例如變量,流程控制和數(shù)據(jù)結(jié)構(gòu)。shell腳步對于經(jīng)常重復(fù)運行的批處理程序非常有用。但是,shell腳本也有一些弱點:

          • shell腳本很容易變?yōu)閺?fù)雜的代碼,導(dǎo)致開發(fā)人員難于閱讀和修改它們。
          • 通常,它的語法和解釋都不是那么靈活,而且不直觀。
          • 它代碼通常不能被其他腳本使用。腳本中的代碼重用率很低,并且腳本通常是解決一些很具體的問題。
          • 它們一般不支持庫特性,例如HTML解釋器或者處理HTTP請求庫,因為庫一般都只出現(xiàn)在流行的語言和腳本語言中。
          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 此譯文

          這些問題通常會導(dǎo)致腳本變得不靈活,并且浪費開發(fā)人員大量的時間。而Python語言作為它的替代品,是相當(dāng)不錯的選擇。使用python作為shell腳本的替代,通常有很多優(yōu)勢:

          • python在主流的linux發(fā)行版本中都被默認安裝。打開命令行,輸入python就可以立刻進入python的世界。這個特性,讓它可以成為大多腳本任務(wù)的最好選擇。
          • python非常容易閱讀,語法容易理解。它的風(fēng)格注重編寫簡約和干凈的代碼,允許開發(fā)人員編寫適合shell腳本的風(fēng)格代碼。
          • python是一個解釋性語言,這意味著,不需要編譯。這讓python成為最理想的腳本語言。python同時還是讀取,演繹,輸出的循環(huán)風(fēng)格,這允許開發(fā)人員可以快速的通過解釋器嘗試新的代碼。開發(fā)人員無需重新編寫整個程序,就可以實現(xiàn)自己的一些想法。
          • python是一個功能齊全的編程語言。代碼重用非常簡單,因為python模塊可以在腳本中方便的導(dǎo)入和使用。腳本可以輕易的擴展。
          • python可以訪問優(yōu)秀的標(biāo)準(zhǔn)庫,還有大量的實現(xiàn)多種功能的第三方庫。例如解釋器和請求庫。例如,python的標(biāo)準(zhǔn)庫包含時間庫,允許我們把時間轉(zhuǎn)換為我們想要的各種格式,而且可以和其他日期做比較。
          • python可以是命令鏈中的一部分。python不能完全代替bash。python程序可以像UNIX風(fēng)格那樣(從標(biāo)準(zhǔn)輸入讀取,從標(biāo)準(zhǔn)輸出中輸出),所以python程序可以實現(xiàn)一些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 pythonimport sysif __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構(gòu)建。除了已完成的工作,還讓我們來看看某個用戶登陸系統(tǒng)到底有多少次。uniq命令只是簡單的刪除重復(fù)記錄,而沒有提示到底這些重復(fù)記錄重復(fù)了多少次。我們使用python腳本替代uniq命令,而且腳本可以作為命令鏈中的一部分。以下是python程序?qū)崿F(xiàn)這個功能(在這個例子中,腳本叫做namescount.py):

          01#!/usr/bin/env python
          02import sys
          03 
          04if __name__ == "__main__":
          05    # 初始化一個names的字典,內(nèi)容為空
          06    # 字典中為name和出現(xiàn)數(shù)量的鍵值對
          07    names = {}
          08    # sys.stdin是一個文件對象。 所有引用于file對象的方法,
          09    # 都可以應(yīng)用于sys.stdin.
          10    for name in sys.stdin.readlines():
          11            # 每一行都有一個newline字符做結(jié)尾
          12            # 我們需要刪除它
          13            name = name.strip()
          14            if name in names:
          15                    names[name] += 1
          16            else:
          17                    names[name] = 1
          18 
          19    # 迭代字典,
          20    # 輸出名字,空格,接著是該名字出現(xiàn)的數(shù)量
          21    for name, count in names.iteritems():
          22            sys.stdout.write("%d\t%s\n" % (count, name))

          讓我們來看看python腳本如何在命令鏈中起作用的。首先,它從標(biāo)準(zhǔn)輸入sys.stdin對象讀取數(shù)據(jù)。所有的輸出都寫到sys.stdout對象里面,這個對象是python里面的標(biāo)準(zhǔn)輸出的實現(xiàn)。然后使用python字典(在其他語言中,叫做哈希表)來保存名字和重復(fù)次數(shù)的映射。要讀取所有用戶的登陸次數(shù),只需執(zhí)行下面的命令:

          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 此譯文

          這里會輸出某用戶出現(xiàn)的次數(shù)還有他的名字,使用tab作為分隔符。接下來的事情就是,以用戶登陸次數(shù)的降序順序輸出。這可以在python中實現(xiàn),但是讓我們使用UNIX的命令來實現(xiàn)吧。前面已經(jīng)提到,使用sort命令可以按字母順序排序。如果sort命令接收一個-rn參數(shù),那么它就會按照數(shù)字的降序方式做排序。因為python腳本輸出到標(biāo)準(zhǔn)輸出,所以我們可以使用管道鏈接sort命令,獲取該輸出:

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

          這個例子使用了python作為命令鏈中的一部分。使用python的優(yōu)勢是:

          • 可以跟例如cat和sort這樣的命令鏈接在一起。簡單的工具(讀取文件,給文件按數(shù)字排序),可以使用成熟的UNIX命令。這些命令都是一行一行的讀取,這意味著這些命令可以兼容大容量的文件,而且它們的效率很高。
          • 如果命令鏈條中某部分很難實現(xiàn),很清晰,我們可以使用python腳本,這可以讓我們做我們想做的,然后減輕鏈條一下個命令的負擔(dān)。
          • python是一個可重用的模塊,雖然這個例子是指定了names,如果你需要處理重復(fù)行的其他輸入,你可以輸出每一行,還有該行的重復(fù)次數(shù)。讓python腳本模塊化,這樣你就可以把它應(yīng)用到其他地方。
          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腳本中結(jié)合模塊和管道風(fēng)格的強大力量,讓我們擴展一下這個問題。讓我們來找出使用服務(wù)最多的前5位用戶。head命令可以讓我們指定需要輸出的行數(shù)。在命令鏈中加入這個命令:

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

          這個命令只會列出前5位用戶。類似的,獲取使用該服務(wù)最少的5位用戶,你可以使用tail命令,這個命令使用同樣的參數(shù)。python命令的結(jié)果輸出到標(biāo)準(zhǔn)輸出,這樣可以允許你擴展和構(gòu)建它的功能。


          為了演示腳本的模塊化特性,我們又來擴展一下問題。該服務(wù)同樣生成一個以逗號分割的csv的日志文件,其中包含,一個email地址列表,還有該地址對我們服務(wù)的評價。如下是其中一個例子:

          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 libraryimport csvimport sysif __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 此譯文

          這個任務(wù)是,提供一個途徑,來發(fā)送一個感謝信息給使用該服務(wù)最多的前10位用戶。首先,我們需要一個腳本讀取csv和輸出其中某一個字段。python提供一個標(biāo)準(zhǔn)的csv讀取模塊。以下的python腳本實現(xiàn)了這個功能:

          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.
          10    csvfile = csv.reader(sys.stdin)
          11 
          12    # 這個腳本必須接收一個參數(shù),指定列的序號
          13    # 使用sys.argv獲取參數(shù).
          14    column_number = 0
          15    if len(sys.argv) > 1:
          16            column_number = int(sys.argv[1])
          17 
          18    # CSV文件的每一行都是用逗號作為字段的分隔符
          19    for row in csvfile:
          20            print row[column_number]

          這個腳本可以把csv轉(zhuǎn)換并返回參數(shù)指定的字段的文本。它使用print代替sys.stout.write,因為print默認使用標(biāo)準(zhǔn)輸出最為它的輸出文件。

          讓我們把這個腳步添加到命令鏈中。新的腳本跟其他命令組合在一起,實現(xiàn)輸出評論最多的email地址。(假設(shè).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 pythonimport smtplibimport sysGMAIL_SMTP_SERVER = "smtp.gmail.com"GMAIL_SMTP_PORT = 587GMAIL_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 smtpserverdef 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 此譯文

          接下來,你需要一個發(fā)送郵件的方法,在Python 函數(shù)標(biāo)準(zhǔn)庫中,你可以導(dǎo)入smtplib 庫,這是一個用來連接SMTP服務(wù)器并發(fā)送郵件的模塊。讓我們寫一個簡單的Python腳本,使用這個模塊發(fā)送一個郵件給每個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    '''
          15    This function initializes and greets the smtp server.
          16    It logs in using the provided credentials and returns
          17    the smtp server object as a result.
          18    '''
          19    smtpserver = smtplib.SMTP(GMAIL_SMTP_SERVER, GMAIL_SMTP_PORT)
          20    smtpserver.ehlo()
          21    smtpserver.starttls()
          22    smtpserver.ehlo()
          23    smtpserver.login(GMAIL_EMAIL, GMAIL_PASSWORD)
          24    return smtpserver
          25 
          26 
          27def send_thank_you_mail(email):
          28    to_email = email
          29    from_email = GMAIL_EMAIL
          30    subj = "Thanks for being an active commenter"
          31    # The header consists of the To and From and Subject lines
          32    # separated using a newline character
          33    header = "To:%s\nFrom:%s\nSubject:%s \n" % (to_email,
          34            from_email, subj)
          35    # Hard-coded templates are not best practice.
          36    msg_body = """
          37    Hi %s,
          38 
          39    Thank you very much for your repeated comments on our service.
          40    The interaction is much appreciated.
          41 
          42    Thank You.""" % email
          43    content = header + "\n" + msg_body
          44    smtpserver = initialize_smtp_server()
          45    smtpserver.sendmail(from_email, to_email, content)
          46    smtpserver.close()
          47 
          48 
          49if __name__ == "__main__":
          50    # for every line of input.
          51    for email in sys.stdin.readlines():
          52            send_thank_you_mail(email)
          這個python腳本能夠連接任何的SMTP服務(wù)器,不管是在本地還是遠程。為便于使用,我使用了Gmail的SMTP服務(wù)器,正常情況下,應(yīng)該提供你連接Gmail的密碼口令,這個腳本使用了smtp庫中的函數(shù)發(fā)送郵件。再一次證明使用Python腳本的強大之處,類似SMTP這樣的交互操作使用python來寫的話是比較簡單易讀的。相同的shell腳本的話,可能是比較復(fù)雜并且像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 此譯文

          為了發(fā)送電子郵件給評論頻率最高的前十名用戶,首先必須單獨得到電子郵件列的內(nèi)容。要取出某一列,在Linux中你可以使用cut命令。在下面的例子中,命令是在兩個單獨的串。為了便于使用,我寫輸出到一個臨時文件,其中可以加載到第二串命令中。這只是讓過程更具可讀性(Python發(fā)送郵件腳本簡稱為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命令鏈的真正威力。編寫的腳本從標(biāo)準(zhǔn)輸入接受 數(shù)據(jù)并且將任何輸出寫入到標(biāo)準(zhǔn)輸出,允許開發(fā)者串起這些命令, 鏈中的這些快速,簡單的命令以及Python程序。這種只為一個目的設(shè)計小程序的哲學(xué)非常適用于這里所使用的命令流方式。                                   

          本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
          打開APP,閱讀全文并永久保存 查看更多類似文章
          猜你喜歡
          類似文章
          在linux操作系統(tǒng)中,如何利用Python調(diào)用shell命令
          Python模塊常用的幾種安裝方式
          python的setup.py文件及其常用命令
          python換行符是什么?
          Linux下高效編寫shell腳本的10個建議
          python安裝后無scripts內(nèi)文件,無法使用pip
          更多類似文章 >>
          生活服務(wù)
          分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
          綁定賬號成功
          后續(xù)可登錄賬號暢享VIP特權(quán)!
          如果VIP功能使用有故障,
          可點擊這里聯(lián)系客服!

          聯(lián)系客服