# Compare two variables and print the difference

**URL:** <https://community.unix.com/t/compare-two-variables-and-print-the-difference/376050>\
**Category:** Shell Programming and Scripting\
**Created:** [August 5, 2019, 1:24pm UTC](https://community.unix.com/t/compare-two-variables-and-print-the-difference/376050 "2019-08-05T13:24:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![amar1208](https://community.unix.com/letter_avatar/amar1208/32/5_5575768a8748004e209b776fc1b2916d.png) [@amar1208](https://community.unix.com/u/amar1208)\
**Post date:** [August 5, 2019, 1:24pm UTC](https://community.unix.com/t/compare-two-variables-and-print-the-difference/376050/1 "2019-08-05T13:24:28Z")

</div>

Hi  
PRIM\_SEQ=`some sql code`  
and output of PRIM\_SEQ is like below

```nohighlight
120
130

```

STB\_SEQ=`some sql code`  
and output of STB\_SEQ is like below

```nohighlight
115
110

```

i need to compare this two variables output ( decimal numbers)  
1) What I want to do is to compare every number in the PRIM\_SEQ with every number in the STB\_SEQ and check, and print the difference between them

for example  
PRIM\_SEQ STB\_SEQ  
120 115  
130 110

so output i am looking from above is  
5  
20

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [August 5, 2019, 2:10pm UTC](https://community.unix.com/t/compare-two-variables-and-print-the-difference/376050/2 "2019-08-05T14:10:36Z")

</div>

```nohighlight
set -A PRIM_SEQ `some sql code`
set -A STB_SEQ `some sql code`

for i in {0..${#PRIM_SEQ}}
do
   [[-n "${PRIM_SEQ}" && -n "${STB_SEQ}"]] && {
      (( x = PRIM_SEQ[$i] - STB_SEQ[$i] ))
      echo $x
   }
done

```

---

<div class="post-metadata">

**Author:** ![amar1208](https://community.unix.com/letter_avatar/amar1208/32/5_5575768a8748004e209b776fc1b2916d.png) [@amar1208](https://community.unix.com/u/amar1208)\
**Post date:** [August 5, 2019, 2:46pm UTC](https://community.unix.com/t/compare-two-variables-and-print-the-difference/376050/3 "2019-08-05T14:46:26Z")

</div>

i am getting error as below

```nohighlight
tat.sh: line 33: {0..8}: syntax error: operand expected (error token is "{0..8}")
================================================================
below is my entire shell scrip and output of it when i run

PRIM_SEQ=`sqlplus -s "/ as sysdba" <<EOF
set heading off feedback off verify off
select max(sequence#) from gv\\$log group by thread#;
exit
EOF`
echo "====================================="
echo "PRIMARY_SEQUENCE of" DB_UNIQUE_NAME
echo "====================================="
echo $PRIM_SEQ
echo ""
STDBY_SEQ=`sqlplus -s sys/nobody@stdby as sysdba <<EOF
set heading off feedback off verify off
select max(sequence#) from gv\\$log group by thread#;
exit
EOF`
echo "====================================="
echo "STANDBY_SEQUENCE of DB" 
echo "====================================="
echo $STDBY_SEQ
echo ""
echo
for i in {0..${#PRIM_SEQ}}
do
   [[-n "${PRIM_SEQ}" && -n "${STB_SEQ}"]] && {
      (( x = PRIM_SEQ[$i] - STB_SEQ[$i] ))
      echo $x
   }
done

sh tat.sh

=====================================
PRIMARY_SEQUENCE of
=====================================
12072
11728

=====================================
STANDBY_SEQUENCE of
=====================================
12070
11700

tat.sh: line 33: {0..8}: syntax error: operand expected (error token is "{0..8}")

```

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [August 5, 2019, 3:01pm UTC](https://community.unix.com/t/compare-two-variables-and-print-the-difference/376050/4 "2019-08-05T15:01:15Z")

</div>

try:

```nohighlight
#!/bin/sh

PRIM_SEQ=(`some sql code`)
STB_SEQ=(`some sql code`)

c=0
for i in ${PRIM_SEQ[*]}
do
   (( x = PRIM_SEQ[c] - STB_SEQ[c] ))
   echo $x
   (( c = c + 1 ))
done

```

---

<div class="post-metadata">

**Author:** ![amar1208](https://community.unix.com/letter_avatar/amar1208/32/5_5575768a8748004e209b776fc1b2916d.png) [@amar1208](https://community.unix.com/u/amar1208)\
**Post date:** [August 6, 2019, 5:36am UTC](https://community.unix.com/t/compare-two-variables-and-print-the-difference/376050/5 "2019-08-06T05:36:38Z")

</div>

output i am getting as 200, instead of 170 see below

```nohighlight
PRIM_SEQ=200
STDBY_SEQ=30
echo "====================================="
echo "PRIMARY_SEQUENCE of"
echo "====================================="
echo $PRIM_SEQ
echo "====================================="
echo "STANDBY_SEQUENCE" 
echo "====================================="
echo $STDBY_SEQ
echo ""
c=0
for i in ${PRIM_SEQ[*]}
do
   (( x = PRIM_SEQ[c] - STB_SEQ[c] ))
   echo $x
   (( c = c + 1 ))
done

output i am getting is 

=====================================
PRIMARY_SEQUENCE of
=====================================
200

=====================================
STANDBY_SEQUENCE of
=====================================
30

200

--- Post updated at 10:36 AM ---

sorry
PRIM_SEQ
200
100

STDBY_SEQ
50
20

SO OUTPUT I NEEDED IS
150
80

```

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [August 6, 2019, 6:14am UTC](https://community.unix.com/t/compare-two-variables-and-print-the-difference/376050/6 "2019-08-06T06:14:31Z")

</div>

> [@amar1208](#):
>
> output i am getting as 200, instead of 170 see below
> 
> PRIM\_SEQ=200  
> STDBY\_SEQ=30  
> echo "====================================="  
> echo "PRIMARY\_SEQUENCE of"  
> echo "====================================="  
> echo $PRIM\_SEQ  
> echo "====================================="  
> echo "STANDBY\_SEQUENCE"  
> echo "====================================="  
> echo $STDBY\_SEQ  
> echo ""  
> c=0  
> for i in ${PRIM\_SEQ[\*]}  
> do  
> (( x = PRIM\_SEQ[c] - STB\_SEQ[c] ))  
> echo $x  
> (( c = c + 1 ))  
> done
> 
> output i am getting is
> 
> # ===================================== PRIMARY\_SEQUENCE of
> 
> 200
> 
> # ===================================== STANDBY\_SEQUENCE of
> 
> 30
> 
> 200
> 
> --- Post updated at 10:36 AM ---
> 
> sorry  
> PRIM\_SEQ  
> 200  
> 100
> 
> STDBY\_SEQ  
> 50  
> 20
> 
> SO OUTPUT I NEEDED IS  
> 150  
> 80

Simple typo in variable name in your expression, try:

```plaintext
PRIM_SEQ=(200 100)
STDBY_SEQ=(50 20)
echo "====================================="
echo "PRIMARY_SEQUENCE of"
echo "====================================="
echo $PRIM_SEQ
echo "====================================="
echo "STANDBY_SEQUENCE"
echo "====================================="
echo $STDBY_SEQ
echo ""
c=0
for i in ${PRIM_SEQ[*]}
do
(( x = PRIM_SEQ[c] - STDBY_SEQ[c] ))
echo $x
(( c = c + 1 ))
done

```

---

<div class="post-metadata">

**Author:** ![amar1208](https://community.unix.com/letter_avatar/amar1208/32/5_5575768a8748004e209b776fc1b2916d.png) [@amar1208](https://community.unix.com/u/amar1208)\
**Post date:** [August 6, 2019, 6:57am UTC](https://community.unix.com/t/compare-two-variables-and-print-the-difference/376050/7 "2019-08-06T06:57:29Z")

</div>

```nohighlight
PRIM_SEQ=(200 100)
STDBY_SEQ=(50 20)
above value actually i am getting with some sql code, below is my shell script code, values in variable into vertical line
not like horizontal (200 100) and (50 20)

shell script code

PRIM_SEQ=`some sql code`
STDBY_SEQ=`some sql code`
echo $PRIM_SEQ
echo $STDBY_SEQ

so here output of PRIM_SEQ and STDBY_SEQ
i am getting output like like below 
PRIM_SEQ
200
100

STDBY_SEQ
50
20

so output i am looking like below
150
80

--- Post updated at 11:57 AM ---

PRIM_SEQ=(200 100)
STDBY_SEQ=(50 20)
above value actually i am getting with some sql code, below is my shell script code, values in variable into vertical line
not like horizontal (200 100) and (50 20)

shell script code

PRIM_SEQ=`some sql code`
STDBY_SEQ=`some sql code`
echo $PRIM_SEQ
echo $STDBY_SEQ

so here output of echo PRIM_SEQ and STDBY_SEQ
i am getting output like like below 
PRIM_SEQ
200
100

STDBY_SEQ
50
20

so after subtract PRIM_SEQ - STDBY_SEQ, output like below i am looking for it, please help
150
80

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [August 6, 2019, 7:45am UTC](https://community.unix.com/t/compare-two-variables-and-print-the-difference/376050/8 "2019-08-06T07:45:43Z")

</div>

Please become accustomed and tightly adhere to telling people in here your OS and shell versions with every new thread! Solutions given above rely on being run with a recent bourne shell, like `bash` or `ksh` .  
Using shell arrays is an apt approach for tasks like given, eliminating "horizontal" and "vertical" problems. Both rdrtx1 and Chubler\_XL use it, ( `set -A` being ksh- specific). Make sure your shell provides arrays. If you run your scripts in e.g. `sh` , none of the proposals will work.

Try (recent `bash` required)

```nohighlight
PRIM_SEQ=($(some sql code))
STDBY_SEQ=($(some sql code))
for i in ${!PRIM_SEQ
[*]}; do echo $(( PRIM_SEQ - STDBY_SEQ )); done
150
80

```

---

<div class="post-metadata">

**Author:** ![wisecracker](https://community.unix.com/user_avatar/community.unix.com/wisecracker/32/2800_2.png) [@wisecracker](https://community.unix.com/u/wisecracker)\
**Post date:** [August 6, 2019, 8:26am UTC](https://community.unix.com/t/compare-two-variables-and-print-the-difference/376050/9 "2019-08-06T08:26:05Z")

</div>

Hi amar1208...

Just a note reference rdrtx1's post #2:  
The first line should read something like ` #!/bin/bash ` as 'sh' assumes POSIX compatibility so ARRAYS are technically not possible.  
Make sure you are using bash, ksh or another advanced shell to get that facility.

--- Post updated at 02:26 PM ---

I hope this does not attach itself to my previous:  
RudiC's code works absolutely fine on bash version:

```nohighlight
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

```

Modified with real commands and fixed values:

```nohighlight
#!/bin/bash
PRIM_SEQ=($( python -c "print('200 100')" ))
STDBY_SEQ=($( echo "50 20" ))
for i in ${!PRIM_SEQ
[*]}; do echo $(( PRIM_SEQ - STDBY_SEQ )); done

```

Note to RudiC:  
'STB\_SEQ' should read 'STDBY\_SEQ'...
