0

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.

8
  • Your code contains several syntax errors. Please edit your question and copy&paste exactly the code you run on your system. Don't re-type your code as this may introduce or remove errors. Also copy&paste the output you get. Variable names are case-sensitive. AS_OF_DATE is not the same as AS_OF_dATE. Commented Jun 11, 2021 at 10:25
  • @bodo have pasted the exact code. and that's the result which i am getting of declare in the console.. the file is having value 2021-03-01 and it's a pipe delimited file which i am reading Commented Jun 11, 2021 at 10:55
  • 1
    this is the result of declare-p can't be, declare -p should print declare var=something. Are you sure you are using bash? Please add set -x on top of the script and post the output. Commented Jun 11, 2021 at 11:01
  • 2
    The [[ "$AS_OF_DATE" != "$A1" || "$AS_OF_DATE" == "NULL" ]] after else is useless. Do you mean elif [[ "$AS_OF_DATE" != "$A1" || "$AS_OF_DATE" == "NULL" ]]; then? But even this could be replaced with a simple else (without the additional condition). If the else (or elif) is reached, the condition [[ "$AS_OF_DATE" == "$A1" ]] must be false, so you don't need to check the negated condition. If you want to force the output false if "$AS_OF_DATE" == "NULL"even if it might match "$AS", then you would have to check for "NULL" first. Commented Jun 11, 2021 at 11:59
  • 1
    I'd suggest you to print these 2 variables in hex for debugging: printf '%s' "$AS_OF_DATE" | od -v -A n -t x1 and printf '%s' "$A1" | od -v -A n -t x1. Commented Jun 11, 2021 at 14:54

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.