Python之shell脚本怎么使用

其他教程   发布日期:2025年04月20日   浏览次数:177

本篇内容主要讲解“Python之shell脚本怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python之shell脚本怎么使用”吧!

一、sh是什么

SH是一个独特的子进程包装器,可将您的系统程序动态映射到Python函数。SH帮助您用Python编写Shell脚本,既能支持Bash的所有功能(简单的命令调用,简单的管道传输) ,又能兼顾Python的灵活性。

SH是Python中成熟的子进程接口,允许您调用任何系统程序,就好像它是一个函数一样。也就是说,SH让您几乎可以调用任何可以从登录shell运行的命令。

更重要的是,您可以更轻松地捕获和解析命令的输出。

二、使用步骤

1.安装

通过pip命令来安装sh

  1. pip install sh

2.使用示例

启动和运行的最简单方法是直接导入sh或从sh导入您需要的命令。然后,该命令就可以像Python函数一样使用。

比如,传递参数,捕获输出并在python中使用输出。详见下面的代码示例

  1. # get interface information
  2. import sh
  3. print sh.ifconfig("eth0")
  4. from sh import ifconfig
  5. print ifconfig("eth0")
  6. # print the contents of this directory
  7. print ls("-l")
  8. # substitute the dash for an underscore for commands that have dashes in their names
  9. sh.google_chrome("http://google.com")

子命令的两种写入方式

  1. # 子命令
  2. >>> from sh import git, sudo
  3. >>> print(git.branch("-v"))
  4. >>> print(git("branch", "-v"))
  5. >>> print(sudo.ls("/root"))
  6. >>> print(sudo("/bin/ls", "/root"))
  7. # with 环境
  8. >>> with sh.contrib.sudo(_with=True):
  9. print(ls("/root"))
  10. # _with=True 关键字告诉命令它正处于 with 环境中, 以便可以正确地运行.
  11. #将多个参数传递给命令时,每个参数必须是一个单独的字符串:
  12. from sh import tar
  13. tar("cvf", "/tmp/test.tar", "/my/home/directory/")
  14. # 这将不起作用:
  15. from sh import tar
  16. tar("cvf /tmp/test.tar /my/home/directory")

3.使用sh执行命令

命令的调用就像函数一样。

但是“需要注意的是,这些不是真正的Python函数,实际运行的是系统中对应的二进制命令,就像Bash一样,通过解析PATH在系统上动态地运行。

也正因为这样,Python对Shell命令的支持非常好,系统上所有命令都可以通过Python轻松运行。”

许多程序都有自己的命令子集,例如git(分支,签出)。

sh通过属性访问处理子命令。

  1. from sh import git
  2. # resolves to "git branch -v"
  3. print(git.branch("-v"))
  4. print(git("branch", "-v")) # the same command

4.关键字参数

关键字参数也可以像您期望的那样工作:它们被替换为命令行参数选项。

  1. # Resolves to "curl http://duckduckgo.com/ -o page.html --silent"
  2. sh.curl("http://duckduckgo.com/", o="page.html", silent=True)
  3. # If you prefer not to use keyword arguments, this does the same thing
  4. sh.curl("http://duckduckgo.com/", "-o", "page.html", "--silent")
  5. # Resolves to "adduser amoffat --system --shell=/bin/bash --no-create-home"
  6. sh.adduser("amoffat", system=True, shell="/bin/bash", no_create_home=True)
  7. # or
  8. sh.adduser("amoffat", "--system", "--shell", "/bin/bash", "--no-create-home")

5.查找命令

“Which”查找程序的完整路径,如果不存在,则返回None。 该命令是用Python函数真正实现的少数命令之一,因此不依赖于实际存在的”which”二进制程序。

  1. print sh.which("python") # "/usr/bin/python"
  2. print sh.which("ls") # "/bin/ls"
  3. if not sh.which("supervisorctl"): sh.apt_get("install", "supervisor", "-y")

sh还可以使用更多功能,并且可以找到所有功能。 在里面官方文件。

6.Baking参数

sh可以将”baking”参数用作命令,作用是输出对应的完整命令行字符串,如下面的代码所示:

  1. # The idea here is that now every call to ls will have the “-la” arguments already specified.
  2. from sh import ls
  3. ls = ls.bake("-la")
  4. print(ls) # "/usr/bin/ls -la"
  5. # resolves to "ls -la /"
  6. print(ls("/"))

ssh+baking命令

在命令上调用”bake”会创建一个可调用对象,该对象会自动传递所有传递给”bake”的参数。

  1. # Without baking, calling uptime on a server would be a lot to type out:
  2. serverX = ssh("myserver.com", "-p 1393", "whoami")
  3. # To bake the common parameters into the ssh command
  4. myserver = sh.ssh.bake("myserver.com", p=1393)
  5. print(myserver) # "/usr/bin/ssh myserver.com -p 1393"

现在,可调用“myserver”表示一个baking的ssh命令:

  1. # resolves to "/usr/bin/ssh myserver.com -p 1393 tail /var/log/dumb_daemon.log -n 100"
  2. print(myserver.tail("/var/log/dumb_daemon.log", n=100))
  3. # check the uptime
  4. print myserver.uptime()
  5. 15:09:03 up 61 days, 22:56, 0 users, load average: 0.12, 0.13, 0.05

以上就是Python之shell脚本怎么使用的详细内容,更多关于Python之shell脚本怎么使用的资料请关注九品源码其它相关文章!