Script has to check if OLD_VERSION is not empty or if it doesn't match some pattern. I tried the below code
#!/bin/bash
OLD_VERSION="ABCDEFGHIJKL"
PATTERN="ABC"
if [[ ! "$OLD_VERSION" =~ "$PATTERN" ]] || [[ ! -z "$OLD_VERSION" ]];
then
echo "Not matched or not null"
else
echo "matched or null"
fi
For the above inputs :
bash -x re.sh
+ OLD_VERSION=ABCDEFGHIJKL
+ PATTERN=ABC
+ [[ ! -z ABCDEFGHIJKL ]]
+ echo 'Not null or doesnot match'
I get the below output:
Not null or doesnot match
and also when I make old_version null, i get Not null or doesnot match, where i expect it to print null.
$OLD_VERSION=""
+ PATTERN=ABC
+ OLD_VERSION=
+ [[ ! -z '' ]]
+ [[ ! '' =~ ABC ]]
+ echo 'Not null or doesnot match'
I get wrong output. I am getting problem in or. What am I doing wrong?
UPDATE :
as suggested by sureshvv , i updated it like :
if [[ ! "$OLD_VERSION" =~ "$PATTERN" && ! -z "${OLD_VERSION}" ]] ;
then
echo "Not null or doesnot match"
else
echo "matched or null"
fi
Thanks all.
wrong output