Python是shell腳本怎麼使用

王林
發布: 2023-05-11 10:25:05
轉載
2276 人瀏覽過

一、sh是什麼

SH是一個獨特的子進程包裝器,可將您的系統程式動態對應到Python函數。 SH幫助您用Python編寫Shell腳本,既能支援Bash的所有功能(簡單的命令調用,簡單的管道傳輸) ,又能兼顧Python的靈活性。

SH是Python中成熟的子進程接口,允許您調用任何系統程序,就好像它是一個函數一樣。也就是說,SH讓您幾乎可以呼叫任何可以從登入shell執行的命令。

更重要的是,您可以更輕鬆地擷取和解析命令的輸出。

二、使用步驟

1.安裝

透過pip指令來安裝sh

pip install sh
登入後複製

2.使用範例

啟動和運行的最簡單方法是直接匯入sh或從sh匯入您需要的命令。然後,該指令就可以像Python函數一樣使用。

例如,傳遞參數,捕獲輸出並在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 环境 >>> with sh.contrib.sudo(_with=True): print(ls("/root")) # _with=True 关键字告诉命令它正处于 with 环境中, 以便可以正确地运行. #将多个参数传递给命令时,每个参数必须是一个单独的字符串: 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執行指令

指令的呼叫就像函數一樣。

但「需要注意的是,這些不是真正的Python函數,實際運行的是系統中對應的二進位命令,就像Bash一樣,透過解析PATH在系統上動態地運行。

也因為這樣,Python對Shell指令的支援非常好,系統上所有指令都可以透過Python輕鬆運作。」

許多程式都有自己的指令子集,例如git(分支,簽出)。

sh透過屬性存取處理子命令。

from sh import git # resolves to "git branch -v" print(git.branch("-v")) print(git("branch", "-v")) # the same command
登入後複製

4.關鍵字參數

關鍵字參數也可以像您期望的那樣運作:它們被替換為命令列參數選項。

# 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函數真正實現的少數命令之一,因此不依賴實際存在的”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可以將”baking”參數用作命令,作用是輸出對應的完整命令列字串,如下面的程式碼所示:

# 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指令

在指令上呼叫”bake」會建立一個可呼叫對象,該物件會自動傳遞所有傳遞給”bake」的參數。

# 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"
登入後複製

現在,可呼叫「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
登入後複製

以上是Python是shell腳本怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!