我正在使用一个 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()