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.
AS_OF_DATEis not the same asAS_OF_dATE.this is the result of declare-pcan't be,declare -pshould printdeclare var=something. Are you sure you are using bash? Please addset -xon top of the script and post the output.[[ "$AS_OF_DATE" != "$A1" || "$AS_OF_DATE" == "NULL" ]]afterelseis useless. Do you meanelif [[ "$AS_OF_DATE" != "$A1" || "$AS_OF_DATE" == "NULL" ]]; then? But even this could be replaced with a simpleelse(without the additional condition). If theelse(orelif) 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 outputfalseif"$AS_OF_DATE" == "NULL"even if it might match"$AS", then you would have to check for"NULL"first.printf '%s' "$AS_OF_DATE" | od -v -A n -t x1andprintf '%s' "$A1" | od -v -A n -t x1.