Compare two variables and print the difference

Hi
PRIM_SEQ=`some sql code`
and output of PRIM_SEQ is like below

120
130

STB_SEQ=`some sql code`
and output of STB_SEQ is like below

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

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

i am getting error as below

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}")

try:

#!/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
2 Likes

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:

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
1 Like
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

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)

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

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:

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:

#!/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'...

1 Like