将参数从shell脚本传递给jython脚本

时间:2012-09-05 00:21:39

标签: python shell jython command-line-arguments

我使用一个shell脚本来调用jython脚本。当我调用shell脚本时,我想传递最终应该传递给jython脚本的参数 示例命令如下:

./passarg.sh -m 12345 -p 'hello'   

passarg.sh代码如下。

#!/bin/bash
#check whether jython exist or not.
if  which jython >/dev/null; then
    echo " "
else
    echo "Jython does't exist on your system, install it first"
    echo "   Command: sudo apt-get install jython"
    exit 1
fi

# The dependent
export CLASSPATH=.:\
./lib/javaee.jar:\


jython myJythonscript.py  -a  #how to passed arguments to myJythonscript.py

在jython脚本中,我使用getopt来解析参数。 myJythonscript.py看起来类似如下所示。

#!/usr/bin/env jython

import unittest
import java
import time
import os
import sys, getopt
def authenticateAccount(verizionAmImp, mdn, password):
    if verizonAMImp.authenticateAccount(mdn, password):
        print mdn, " is registered"
    else:
         print mdn, " is not registered"    
def getRole(verizonAmImp, mdn):
     role = verizonAMImp.getRole(mdn)
     print mdn, " role is ", role

def main():
    URL = "http://localhost:8080/verizon-am-mock/ws/onlinedevice/service"

    password = "verizon0"
    methodCall = 0


    #for Debugging
    print " System Arguments ", sys.argv
    opts, operand = getopt(sys.argv[1:],"ar:m:p:u")

    for o,v in opts:
        if o == "-a":
            methodCall = 1
        elif o == "-r":
            methodCall = 2
        elif o == "-m":
            mdn = v
        elif o == "-p":
            password = v
        elif o == "-u":
            URL = v

    if (mdn is None) or (len(mdn) == 0):
        print "Please provide MDN in the argument"
        exit(1)       

    verizonAMImp = makePortFactory(URL);    

    if methodCall == 1:
        authenticateAccount(verizonAMImp, mdn, password)
    elif methodCall == 2 :    
        getRole(verizonAMImp, mdn)    
    else:
        print "Please check the arguments"
        exit(1) 
if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:2)

一如既往。

./someprog "$@"