前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python UNIX系统管理指南

Python UNIX系统管理指南

作者头像
云深无际
发布2020-08-12 10:18:18
6090
发布2020-08-12 10:18:18
举报
文章被收录于专栏:云深之无迹云深之无迹

一本个人感觉很不错的书,这些文章算是实践

自带的终端来运行命令

chmod a+x是加读写权限

两个python,前面是目录,后面是选择解释器

代码语言:javascript
复制

#! /usr/bin/env python
#A System Information Gathering Script
import subprocess

#Command 1
uname = "uname"
uname_arg = "-a"
print ("Gathering system information with %s command:\n" % uname)
subprocess.call([uname, uname_arg])

#Command 2
diskspace = "df"
diskspace_arg = "-h"
print ("Gathering diskspace information %s command:\n" % diskspace)
subprocess.call([diskspace, diskspace_arg])
代码语言:javascript
复制
#! /usr/bin/env bash
#A System Information Gathering Script
#Command 1
UNAME="uname -a"
printf "Gathering system information with the $UNAME command: \n\n"
$UNAME

#Command 2
DISKSPACE="df -h"
printf "Gathering diskspace information with the $DISKSPACE command: \n\n"
$DISKSPACE

你看以上的脚本的时候,其实是可以看的出来。输出几乎一样的。

那么call的时候将命令和参数分开写是不必要的。完全可以这样写

代码语言:javascript
复制
subprocess.call("df -h",shell=True")

python

bash

编写你个简单的函数

代码语言:javascript
复制
#! /usr/bin/env bash
#A System Information Gathering Script

#Command 1
function uname_func ()

{
    UNAME="uname -a"
    printf "Gathering system information with the $UNAME command: \n\n"
    $UNAME
}
#Command 2
function disk_func ()
{
    DISKSPACE="df -h"
    printf "Gathering diskspace information with the $DISKSPACE command: \n\n"
    $DISKSPACE
}

function main ()
{
    uname_func
    disk_func
}

Main
代码语言:javascript
复制
#! /usr/bin/env python
#A System Information Gathering Script
import subprocess

#Command 1
def uname_func():

    uname = "uname"
    uname_arg = "-a"
    print ("Gathering system information with %s command:\n" % uname)
    subprocess.call([uname, uname_arg])

#Command 2
def disk_func():

    diskspace = "df"
    diskspace_arg = "-h"
    print ("Gathering diskspace information %s command:\n" % diskspace)
    subprocess.call([diskspace, diskspace_arg])

#Main function that call other functions
def main():
    uname_func()
    disk_func()

main()

成功

失败,应该是语法错误

不加权限,就运行不了

把下文的main()改成这样,变成可福用脑本

我们来考虑找个东西,输出好像一样,那么它是真的一样吗?


print语句使用的是非正式的字符串表达式

简单变量名使用的事正式的字符串表达式

在处理自定义类时,这差异会变得十分明显

这个老东西写的不好~


我们创建了一个DoubleRep的类,里面有两个类

在实例化对象以后,指定对象df来保存该对象

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 云深之无迹 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档