Python UNIX系统管理指南
一本个人感觉很不错的书,这些文章算是实践
![](http://n4.ikafan.com/assetsj/blank.gif)
自带的终端来运行命令
![](http://n4.ikafan.com/assetsj/blank.gif)
chmod a+x是加读写权限
![](http://n4.ikafan.com/assetsj/blank.gif)
两个python,前面是目录,后面是选择解释器
#! /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])
![](http://n4.ikafan.com/assetsj/blank.gif)
#! /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
![](http://n4.ikafan.com/assetsj/blank.gif)
你看以上的脚本的时候,其实是可以看的出来。输出几乎一样的。
那么call的时候将命令和参数分开写是不必要的。完全可以这样写
subprocess.call("df -h",shell=True")
![](http://n4.ikafan.com/assetsj/blank.gif)
![](http://n4.ikafan.com/assetsj/blank.gif)
![](http://n4.ikafan.com/assetsj/blank.gif)
![](http://n4.ikafan.com/assetsj/blank.gif)
python
![](http://n4.ikafan.com/assetsj/blank.gif)
bash
![](http://n4.ikafan.com/assetsj/blank.gif)
编写你个简单的函数
#! /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
#! /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()
![](http://n4.ikafan.com/assetsj/blank.gif)
成功
![](http://n4.ikafan.com/assetsj/blank.gif)
失败,应该是语法错误
![](http://n4.ikafan.com/assetsj/blank.gif)
不加权限,就运行不了
![](http://n4.ikafan.com/assetsj/blank.gif)
把下文的main()改成这样,变成可福用脑本
![](http://n4.ikafan.com/assetsj/blank.gif)
我们来考虑找个东西,输出好像一样,那么它是真的一样吗?
print语句使用的是非正式的字符串表达式
简单变量名使用的事正式的字符串表达式
在处理自定义类时,这差异会变得十分明显
![](http://n4.ikafan.com/assetsj/blank.gif)
这个老东西写的不好~
我们创建了一个DoubleRep的类,里面有两个类
在实例化对象以后,指定对象df来保存该对象
![](http://n4.ikafan.com/assetsj/blank.gif)
赞 (0)