Parenthesis Check in Linux

In this blog, Sharing code to check whether Parenthesis or Brackets are correct in a string or not.
Consider a String”[()]” should return “Parenthesis check Passed” while String “{()[(]))”should return “Parenthesis check Failed”.

CODE EXPLANATION:
1. Created two String OpenBracketStr and CloseBracketStr.
2. Keep on Appending based on Character present in the String. If its a opening Bracket, it will be added to OpenBracketStr and as we have received Open Bracket type, I am adding a Expected Closed Bracket by myself in String called CloseBracketStr.
In case I get 5 Opening bracket consecutively in a string,then I will add 5 expected closing Brackets to String CloseBracketStr. Remember , I used word “Expected“.
3. As Soon as I will get a Closing Character from a Original String, I will compare the latest Character in string CloseBracketStr with the Closing Character Received during Original String processing. If matched, I will remove the latest Character added (last Character) from String CloseBracketStr and I will remove latest or last Character from OpenBracketStr as well.
4. Once this processing is completed, You will end up with Blank String or empty String for both CloseBracketStr and OpenBracketStr if Original String has correct order of brackets else if it will not be Blank.

str="[()]"
n=${#str} ##Length of a String
i=0
OpeningBracketList='[{(' #List of Opening Bracket Types
ClosingBracketList=']})' #List of Closing Bracket Types
FirstChar=${str:0:1} #First Character of a String
RoundParenthesis=echo $FirstChar | grep -F ')' | wc -l ##Checking whether a First Character is closing Bracket, if yes, then code will exit.
SqParenthesis=echo $FirstChar | grep -F ']' | wc -l
CurlyParenthesis=echo $FirstChar | grep -F '}' | wc -l
if [[ $RoundParenthesis -eq "1" || $SqParenthesis -eq "1" || $CurlyParenthesis -eq "1" ]]
then
echo "Parenthesis Check Failed"
exit 2
fi
OpenBracketStr=''
CloseBracketStr=''
while [[ $i -lt $n ]] ## Starting Loop Bracket by Bracket or you can say Character By Character.
do
Char=${str:$i:1}
OpenBracketCnt=echo $OpeningBracketList | grep -F $Char | wc -l
CloseBracketCnt=echo $ClosingBracketList | grep -F $Char | wc -l
if [[ $OpenBracketCnt -eq "1" ]] ##If open Bracket Comes, add to a new String OpenBracketStr
then
OpenBracketStr=${OpenBracketStr}${Char}
fi
if [ $Char == "[" ] ## if any open Bracket comes, add its respective closing Bracker to another String called CloseBracketStr
then
CloseBracketStr=${CloseBracketStr}']'
fi
if [ $Char == "(" ]
then
CloseBracketStr=${CloseBracketStr}')'
fi
if [ $Char == "{" ]
then
CloseBracketStr=${CloseBracketStr}'}'
fi
len=${#CloseBracketStr}
len=$(( $len -1))
ExpectedClosingBracket=${CloseBracketStr:$len} ## Calculating Expected Closing Bracket.
Openlen=${#OpenBracketStr}
Openlen=$(( $Openlen -1))
if [[ $CloseBracketCnt -eq "1" && $ExpectedClosingBracket == $Char ]] ## Comparing Closed Bracket Char and Expected Closed Character if matches,Remove one Character from CloseBracketStr else add newly Received Closed Bracket to CloseBracketStr. Same Goes for OpenBracketStr as well.
then
CloseBracketStr=${CloseBracketStr:0:$len}
OpenBracketStr=${OpenBracketStr:0:$Openlen}
fi
if [[ $CloseBracketCnt -eq "1" && $ExpectedClosingBracket != $Char ]]
then
echo "Received Incorrect Closing Character"
fi
i=$(( $i + 1))
done
echo "Closing Brackets Str :" $CloseBracketStr ## If String has correctly ordered Brackets, then both these Strings will be Blank else it will not be
echo "Opening Brackets Str :" $OpenBracketStr
if [[ "$CloseBracketStr" -eq "" && "$OpenBracketStr" -eq "" ]]
then
echo "Parenthesis Check Passed"
else
echo "Parenthesis Check Failed"
fi



Passed Scenario
Failed Case. See Opening and Closing Str are not Blank. Original String : “[(])”

Related posts