Shell script to Calculate the bitwise binary calculation AND,OR and XOR for a Input String
Detailed Video Explanation is Below of the Script
Script
#!/bin/bash
#set -x
#############################################
#taking string from user#
echo "Please enter a string with binary calculation"
read v_str
#####Now check the string length is odd or even########
v_str_len=`echo ${#v_str}`
####check if the lenght is odd or even###########
if [ ! -z $v_str ]
then
if [ $((v_str_len%2)) -eq 0 ]
then
echo "lenght is even."
echo "exiting"
exit 1;
else
echo "Number is odd."
echo "Moving Ahead"
fi
else
echo "Empty string passed"
echo "exiting Bye!!"
exit 1;
fi
while [ ${#v_str} -gt 1 ]
do
v_last3=${v_str: -3}
v_char1=`echo $v_last3 |cut -c 1`
v_char2=`echo $v_last3 |cut -c 2`
v_char3=`echo $v_last3 |cut -c 3`
if [ "$v_char2" = "A" ]
then
z=$(( $v_char1 & $v_char3 ))
elif [ "$v_char2" = "B" ]
then
z=$(( $v_char1 | $v_char3 ))
elif [ "$v_char2" = "C" ]
then
z=$(( $v_char1 ^ $v_char3 ))
else
echo "wrong operator[use A of AND , B for OR,C for XOR]"
exit 1;
fi
v_str=${v_str%$v_last3}${z}
done
echo "Result is $z"
Post a Comment
Post a Comment