Visit Our UNIX and Linux User Community


Comparing multiple variable in if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing multiple variable in if statement
# 1  
Old 05-02-2012
Comparing multiple variable in if statement

Hi there

this script is an atempt to define which instances of Jboss relate to its PID by the date and timestamp I am using calc to test with. On our system the only way you can tell which instance relates to a particular instance is by looking at the start up time and date in a log.

The problem I am having is that if [ "$inst1" == "$proc$val" ] needs to compare with $proc0 the first time the loop runs, $pro1 the next and $proc3 finally in my case. This needs to increment in this way as in theory you could have varying instances of JBoss or calc in this case running.

Can any one point me in the right direction?

Code:
 
#note inst1 and 2 would normally be pulled from pgrep
inst1="09:21"
inst2="11:40"
#pull in the current running instances of gcalctool
PID1=`pgrep -f "gcalctool"`
set -A array $PID1
echo 'Current number of calc running processes : '${#array[*]}
# // print number values in array
#Cycle through processes and get the timestamp for each process match to Jboss instance
val=0
for i in {1..${#array[*]}}
do
        eval "proc$val=\"`ls -ld /proc/${array[$val]} |awk '{print$7}'`\""       
        if [ "$inst1" == "$proc$val" ]
                then
                        echo 'killing '${array[0]}' will shutdown the Jboss Instance for Efin live'
                fi
((val++))
done
echo $proc0 $proc1 $proc2 $tmp
unset array

# 2  
Old 05-02-2012
What shell is this? You may be able to use more graceful, safe, and simple things than eval in shells better than a generic bourne.
# 3  
Old 05-02-2012
Hi there

Its running in ksh, is it possible to do what I am asking?

cheers
# 4  
Old 05-02-2012
In BASH and KSH you can do things like:

Code:
VARNAME="$proc$val"
echo "value of ${VARNAME} is ${!VARNAME}"

Also, you can avoid the eval by feeding the variable name into read:

Code:
... | read "$proc$val"

Note that this won't work right in other shells, because the 'read' will end up in a subshell. KSH orders pipes in reverse for this reason.
# 5  
Old 05-02-2012
Also, why run ls 99 times instead of 1? You can do everything in one whack, and move the equality inside awk itself:

Code:
pushd /proc
PIDS=$(ls -ld ${array[*]} | awk -v INST="$inst1" '$7 == INST { print $NF }')
popd

echo $PIDS

Generating a simple list without the need for dynamic variables inside the shell.

If your needs are more complex than they appear -- do you need to compare inst2 as well? -- explain and I'll add it.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 05-03-2012
On the inst yes it needs to compare, you see when Jboss starts it writes a new log file with the date and timestamp when its was started.

If we can compare these with the PID date and time then we can detremine if it was TEST DEV or Live and which PID to kill.

here is my script in progress (no laughing please I don't do scripting enough to warrant a good understanding) Smilie its work in progress!

Code:
 
#!/usr/bin/ksh
#NHA April 2012
echo 'The current Efin solution runs six instances of JBoss :  efin|live efin|test cp|live cp|test eproc|live eproc|test'
echo
#efin live | test  INSTANCES
#inst1=`ls -ltr /cedar/efin/v40/jboss/live/efin/coa/JBoss_4_2_3/server/coa/log/boot.log | awk ' {print $6,$7,$8}'`
#inst2=`ls -ltr /cedar/efin/v40/jboss/test/efin/coa/JBoss_4_2_3/server/coa/log/boot.log |  awk ' {print $6,$7,$8}'`
inst1="09:21"
inst2="11:40"
#pull in the current running instances of Jboss
PID1=`pgrep -f "gcalctool"`
set -A array $PID1
echo 'Current number of calc running processes : '${#array[*]}
# // print number values in array
#Cycle through processes and get the timestamp for each process match to Jboss instance
val=0
for i in {1..${#array[*]}}
do
        eval "proc$val=\"`ls -ld /proc/${array[$val]} |awk '{print $7}'`\""
        tmp=$proc$val
        echo $tmp
        if [ "$inst1" == "$proc$val" ]
                then
                        echo 'killing '${array[0]}' will shutdown the Jboss Instance for Efin live'
                fi
((val++))
done
echo $proc0 $proc1 $proc2 $tmp
unset array
~


Last edited by nathan.harris; 05-04-2012 at 05:08 AM..
# 7  
Old 05-03-2012
I'd handle it as a stream instead of using arrays and dynamic variable names. xargs can be used to attach the stream of one program to the arguments of another to make things happen in even fewer shell steps. Much more straightforward.

I'd like to see what the output for pgrep -f "gcalctool" and cd /proc ; ls -ld somepid looks like for your system or I may get this somewhat wrong.

Code:
# pushd is like cd but with memory.  'popd' to return where you started.  It can nest, too.
pushd /proc

# Get the PID's, run ls -ld pid1 pid2 ..., feed it into awk, which will hunt for matching start-times and print the relevant PID's and what they belong to.
pgrep -f "gcalctool" | xargs ls -ld | awk 'BEGIN { A[live]="live"; A[test]="test" }; $7 in A { print $NF, $7, A[$7] }' live="09:21" test="11:40" |
while read PID TIME WHICH
do
        echo "$PID is $WHICH"
done

popd

Should show multiple lines like '123456 is live'.

Previous Thread | Next Thread
Test Your Knowledge in Computers #546
Difficulty: Medium
In computer science, an operator takes the values of its operands and combines them in some way to produce a new value.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to apply the update statement in multiple servers on multiple dbs at a time .?

Hi , Can any please help the below requirement on all multiple servers and multiple dbs. update configuration set value='yes' ;1) the above statement apply on 31 Databases at a time on different Ip address eg : 10.104.1.12 (unix ip address ) the above ip box contains 4 db's eg : db... (2 Replies)
Discussion started by: venkat918
2 Replies

2. Shell Programming and Scripting

Comparing multiple files

I want to develop one unix script that will first match the multiple files on one server say A with multiple files on another server say B and copy those to server A. After that need to compare the contents of these 2 set of multiple files on different location on same server and generate the... (4 Replies)
Discussion started by: Charnjeet Singh
4 Replies

3. UNIX for Advanced & Expert Users

Need help in comparing multiple columns from two files.

Hi all, I have two files as below. I need to compare field 2 of file 1 against field 1 of file 2 and field 5 of file 1 against filed 2 of file 2. If both matches , then create a result file 1 with first file data and if not matches , then create file with first fie data. Please help me in... (12 Replies)
Discussion started by: sivarajb
12 Replies

4. Shell Programming and Scripting

comparing variables in an if statement

#!/bin/bash #timetest TIMENOW="$(date)" T1=12:00:00 echo $TIMENOW >timenow cat timenow |cut -f4 -d' ' >time1 T2=$(sed -n "${1}p" time1) echo "T1 = " $T1 echo "T2 = " $T2 if then echo $T1 else echo $T2 fi I thought scripting was simple! So why does this script result in: T1 =... (4 Replies)
Discussion started by: habuchas
4 Replies

5. Shell Programming and Scripting

comparing multiple variables by 'if then'

Hi, I am a noob at shell scripting. basically I am trying to compare row counts from 8 tables in different databases. I have managed to get the row counts using awk from the spool files for both databases. now I have 16 variables with me for database 1 : $A $B $C $D $E $F $G... (3 Replies)
Discussion started by: smallville
3 Replies

6. Shell Programming and Scripting

Comparing multiple variables

Hi! I've come up with a ksh-script that produces one or more lists of hosts. At the and of the script, I would like to print only those hosts that exists in all the lists. Ex. HOSTS="host1 host2 host3 host11" HOSTS="host1 host2 host4" HOSTS="host2 host11" HOSTS="host2 host5 host6 host7... (1 Reply)
Discussion started by: Bugenhagen
1 Replies

7. Shell Programming and Scripting

comparing multiple files

hi, quick question i have one file which join one file with reference one Looks like this: KB0000 KB207418 KB0001 KB244904 KB0002 KB215027 KB0003 KB215027 KB0004 KB215027 KB0005 KB204320 KB0006 KB207074 KB0007 KB215204 KB0008 KB223809 KB0009 KB236640 KB0010 KB244506 ....... (2 Replies)
Discussion started by: karla
2 Replies

8. Shell Programming and Scripting

Comparing a variable to a string

Hi, I am trying to write a script to show the status of a Network card. Variables: chosennic is a read variable statuss=`/sbin/ifconfig $chosennic | grep MTU | awk '{print $1}'` ipadd=`/sbin/ifconfig $chosennic | grep Bcast | awk '{print $2}' | awk -F : '{print $2}'`... (2 Replies)
Discussion started by: mikejreading
2 Replies

9. Shell Programming and Scripting

comparing multiple files in multiple subfolders

Hello, I am having a bit of hard time to get my head around this one. I really hope someone is out there to help me out! Background of my code: I am doing some automation where I am verifying multiple files in multiple sub folders and if they are all identical, I would echo a line with my test... (0 Replies)
Discussion started by: Riz
0 Replies

10. Shell Programming and Scripting

comparing values of same variable

Hello all while writing a small script , i got stuck with this simple thing.Hope you guyz can help. Iam trying to read password echo"enter password" read $pwd now i have to check this echo"enter password to proceed" read $pwd now i have to check both the values of the... (2 Replies)
Discussion started by: coolkid
2 Replies

Featured Tech Videos