Python學(xué)習(xí)之shell腳本的使用詳解
一、sh是什么
SH是一個獨特的子進程包裝器,可將您的系統(tǒng)程序動態(tài)映射到Python函數(shù)。SH幫助您用Python編寫Shell腳本,既能支持Bash的所有功能(簡單的命令調(diào)用,簡單的管道傳輸) ,又能兼顧Python的靈活性。
SH是Python中成熟的子進程接口,允許您調(diào)用任何系統(tǒng)程序,就好像它是一個函數(shù)一樣。也就是說,SH讓您幾乎可以調(diào)用任何可以從登錄shell運行的命令。
更重要的是,您可以更輕松地捕獲和解析命令的輸出。
二、使用步驟
1.安裝
通過pip命令來安裝sh
pip install sh
2.使用示例
啟動和運行的最簡單方法是直接導(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)境中, 以便可以正確地運行. #將多個參數(shù)傳遞給命令時,每個參數(shù)必須是一個單獨的字符串: 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ù),實際運行的是系統(tǒng)中對應(yīng)的二進制命令,就像Bash一樣,通過解析PATH在系統(tǒng)上動態(tài)地運行。
也正因為這樣,Python對Shell命令的支持非常好,系統(tǒng)上所有命令都可以通過Python輕松運行。”
許多程序都有自己的命令子集,例如git(分支,簽出)。
sh通過屬性訪問處理子命令。
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ù)選項。
# 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ù)真正實現(xiàn)的少數(shù)命令之一,因此不依賴于實際存在的”which”二進制程序。
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ù)用作命令,作用是輸出對應(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”會創(chuàng)建一個可調(diào)用對象,該對象會自動傳遞所有傳遞給”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”表示一個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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Tensor 和 NumPy 相互轉(zhuǎn)換的實現(xiàn)
本文主要介紹了Tensor 和 NumPy 相互轉(zhuǎn)換的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Pandas實現(xiàn)groupby分組統(tǒng)計方法實例
在數(shù)據(jù)處理的過程,有可能需要對一堆數(shù)據(jù)分組處理,例如對不同的列進行agg聚合操作(mean,min,max等等),下面這篇文章主要給大家介紹了關(guān)于Pandas實現(xiàn)groupby分組統(tǒng)計方法的相關(guān)資料,需要的朋友可以參考下2023-06-06