Is it possible to extract these values from the output

Hi,

I would like to search based of "java" and "-Dplatform.home=" and store these two values in bash variables.

 
ps -xef | grep <pid>
     wlsuser 15160 15144  0  Feb 20  ?  17:27 /app1/jdk150_07/bin/IA64N/java -server -Xms1536m -Dplatform.home=/app1/bea/weblogic92 -Dwli.home=/app1/bea/weblogic92/integration

Desired ouput:

 
java_home=/app1/jdk150_07/bin/IA64N/java 
weblogic_home=/app1/bea/weblogic92

I guess this has been answered in this thread

It has not been answered and that is the very reason I opened the new thread. I want them to be stored in variables which is the addressed in the previous thread. Kindly help.

Why don't you help yourself? That would be very educational! All pieces of the puzzle (parts of the solution) are given in that previous thread, you just need to get your ducks in a row.

If your intention is to question my naive skills on unix and / or the efforts put in then below is what i unsuccessfully tried.

eblogic_jdk_home=$($pscmd $weblogic_pid | awk 'BEGIN{X[1]="java_home="}{for(i=1;i<=NF;i++){if($i ~ /java$/){split($i,P,"=");s=P[2]?P[2]:$i;print X[++a]""s}}}')

But it does not give me the desired output. Can you help find me a solution ?

Well, honouring your little effort, I'll try my best. That is not so easy - your wishes and specifications change form post to post and from thread to thread. Pulling some info together from the various sources, and given the fact that I can't duplicate your ps output so I use a file input, I've come up with

TMP=$(awk       '/java/         {printf "/%s/%s", $2, $3}
                 /platform/     {printf "|%s\n", $NF}
                ' FS="/" RS=" +" file )
weblogic_home=${TMP#*|}
java_home=${TMP%|*}
echo $java_home $weblogic_home
/app1/jdk150_07 weblogic92

It is well possible that your awk implementation does not accept e.g. the RS pattern; then we need to play on...

var=`ps -xef | grep <pid>`
echo $var|tr " " "\n" | grep jdk150_07|sed "s/^/java_home=/g"
echo $var|tr " " "\n"|grep platform|sed 's/-Dplatform.home=/weblogic_home=/g'

Check out if you get the required o/p

I don't need "java_home=" or "weblogic_home=" in the output.

All i need is the output value be assigned to bash variable. Please read OP for details.

Here is another approach:

#!/bin/bash

o=$( ps -xef | grep <pid> )

for t in $o
do
        [[ $t =~ java ]] && java_home="$t"
        [[ $t =~ -Dplatform.home ]] && weblogic_home="${t#-Dplatform.home=}"

done
1 Like

Hi,

echo $java_home # does not yeild the correct result
echo $weblogic_home # yeilds the correct result.

From the below output

     bea 15160 15144  0  Feb 20  ?        52:49 /opt/jdk150_07/bin/IA64N/java -Dplatform.home=/opt/bea/weblogic92 -Dwls.home=/opt/bea/weblogic92/server -Djava.security.policy=

echo $java_home yeilds "-Djava.security.policy=" instead of "/opt/jdk150_07/bin/IA64N/java"

The best string to grep for the correct java could be to grep using keyword "bin" and "/java".
Also, when i copy paste it in my main script, i get the below error.

 
./master_script.sh[27]: Syntax error at line 78 : `=~' is not expected.

Kindly help.

That is because it assigned the last occurrence of pattern: java

Replace [[ $t =~ java ]] with [[ $t =~ /java ]]

I hope this helps.

It does help.

It works when I create a new script file and copy-paste your code.

But, when i copy paste it in my existing script the same code, i get the below error.

 
./master_script.sh[27]: Syntax error at line 78 : `=~' is not expected.

I have HP-UX

Kindly help.

OK, then replace existing for loop with:

for t in $o
do
        if echo "$t" | grep -q "/java"
        then
                java_home="$t"
        fi
        if echo "$t" | grep -q "\-Dplatform\.home"
        then
                weblogic_home="${t#-Dplatform.home=}"
        fi
done

OR

for t in $o
do
        $( echo "$t" | grep -q "/java" )  && java_home="$t"
        $( echo "$t" | grep -q "\-Dplatform\.home" ) && weblogic_home="${t#-Dplatform.home=}"
done
1 Like

Thanks a Ton!!