溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python之shell腳本怎么使用

發(fā)布時(shí)間:2023-04-26 10:01:23 來(lái)源:億速云 閱讀:126 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“Python之shell腳本怎么使用”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Python之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命令來(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一樣,通過解析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

到此,相信大家對(duì)“Python之shell腳本怎么使用”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI