0
我正在使用一個shell腳本來調用jython腳本。當我調用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()