Suraj
Suraj

Reputation: 47

comparing strings in shell

I have a code where it is checking first column from a file which is as_of_date. then from hive query i am storing the result in a variable A1. the column is string as well. but when i am comparing in if loop even if both the strings are same it's going into else loop as both are not equal. Can anyone correct me where i went wrong.

#!/bin/bash

AS_OF_DATE=$(awk -F\|  '{var1=$1; print var1}' a.txt)

A1=$(HIVE -S -e "select max(date) from db.table;")

declare -p A1 AS_OF_DATE

if [[ "$AS_OF_DATE" == "$A1" ]]; then 
echo "true"
else [[ "$AS_OF_DATE" != "$A1" || "$AS_OF_DATE" == "NULL" ]]; 
echo  'False'
fi

Now even if the dates are matching the false if printing and this is the result of declare-p

AS_OF_DATE - "2021-03-01"
A1 - "2021-03-01

Can someone suggest where i am going wrong.

Upvotes: 0

Views: 306

Answers (1)

Darkman
Darkman

Reputation: 3001

This might solve your problem.

#!/bin/bash

AS_OF_DATE="$(awk -F\|  '{print $1; exit}' a.txt)"

A1="$(hive -S -e "select max(date) from db.table;" | awk '{print; exit}')"

declare -p A1 AS_OF_DATE

printf '%s' "${AS_OF_DATE}" | od -v -A n -t x1
printf '%s' "${A1}" | od -v -A n -t x1

if [[ "${AS_OF_DATE}" == "${A1}" ]]; then 
    echo 'True'
else
    echo 'False'
fi

Upvotes: 1

Related Questions