2011-05-01 125 views
0

我從互聯網複製了這個腳本,但是不知道如何使用它。我是python新手,所以請幫忙。當我使用 ./test.py執行它的話,我只能看到需要使用bash命令的python腳本幫助

usage: py4sa [option] 

A unix toolbox 

options: 
    --version  show program's version number and exit 
    -h, --help  show this help message and exit 
    -i, --ip  gets current IP Address 
    -u, --usage gets disk usage of homedir 
    -v, --verbose prints verbosely 

當我py4sa類型,然後它說bash命令沒有找到 完整的腳本是

#!/usr/bin/env python 
import subprocess 
import optparse 
import re 

#Create variables out of shell commands 
#Note triple quotes can embed Bash 

#You could add another bash command here 
#HOLDING_SPOT="""fake_command""" 

#Determines Home Directory Usage in Gigs 
HOMEDIR_USAGE = """ 
du -sh $HOME | cut -f1 
""" 

#Determines IP Address 
IPADDR = """ 
/sbin/ifconfig -a | awk '/(cast)/ { print $2 }' | cut -d':' -f2 | head -1 
""" 

#This function takes Bash commands and returns them 
def runBash(cmd): 
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) 
    out = p.stdout.read().strip() 
    return out #This is the stdout from the shell command 

VERBOSE=False 
def report(output,cmdtype="UNIX COMMAND:"): 
    #Notice the global statement allows input from outside of function 
    if VERBOSE: 
     print "%s: %s" % (cmdtype, output) 
    else: 
     print output 

#Function to control option parsing in Python 
def controller(): 
    global VERBOSE 
    #Create instance of OptionParser Module, included in Standard Library 
    p = optparse.OptionParser(description='A unix toolbox', 
              prog='py4sa', 
              version='py4sa 0.1', 
              usage= '%prog [option]') 
    p.add_option('--ip','-i', action="store_true", help='gets current IP Address') 
    p.add_option('--usage', '-u', action="store_true", help='gets disk usage of homedir') 
    p.add_option('--verbose', '-v', 
       action = 'store_true', 
       help='prints verbosely', 
       default=False) 

    #Option Handling passes correct parameter to runBash 
    options, arguments = p.parse_args() 
    if options.verbose: 
     VERBOSE=True 
    if options.ip: 
     value = runBash(IPADDR) 
     report(value,"IPADDR") 
    elif options.usage: 
     value = runBash(HOMEDIR_USAGE) 
     report(value, "HOMEDIR_USAGE") 
    else: 
     p.print_help() 

#Runs all the functions 
def main(): 
    controller() 

#This idiom means the below code only runs when executed from command line 
if __name__ == '__main__': 
    main() 

回答

2

在我看來,你已經用另一個名字存儲腳本:test.py而不是py4sa。因此,像你一樣輸入./test.py對你來說是正確的。然而,該程序需要參數,因此您必須輸入'usage'下列出的選項之一。

通常,「py4sa [OPTIONS]」將意味着選項是可選的,但看代碼,我們可以看到,它是不是:

if options.verbose: 
    # ... 
if options.ip: 
    # ... 
elif options.usage: 
    # ... 
else: 
    # Here's a "catch all" in case no options are supplied. 
    # It will show the help text you get: 
    p.print_help() 

需要注意的是該方案可能不會被認可的bash即使你將它重命名爲py4sa,因爲當前目錄通常不在bash的PATH中。它說'使用:py4sa(..)',因爲它被硬編碼到程序中。

+0

我試圖**。/ test.py --ip **和它的工作 – Mahakaal 2011-05-01 04:57:20

0

該腳本被稱爲「test.py」。要麼像這樣調用它,要麼將其重命名爲「py4sa」。

0

運行使用解釋Python腳本,所以

$ python py4sa