shell & python 脚本的对比
下面是收集系统信息的脚本,对比一下,shell和python的区别。
#!/bin/bash # A system information gathering script function uname_func () { UNAME="uname -a" printf "Gathering system information with the $UNAME command: \n\n" $UNAME } function disk_func () { DISKSPACE="df -h" printf "Gatheringg system information with the $DISKSPACE command: \n\n" $DISKSPACE } function main () { uname_func disk_func } main
下面这个是python的脚本,和shell脚本实现了相同的功能
#!/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 "Gatheringg diskspace information %s command:\n" % diskspace
subprocess.call([diskspace,diskspace_arg])
#Main function that call other functions
def main():
uname_func()
disk_func()
main()
看完之后,可能觉得连个脚本有很多相似之处。创建了2个函数,然后通过main函数进行调用。其实,最大的区别么过于shell和python对函数的应用在格式上有点不同,请牢记函数格式!!!
转载于:https://blog.51cto.com/bronte/1439587