Python - emulando comandos shell

Posted on seg 25 janeiro 2010 in HowTo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/python
#-*- coding: UTF-8 -*-

import os

class Cmd(object):
   def __init__(self, cmd):
       self.cmd = cmd
   def __call__(self, *args):
       return os.system("%s %s" % (self.cmd, " ".join(args)))

class Sh(object):
    def __getattr__(self, attribute):
        return Cmd(attribute)

# exemplos de uso:

sh = Sh()
sh.ls('-la')
sh.free()
sh.ps('aux')
sh.uname('-a')