SH is a unique subprocess wrapper that can dynamically map your system programs to Python functions. SH helps you write Shell scripts in Python, which not only supports all the functions of Bash (simple command invocation, simple pipe transmission), but also takes into account the flexibility of Python.
SH is a mature subprocess interface in Python that allows you to call any system program as if it were a function. That is, SH lets you invoke almost any command that can be run from a login shell.
More importantly, you can capture and parse the output of your commands more easily.
Install sh
pip install sh
# 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")
from sh import git # resolves to "git branch -v" print(git.branch("-v")) print(git("branch", "-v")) # the same command
# 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")
print sh.which("python") # "/usr/bin/python" print sh.which("ls") # "/bin/ls" if not sh.which("supervisorctl"): sh.apt_get("install", "supervisor", "-y")
# 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("/"))
# 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"
# 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
The above is the detailed content of How to use Python shell script. For more information, please follow other related articles on the PHP Chinese website!