Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Thursday, May 25, 2017

Binary to Decimal Conversion Shell Script

Binary to Decimal Conversion Shell Script



#!/bin/bash
# SCRIPT: binary2dec.sh
# USAGE: binary2dec.sh Binary_Numbers(s)
# PURPOSE: Binary to Decimal Conversion. Takes input as command line
# arguments.
# \ ////
# - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# Script Starts Here #
#####################################################################
if [ $# -eq 0 ]
then
echo "Argument(s) not supplied "
echo "Usage: binary2dec.sh Binary_Number(s)"
else
echo -e "33[1mBINARY DECIMAL33[0m"

while [ $# -ne 0 ]
do
Binary=$1
Bnumber=$Binary
Decimal=0
power=1

while [ $Binary -ne 0 ]
do
rem=$(expr $Binary % 10 )
Decimal=$((Decimal+(rem*power)))
power=$((power*2))
Binary=$(expr $Binary / 10)
done

echo -e "$Bnumber $Decimal"
shift
done

fi

#NOTE: Using bc command you can directly get output at command line.
# Here were converting the binary number 111 to a base 10 (decimal)
# number.
#
# $ echo ibase=2;obase=A;111 | bc
# 7
#
#Note that the obase is "A" and not "10".The reason for this is youve
# set the ibase to 2, so if you now had tried to use "10" as the value
# for the obase, it would stay as "2", because "10" in base 2 is "2".
# So you need to use hex to break out of binary mode or use obase=1010
#
# $ echo ibase=2;obase=1010;10000 | bc
# 16
# $ echo ibase=2;obase=1010;1000001 | bc
# 65
#
# You can omit obase=A, because default obase is 10(decimal).
# $ echo ibase=2;1000001 | bc
# 65

OUTPUT:
# sh binary2dec.sh 100 1111 1111111 1000000
BINARY DECIMAL
100 4
1111 15
1111111 127
1000000 64

# sh binary2dec.sh 1111111111111111111
BINARY DECIMAL
1111111111111111111 524287

# sh binary2dec.sh 10000000000000000000
BINARY DECIMAL
binary2dec.sh:line 28:[:10000000000000000000:integer expression expected
10000000000000000000

NOTE:My bash can handle upto 9223372036854775807.
But with bc command you can do more.

# echo ibase=2;10000000000000000000|bc
524288
# echo ibase=2;110000000000000000000|bc
1572864

Available link for download

Read more »

Tuesday, March 14, 2017

Binarysearch Shell Script

Binarysearch Shell Script


 Binary search works by comparing an input value to the middle element
of the array. The comparison determines whether the element equals the
input, less than the input or greater. When the element being compared
to equals the input the search stops and typically returns the posit-
ion or number of searches of the element. If the element is not equal
to the input and the element at the middle point is greater than the
input being searched, the current middle point becomes the new high
point and the array is cut in half again and re-tested. If the element
at the middle point is less than the input being searched, the current
middle point becomes the new low point and the array is cut in half
again and retested. This cutting in half and adjusting either the high
point or the low point is repeated until the item is found or the low
point and the high point converge. This is much faster then sequentia-
lly searching an entire array.

#!/bin/bash
# SCRIPT : binarysearch.sh
# USAGE : binarysearch.sh
# PURPOSE: Searches given number in a sorted list.
# \ ////
# - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# Define Functions Here #
#####################################################################

printnumbers()
{
echo ${ARRAY[*]}
}

sortnumbers() # Using insertion sort
{
for((i=1;i<count;i++))
do
Temp=${ARRAY[i]}
j=$((i-1))
while [ $Temp -lt ${ARRAY[j]} ]
do
ARRAY[j+1]=${ARRAY[j]}
let j--
if [ $j == -1 ]
then
break
fi
done
ARRAY[j+1]=$Temp
done
}

binarysearch()
{
status=-1
i=1
array=($(echo "$@"))
LowIndex=0
HeighIndex=$((${#array[@]}-1))

while [ $LowIndex -le $HeighIndex ]
do

MidIndex=$(($LowIndex+($HeighIndex-$LowIndex)/2))
MidElement=${array[$MidIndex]}

if [ $MidElement -eq $SearchedItem ]
then
status=0
searches=$i
return
elif [ $SearchedItem -lt $MidElement ]
then
HeighIndex=$(($MidIndex-1))
else
LowIndex=$(($MidIndex+1))
fi

let i++

done
}

#####################################################################
# Variable Declaration #
#####################################################################

clear

echo "Enter Array Elements : "

read -a ARRAY

count=${#ARRAY[@]}

search=y

#####################################################################
# Main Script Starts Here #
#####################################################################

# sort the loaded array, must for binary search.
# You can apply any sorting algorithm. I applied insertion sort.

sortnumbers

echo "Array Elements After Sort: "

printnumbers

while [ "$search" == "y" -o "$search" == "Y" ]
do

echo -n "Enter Element to be searched : "
read SearchedItem
binarysearch "${ARRAY[@]}"

if [ $status -eq 0 ]
then
echo "$SearchedItem found after $searches searches"
else
echo "$SearchedItem not found in the list"
fi

echo -n "Do you want another search (y/n): "
read search

done

OUTPUT:

[venu@localhost shell]$ chmod 755 binarysearch.sh
[venu@localhost shell]$ ./binarysearch.sh
Enter Array Elements :
12 34 56 21 43 11 -32 87 112 -43 -111 98 100 22 0 11
Array Elements After Sort:
-111 -43 -32 0 11 11 12 21 22 34 43 56 87 98 100 112
Enter Element to be searched : 21 # Middle element
21 found after 1 searches
Do you want another search (y/n): y
Enter Element to be searched : 112
112 found after 5 searches
Do you want another search (y/n): y
Enter Element to be searched : 56 # second middle element(upper)
56 found after 2 searches
Do you want another search (y/n): y
Enter Element to be searched : 0 # second middle element(lower)
0 found after 2 searches
Do you want another search (y/n): y
Enter Element to be searched : -111
-111 found after 4 searches
Do you want another search (y/n): n

Available link for download

Read more »