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.
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
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.
Related
I have a requirement, where i need to pass multiple arguments to the script to trigger parallel process for each argument. Now i need to capture each process output in the separate log file.
for arg in test_{01..05} ; do bash test.sh "$arg" & done
Above piece of code can only give parallel processing for the input arguments. I tried with exec > >(tee "/path/of/log/$arg_filedate +%Y%m%d%H.log") 2>&1 and it was able to create single log file name with just date with empty output. Can someone suggest whats going wrong here or if there is any best way other than using parallel package
Try:
data_part=$(date +%Y%m%d%H)
for arg in test_{01..05} ; do bash test.sh "$arg" > "/path/to/log/${arg}_${data_part}.log" & done
If i use "$arg_date +%Y%m%d%H.log" it is creating a file with just date without arg
Yes, because $arg_ is parsed as a variable name
arg_=blabla
echo "$arg_" # will print blabla
echo "${arg_}" # equal to the above
To separate _ from arg use braces "${arg}_" would expand variable arg and add string _.
Simple question, I know squat about bash scripts.
I've got a script test.sh and it sends a mail with some parameters of our DB while we run some stuff. I want to add the options 1, 2, 3 next to the ./test.sh so that the mail contains the current step of the process.
Example:
./test.sh 1 #>> Sends the mail with "Pre-aplication" in its subject.
PS: I know where to change the subject of the mail, but don't know how to read the variable from beside the .sh and then choose the words.
Your first command line input is simply stored in the $1 variable within the script. So you can use $1 without any explicit assignment in test.sh to read the number defined in command line. Find an example here. Note that to get the value, you should use double-quote in your script: "$1"
What should be using arguments.
Assuming you have a function set-up as such
function_name () {
echo "Parameter #1 is $1"
}
You can pass in an argument from the command line like so
sh example.sh example
Basically you can pass in any number of arguments and access each one like so..
$1 ('first argument')... $2 ('second argument')... $n('Nth argument)
You can go through this Documentation on Advanced bash-scripting guide to know more
http://www.tldp.org/LDP/abs/html/internalvariables.html#ARGLIST
I want to create a bash script that is simular to a programming interpreter like mongo, node, redis-cli, mysql, etc.
I want to be able to use a command like test and it behave like the examples above.
thomas#workstation:~$ test
>
How do I make a command that behaves like this? What is this called?
I want to be able to take the content and turn it into a variable.
thomas#workstation:~$ test
> hello world
hello world
thomas#workstation:~$
I only want to take one "entry" after enter is pressed once I want to be able to process the string "hello world" in the code, like echo it.
What is this called? How do I make one using BASH?
I think "read" is what you are looking for, isn't it?
here is a link with some examples: http://bash.cyberciti.biz/guide/Getting_User_Input_Via_Keyboard
so you can do stuff like this:
read -p "Enter your name : " name
echo "Hi, $name. Let us be friends!"
I'm sorry this doesn't answer you directly, but it might be worth it to look into using a more fully capable programming language such as Python, Ruby, or Perl for a task like this. In Python you can use the raw_input() function.
user_command = raw_input('> ')
would yield your prompt.
First, do not name your script test. That generates too much confusion. Whatever you call it, you can do many things:
#!/bin/sh
printf '> '
read line
echo "$line"
If your shell supports it:
#!/bin/sh
read -p '> ' line
echo "$line"
or
#!/bin/sh
printf '> '
sed 1q # This will print the input. To store in in a variable: a=$( sed 1q )
[spatel#tux ~]$ read a
Hello World!!!!!
[spatel#tux ~]$ echo $a
Hello World!!!!!
Key word that might be useful here is REPL (Read–eval–print loop) used primarily for programming languages or coding environments. Your browsers console is a great example of a REPL.
Node allows you use their REPL to build interactive apps.
When I use bash to run the following code, it will assign the value 5 to the var1.
var1=$(awk '$1>$3{ print "5"}' newfile2)
echo $var1
But when I use this same code in banana or something, it gives me error. Can someone please tell me if there is some other way I can write this code so I can run it using the C or KornShell (ksh) as well.
For C shell, use
set var=`....`
For bash/ksh
var1=$(awk '$1>$3{ print "5"}' newfile2)
Use backticks and the set command for csh.
set var1=`awk '$1>$3{ print "5"}' newfile2`
echo $var1
Please note that there are no spaces before and after the variable name.
so,
var1=`ls`
is what you need. But, if you have
var = `ls`
you will get errors.
So, your code should be:
var1=`awk '$1>$3{ print "5"}' newfile2`
echo $var1
Make sure you are in BASH shell, not C or TCSH.
I'm writing a cPanel postwwwact script, if you're not familiar with the script its run after a new account is created. it relies on the user account variable being passed to the script which i then use for various things (creating databases etc). However, I can't seem to find the right way to access the variable i want. I'm not that good with shell scripts so i'd appreciate some advice. I had read somewhere that the value i wanted would be included in $ARGV{'user'} but this simply gives "root" as opposed to the value i need. I've tried looping through all the arguments (list of arguments here) like this:
#!/bin/sh
for var
do
touch /root/testvars/$var
done
and the value i want is in there, i'm just not sure how to accurately target it. There's info here on doing this with PHP or Perl but i have to do this as a shell script.
EDIT Ideally i would like to be able to call the variable by something other than $1 or $2 etc as this would create issues if an argument is added or removed
..for example in the PHP code here:
function argv2array ($argv) {
$opts = array();
$argv0 = array_shift($argv);
while(count($argv)) {
$key = array_shift($argv);
$value = array_shift($argv);
$opts[$key] = $value;
}
return $opts;
}
// allows you to do the following:
$opts = argv2array($argv);
echo $opts[‘user’];
Any ideas?
The parameters are passed to your script as a hash:
/scripts/$hookname user $user password $password
You can use associative arrays in Bash 4, or in earlier versions of Bash you can use built up variable names.
#!/bin/bash
# Bash >= 4
declare -A argv
for ((i=1;i<=${##};i+=2))
do
argv[${#:i:1}]="${#:$((i+1)):1}"
done
echo ${argv['user']}
Or
#!/bin/bash
# Bash < 4
for ((i=1;i<=${##};i+=2))
do
declare ARGV${#:i:1}="${#:$((i+1)):1}"
done
echo ${!ARGV*} # outputs all variable names that begin with ARGV
echo $ARGVuser
Running either:
$ ./argvtest user dennis password secret
dennis
Note: you can also use shift to step through the arguments, but it's destructive and the methods above leave $# ($1, $2, etc.) in place.
#!/bin/bash
# Bash < 4
# using shift (can use in Bash 4, also)
for ((i=1;i<=${##}+2;i++))
do
declare ARGV$1="$2"
# Bash 4: argv[$1}]="$2"
shift 2
done
echo ${!ARGV*}
echo $ARGVuser
If it's passed as a command-line parameter to the script, it's available as $1 if it's first parameter, $2 for the second, and so on.
Why not start off your script with something like
ARG_USER=$1
ARG_FOO=$2
ARG_BAR=$3
And then later in your script refer to $ARG_USER, $ARG_FOO and $ARG_BAR instead of $1, $2, and $3. That way, if you decide to change the order of arguments, or insert a new argument somewhere other than at the end, there is only one place in your code that you need to update the association between argument order and argument meaning.
You could even do more complex processing of $* to set your $ARG_WHATEVER variables, if it's not always going to be that all of the are specified in the same order every time.
You can do the following:
#!/bin/bash
for var in $argv; do
<do whatver you want with $var>
done
And then, invoke the script as:
$ /path/to/script param1 arg2 item3 item4 etc