3
votes

I am trying to pass a value retrieved from WebSphere using a Jython script called by wsadmin.sh to a variable in my caller shell script.

The caller shell script (getValue.sh) would have:

#!/bin/sh

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

exit 0

The Jython script (Jython.py) would have:

cellName = AdminControl.getCell()
return cellName

How can I store the value of cellName into a variable in my shell script, for example, CELL_NAME, that I could use like:

echo "Cell Name is: " ${CELL_NAME}

This version of the Jython script is a lot simpler than the one I am using in reality, but I think the concept is the same.

If I am using a lot of functions in the Jython script, is there a way to pass one of the values to my shell script? i.e.

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

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

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

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

I there a way to store multiple values into different shell script variables? i.e.

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

... This way I could run one Jython script that would retrieve multiple values and use those multiple values in my shell script for further processing.

Thank you for any help you can provide.

2
I don't miss using wsadmin. I will think about this and hopefully get you something you can work with. There may be more clever answers though.Matt

2 Answers

3
votes

Thank you Matt. You put me on the right track. I was able to accomplish what I needed by adding " | tail -1" to the command. You probably already know how a wsadmin SOAP connection always spits out the line:

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

... so I had to find a way to take only the last part of the screen output to assign to my variable, thus using "tail -1".

The command becomes:

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

Using that, you have to be careful about what you are printing on the screen in the Jython script, because only the last print will be assigned to the variable. You can adjust what you need with the tail command.

Thank you for your help

1
votes

I will try to use your code as much as possible. I think the way you're going to find most effective is using the print command from Jython. if your get variable script looks like this

#!/bin/sh

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

exit 0

Then somewhere in your Jython.py file, you will need to print. It may look something like this.

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

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

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

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

output will be

1
2
3

if you need to do execute a command based on that, you could consider piping the results to xargs. Assuming above you could execute this instead

#!/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

this will write "hello world" to file1.txt, file2.txt, and file3.txt

if you simply want to save the result, try

#!/bin/sh

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

exit 0

with ticks(`) (above tilde ~) surrounding your command.

My memory is a little fuzzy and you may need a quiet flag for wsadmin for those to work. I hope this helps.

Happy coding! Leave a comment if you have any more questions.