Home  >  Article  >  Backend Development  >  How to use Python shell script

How to use Python shell script

王林
王林forward
2023-05-11 10:25:052415browse

1. What is sh

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.

2. Usage steps

1. Installation

Install sh

pip install sh

through the pip command

#2.Use the example

Start and The easiest way to run is to import sh directly or import the command you need from sh. The command can then be used like a Python function.

For example, pass parameters, capture the output and use the output in python. See the code example below for details

# 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")

Two ways to write subcommands

# 子命令
>>> 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. Use sh to execute the command

The command is called just like a function.

But "It should be noted that these are not real Python functions. What is actually run is the corresponding binary command in the system. Just like Bash, it runs dynamically on the system by parsing PATH.

Because of this, Python has very good support for Shell commands, and all commands on the system can be easily run through Python."

Many programs have their own subsets of commands, such as git (branch, sign out).

sh handles subcommands through attribute access.

from sh import git

# resolves to "git branch -v"
print(git.branch("-v"))

print(git("branch", "-v")) # the same command

4. Keyword Arguments

Keyword arguments also work as you would expect: they are replaced with command line argument options.

# 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. Find command

"Which" finds the full path of the program, and returns None if it does not exist. This command is one of the few that is actually implemented as a Python function, and therefore does not rely on an actual "which" binary.

print sh.which("python")     # "/usr/bin/python"
print sh.which("ls")         # "/bin/ls"

if not sh.which("supervisorctl"): sh.apt_get("install", "supervisor", "-y")

There are many more features available with sh and all of them can be found. in official documentation.

6.Baking parameter

sh can use the "baking" parameter as a command, which is to output the corresponding complete command line string, as shown in the following code:

# 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 command

Calling "bake" on the command creates a callable object that automatically passes all arguments passed to "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"

Now, you can call "myserver" to represent a baking ssh command: ###
# 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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete