Python學(xué)習(xí)之shell腳本的使用詳解
一、sh是什么
SH是一個(gè)獨(dú)特的子進(jìn)程包裝器,可將您的系統(tǒng)程序動(dòng)態(tài)映射到Python函數(shù)。SH幫助您用Python編寫Shell腳本,既能支持Bash的所有功能(簡(jiǎn)單的命令調(diào)用,簡(jiǎn)單的管道傳輸) ,又能兼顧Python的靈活性。
SH是Python中成熟的子進(jìn)程接口,允許您調(diào)用任何系統(tǒng)程序,就好像它是一個(gè)函數(shù)一樣。也就是說(shuō),SH讓您幾乎可以調(diào)用任何可以從登錄shell運(yùn)行的命令。
更重要的是,您可以更輕松地捕獲和解析命令的輸出。
二、使用步驟
1.安裝
通過(guò)pip命令來(lái)安裝sh
pip install sh
2.使用示例
啟動(dòng)和運(yùn)行的最簡(jiǎn)單方法是直接導(dǎo)入sh或從sh導(dǎo)入您需要的命令。然后,該命令就可以像Python函數(shù)一樣使用。
比如,傳遞參數(shù),捕獲輸出并在python中使用輸出。詳見下面的代碼示例
# get interface information import sh print sh.ifconfig("eth0") from sh import ifconfig print ifconfig("eth0") # print the contents of this directory print ls("-l") # substitute the dash for an underscore for commands that have dashes in their names sh.google_chrome("http://google.com")
子命令的兩種寫入方式
# 子命令 >>> from sh import git, sudo >>> print(git.branch("-v")) >>> print(git("branch", "-v")) >>> print(sudo.ls("/root")) >>> print(sudo("/bin/ls", "/root")) # with 環(huán)境 >>> with sh.contrib.sudo(_with=True): print(ls("/root")) # _with=True 關(guān)鍵字告訴命令它正處于 with 環(huán)境中, 以便可以正確地運(yùn)行. #將多個(gè)參數(shù)傳遞給命令時(shí),每個(gè)參數(shù)必須是一個(gè)單獨(dú)的字符串: from sh import tar tar("cvf", "/tmp/test.tar", "/my/home/directory/") # 這將不起作用: from sh import tar tar("cvf /tmp/test.tar /my/home/directory")
3.使用sh執(zhí)行命令
命令的調(diào)用就像函數(shù)一樣。
但是“需要注意的是,這些不是真正的Python函數(shù),實(shí)際運(yùn)行的是系統(tǒng)中對(duì)應(yīng)的二進(jìn)制命令,就像Bash一樣,通過(guò)解析PATH在系統(tǒng)上動(dòng)態(tài)地運(yùn)行。
也正因?yàn)檫@樣,Python對(duì)Shell命令的支持非常好,系統(tǒng)上所有命令都可以通過(guò)Python輕松運(yùn)行。”
許多程序都有自己的命令子集,例如git(分支,簽出)。
sh通過(guò)屬性訪問(wèn)處理子命令。
from sh import git # resolves to "git branch -v" print(git.branch("-v")) print(git("branch", "-v")) # the same command
4.關(guān)鍵字參數(shù)
關(guān)鍵字參數(shù)也可以像您期望的那樣工作:它們被替換為命令行參數(shù)選項(xiàng)。
# Resolves to "curl http://duckduckgo.com/ -o page.html --silent" sh.curl("http://duckduckgo.com/", o="page.html", silent=True) # If you prefer not to use keyword arguments, this does the same thing sh.curl("http://duckduckgo.com/", "-o", "page.html", "--silent") # Resolves to "adduser amoffat --system --shell=/bin/bash --no-create-home" sh.adduser("amoffat", system=True, shell="/bin/bash", no_create_home=True) # or sh.adduser("amoffat", "--system", "--shell", "/bin/bash", "--no-create-home")
5.查找命令
“Which”查找程序的完整路徑,如果不存在,則返回None。 該命令是用Python函數(shù)真正實(shí)現(xiàn)的少數(shù)命令之一,因此不依賴于實(shí)際存在的”which”二進(jìn)制程序。
print sh.which("python") # "/usr/bin/python" print sh.which("ls") # "/bin/ls" if not sh.which("supervisorctl"): sh.apt_get("install", "supervisor", "-y")
sh還可以使用更多功能,并且可以找到所有功能。 在里面官方文件。
6.Baking參數(shù)
sh可以將”baking”參數(shù)用作命令,作用是輸出對(duì)應(yīng)的完整命令行字符串,如下面的代碼所示:
# The idea here is that now every call to ls will have the “-la” arguments already specified. from sh import ls ls = ls.bake("-la") print(ls) # "/usr/bin/ls -la" # resolves to "ls -la /" print(ls("/"))
ssh+baking命令
在命令上調(diào)用”bake”會(huì)創(chuàng)建一個(gè)可調(diào)用對(duì)象,該對(duì)象會(huì)自動(dòng)傳遞所有傳遞給”bake”的參數(shù)。
# Without baking, calling uptime on a server would be a lot to type out: serverX = ssh("myserver.com", "-p 1393", "whoami") # To bake the common parameters into the ssh command myserver = sh.ssh.bake("myserver.com", p=1393) print(myserver) # "/usr/bin/ssh myserver.com -p 1393"
現(xiàn)在,可調(diào)用“myserver”表示一個(gè)baking的ssh命令:
# resolves to "/usr/bin/ssh myserver.com -p 1393 tail /var/log/dumb_daemon.log -n 100" print(myserver.tail("/var/log/dumb_daemon.log", n=100)) # check the uptime print myserver.uptime() 15:09:03 up 61 days, 22:56, 0 users, load average: 0.12, 0.13, 0.05
如果想了解更多的內(nèi)容和功能,可以參考官方文件。
到此這篇關(guān)于Python學(xué)習(xí)之shell腳本的使用詳解的文章就介紹到這了,更多相關(guān)Python shell腳本內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Tensor 和 NumPy 相互轉(zhuǎn)換的實(shí)現(xiàn)
本文主要介紹了Tensor 和 NumPy 相互轉(zhuǎn)換的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python解析、提取url關(guān)鍵字的實(shí)例詳解
今天小編就為大家分享一篇Python解析、提取url關(guān)鍵字的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Pandas實(shí)現(xiàn)groupby分組統(tǒng)計(jì)方法實(shí)例
在數(shù)據(jù)處理的過(guò)程,有可能需要對(duì)一堆數(shù)據(jù)分組處理,例如對(duì)不同的列進(jìn)行agg聚合操作(mean,min,max等等),下面這篇文章主要給大家介紹了關(guān)于Pandas實(shí)現(xiàn)groupby分組統(tǒng)計(jì)方法的相關(guān)資料,需要的朋友可以參考下2023-06-06