将变量从Jython(wsadmin)传递给shell脚本

时间:2015-02-28 21:47:53

标签: linux shell jython wsadmin

我正在尝试使用wsadmin.sh调用的Jython脚本将从WebSphere检索到的值传递给我的调用者shell脚本中的变量。

调用者shell脚本(getValue.sh)将具有:

#!/bin/sh

/opt/ibm/WebSphere/AppServerV70/bin/wsadmin.sh -lang jython -conntype SOAP -f /home/user/Jython.py

exit 0

Jython脚本(Jython.py)将具有:

cellName = AdminControl.getCell()
return cellName

如何将cellName的值存储到我的shell脚本中的变量中,例如CELL_NAME,我可以使用它:

echo "Cell Name is: " ${CELL_NAME}

这个版本的Jython脚本比我现实使用的版本简单得多,但我认为这个概念是一样的。

如果我在Jython脚本中使用了很多函数,有没有办法将其中一个值传递给我的shell脚本?即。

def getValue1():
     value1 = "1"
     return value1

def getValue2():
     value2 = "2"
     return value2

def getValue3():
     value3 = "3"
     return value3

print getValue1()
print getValue2()
print getValue3()

我有办法将多个值存储到不同的shell脚本变量中吗?即。

echo "Value #1: " ${VALUE_ONE}
echo "Value #2: " ${VALUE_TWO}
echo "Value #3: " ${VALUE_THREE}

...这样我可以运行一个Jython脚本来检索多个值,并在我的shell脚本中使用这些多个值进行进一步处理。

感谢您提供任何帮助。

2 个答案:

答案 0 :(得分:3)

谢谢马特。你让我走上了正确的轨道。通过在命令中添加“| tail -1”,我能够完成所需的工作。您可能已经知道wsadmin SOAP连接总是如何吐出这行:

WASX7209I: Connected to process "dmgr" on node labCellManager01 using SOAP connector;  The type of process is: DeploymentManager

...所以我必须找到一种方法只将屏幕输出的最后一部分分配给我的变量,因此使用“tail -1”。

命令变为:

result=`/opt/ibm/WebSphere/AppServerV70/bin/wsadmin.sh -lang jython -conntype SOAP -f /home/user/Jython.py | tail -1`

使用它,您必须小心在Jython脚本中在屏幕上打印的内容,因为只有最后一次打印将分配给变量。您可以使用tail命令调整所需内容。

感谢您的帮助

答案 1 :(得分:1)

我会尝试尽可能多地使用您的代码。我认为你发现最有效的方法是使用Jython的print命令。如果你的get变量脚本看起来像这样

#!/bin/sh

/opt/ibm/WebSphere/AppServerV70/bin/wsadmin.sh -lang jython -conntype SOAP -f /home/user/Jython.py

exit 0

然后在Jython.py文件中的某处,您需要打印。它可能看起来像这样。

def getValue1():
 value1 = "1"
 return value1

def getValue2():
 value2 = "2"
 return value2

def getValue3():
 value3 = "3"
 return value3

print getValue1()
print getValue2()
print getValue3()

输出

1
2
3

如果您需要执行基于此的命令,您可以考虑将结果传递给xargs。假设上面你可以执行此操作

#!/bin/sh

/opt/ibm/WebSphere/AppServerV70/bin/wsadmin.sh -lang jython -conntype SOAP -f /home/user/Jython.py | xargs -i echo "hello world" > file{}.txt

exit 0

这将写出" hello world"到file1.txt,file2.txt和file3.txt

如果您只是想保存结果,请尝试

#!/bin/sh

result=`/opt/ibm/WebSphere/AppServerV70/bin/wsadmin.sh -lang jython -conntype SOAP -f /home/user/Jython.py`

exit 0

围绕命令的ticks(`)(在tilde~上方)。

我的记忆有点模糊,你可能需要一个安静的标志,让wsadmin让那些工作。我希望这会有所帮助。

快乐的编码!如果您还有其他问题,请发表评论。