Python之shell腳本如何使用

本篇內(nèi)容介紹了“Python之shell腳本如何使用”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比撫松網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式撫松網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋撫松地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。

一、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)行?!?/p>

許多程序都有自己的命令子集,例如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

“Python之shell腳本如何使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

網(wǎng)頁(yè)題目:Python之shell腳本如何使用
分享鏈接:http://muchs.cn/article8/pisjip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)動(dòng)態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、小程序開發(fā)、網(wǎng)站設(shè)計(jì)公司、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化