How to use if command to compare in an array?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to use if command to compare in an array?
# 1  
Old 07-20-2015
How to use if command to compare in an array?

Hi all,


Currently i am facing issue using if command to compare to array value.

can someone help me?

Code:
if [ "${ZZZLAST_DATAIN_TIME[$i]}" -gt "${ZDATE_1[$i]}" ]
then
do
echo "red"
else 
echo "green"
fi
done

i have checked array is giving values fine but there seems some issue with if command.

Last edited by Don Cragun; 07-20-2015 at 07:43 PM.. Reason: Add CODE tags.
# 2  
Old 07-20-2015
What errors are you encoutering? Are those array values integers or strings? -gt is for comparing integers only.
# 3  
Old 07-20-2015
I got the answer.

below is the code that worked for me.

Code:
for((i=0;i<15;i++))
do
if [ "${ZZZLAST_DATAIN_TIME[$i]}" -gt "${ZDATE_1[$i]}" ]
then
echo "YES"
else 
echo "no"
fi
done


Last edited by Don Cragun; 07-20-2015 at 07:44 PM.. Reason: Add CODE tags.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Array compare bash script

Hello, i have a script that should compare between ${ARRAY} that contains all fstab record like this : >>echo ${ARRAY} / /boot between all mountpoints in my df that is stord in ${ARRAY2} >>echo ${ARRAY2} / /boot /dev/shm /var/spool/asterisk/monitor now i have this loop: for i in... (6 Replies)
Discussion started by: batchenr
6 Replies

2. Shell Programming and Scripting

Compare file to array, replace with corresponding second array

ok, so here is the issue, I have 2 arrays. I need to be able to create a loop that will find ${ARRAY1 in the text doc, and replace it with ${ARRAY2 then write the results. I already have that working. The problem is, I need it to do that same result across however many items are in the 2... (2 Replies)
Discussion started by: gentlefury
2 Replies

3. Shell Programming and Scripting

compare two array

Hi firnds, I am having two arrays eg. @a=qw(1 2 3 4) and @b=qw(1 3 6 9) Now, I want to compare two arrays on first index of both arrays and if they matched, it should output remaining indexes Pseudo-code if ($a == $b) { print "remaining indexes";} How can i do this using perl? (1 Reply)
Discussion started by: Renesh
1 Replies

4. Shell Programming and Scripting

compare two value in array - Perl

Hi, I just wondering if I want to compare previous value to next value if it same count + 1 if not doesn't count how do we compare in Perl? Thank (2 Replies)
Discussion started by: guidely
2 Replies

5. Shell Programming and Scripting

Require compare command to compare 4 files

I have four files, I need to compare these files together. As such i know "sdiff and comm" commands but these commands compare 2 files together. If I use sdiff command then i have to compare each file with other which will increase the codes. Please suggest if you know some commands whcih can... (6 Replies)
Discussion started by: nehashine
6 Replies

6. UNIX for Dummies Questions & Answers

Compare 2 array files using grep

Using the bash shell I am trying to read in 2 files. The first file (file1) is a list of names to search for in (file2). If a name in file1 is found in file2 I want the entire line in file2 to be printed to an output file. File1 consists of a single column of names. File2 consists of several... (2 Replies)
Discussion started by: lanna001
2 Replies

7. Shell Programming and Scripting

Compare Array results

Hi, In a kshell , i need to compare the results of two array . each Non-match value should copy to a new array. For example: ========== Array one contain the following values: A B C Array two contain the following values: C F A E After comparing this arrays , a new array should... (4 Replies)
Discussion started by: yoavbe
4 Replies

8. UNIX for Dummies Questions & Answers

Compare array values

# tail myprocesses.txt 178 processes at Tue Oct 21 14:33:01 IST 2008 16 MySQL processes at Tue Oct 21 14:33:01 IST 2008 175 processes at Tue Oct 21 14:36:01 IST 2008 60 MySQL processes at Tue Oct 21 14:36:01 IST 2008 192 processes at Tue Oct 21 14:39:01 IST 2008 64 MySQL processes at Tue Oct... (2 Replies)
Discussion started by: shantanuo
2 Replies

9. Shell Programming and Scripting

compare a variable against array elements

hi everyone - i have a bash script that does, in broad terms, the following: given a file containing a list of email accounts, for every account, do , then move on to the next account. pretty simple, and all that stuff works fine. thing is, there's a very good change that any account... (2 Replies)
Discussion started by: fearboy
2 Replies

10. UNIX for Dummies Questions & Answers

compare array contents with file

I have an array "arrA" with the following contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf My data file "Files.dat" has the same contents: A0012 Paint Shop.doc ES001 Contract Signature.doc Budget Plan.pdf TS PWS.pdf I have a script that compares... (0 Replies)
Discussion started by: orahi001
0 Replies
Login or Register to Ask a Question