当前位置: 首页 > news >正文

Python学习之sh(shell脚本)在Python中的使用

文章目录

  • 前言
  • 一、sh是什么?
  • 二、使用步骤
    • 1.安装
    • 2.使用示例
    • 3.使用sh执行命令
    • 4.关键字参数
    • 5.查找命令
    • 6.Baking参数


前言

本文章向大家介绍[Python库]分析一个python库–sh(系统调用),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。


一、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 lsls = 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

如果想了解更多的内容和功能,可以参考官方文件。


参考文章:How to use sh in Python

http://www.lryc.cn/news/62921.html

相关文章:

  • 追求卓越:编写高质量代码的方法和技巧
  • MATLAB算法实战应用案例精讲-【人工智能】机器视觉(概念篇)(最终篇)
  • 【老王读SpringMVC-3】根据 url 是如何找到 controller method 的?
  • 人机交互到艺术设计及玫瑰花绘制实例
  • 多臂老虎机问题
  • DNS 查询原理详解
  • 浅谈软件测试工程师的技能树
  • 转型产业互联网,新氧能否再造辉煌?
  • CRE66365 应用资料
  • vue3快速上手学习笔记,还不快来看看?
  • HDU 5927 Auxiliary Set
  • 24:若所有参数皆需类型转换,请为此采用non-member函数
  • CMake(2)-详解-编译-安装-支持GDB-添加环境检查-添加版本号-生成安装包
  • java面试题(redis)
  • Vue组件懒加载
  • Qt音视频开发42-网络推流(视频推流/本地摄像头推流/桌面推流/网络摄像头转发推流等)
  • 更简单的存取Bean方式-@Bean方法注解
  • 边缘计算与AI布署应用电力物联网解决方案-RK3588开发平台
  • centos部署unity accelerator
  • HANA开发指南
  • 请问你见过吐代码的泡泡吗(冒泡排序)
  • 【VM服务管家】VM4.0平台SDK_2.1环境配置类
  • 最新研究:可审计的具有拜占庭鲁棒的联邦学习方案
  • JDK1.8下载、安装和环境配置教程
  • 天津超算,青索帮助文档
  • SpringMVC的拦截器和异常处理器
  • 查看库文件是32位还是64位|查看lib是静态库还是导入库|判断是debug模式还是release模式
  • Python小姿势 - Python爬取数据的库——Scrapy
  • [C++初阶]栈和队列_优先级队列的模拟实现 deque类 的理解
  • Spring是什么?关于Spring家族