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ù)一樣。也就是說,SH讓您幾乎可以調(diào)用任何可以從登錄shell運(yùn)行的命令。
更重要的是,您可以更輕松地捕獲和解析命令的輸出。
二、使用步驟
1.安裝
通過pip命令來安裝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一樣,通過解析PATH在系統(tǒng)上動(dòng)態(tài)地運(yùn)行。
也正因?yàn)檫@樣,Python對(duì)Shell命令的支持非常好,系統(tǒng)上所有命令都可以通過Python輕松運(yùn)行。”
許多程序都有自己的命令子集,例如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ù)選項(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)文章
Python基于Dlib的人臉識(shí)別系統(tǒng)的實(shí)現(xiàn)
這篇文章主要介紹了Python基于Dlib的人臉識(shí)別系統(tǒng)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
這篇文章主要介紹了python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Python打包文件夾的方法小結(jié)(zip,tar,tar.gz等)
這篇文章主要介紹了Python打包文件夾的方法,結(jié)合實(shí)例形式總結(jié)分析了Python打包成zip,tar,tar.gz等格式文件的操作技巧,需要的朋友可以參考下2016-09-09
pandas篩選某列出現(xiàn)編碼錯(cuò)誤的解決方法
今天小編就為大家分享一篇pandas篩選某列出現(xiàn)編碼錯(cuò)誤的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11
利用Python循環(huán)(包括while&for)各種打印九九乘法表的實(shí)例
下面小編就為大家?guī)硪黄肞ython循環(huán)(包括while&for)各種打印九九乘法表的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望對(duì)大家有所幫助2017-11-11
詳解利用python-highcharts庫(kù)繪制交互式可視化圖表
本文主要和大家分享一個(gè)超強(qiáng)交互式可視化繪制工具-python-highcharts。python-highcharts就是使用Python進(jìn)行Highcharts項(xiàng)目繪制,簡(jiǎn)單的說就是實(shí)現(xiàn)Python和Javascript之間的簡(jiǎn)單轉(zhuǎn)換層,感興趣的可以了解一下2022-03-03

