0% found this document useful (1 vote)
2K views

Linear Search Shell Script

This shell script performs a linear search on a user-provided array to find a given search term. It defines a function called lsearch that loops through each element of the array, compares it to the search term, and returns the index if found or -1 if not found. The main script then prompts the user to enter array elements, a search term, displays the result, and allows additional searches until the user enters 'n'.

Uploaded by

kvsrvzm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
2K views

Linear Search Shell Script

This shell script performs a linear search on a user-provided array to find a given search term. It defines a function called lsearch that loops through each element of the array, compares it to the search term, and returns the index if found or -1 if not found. The main script then prompts the user to enter array elements, a search term, displays the result, and allows additional searches until the user enters 'n'.

Uploaded by

kvsrvzm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Linear Search Shell Script

#!/bin/bash
# SCRIPT : linearsearch.sh
# USAGE

: linearsearch.sh

# PURPOSE: Searches given number in a list.


# A variation of Here Document permits "commenting out" data
block.
: <<DATABLOCK
In computer science, linear search or sequential search is a
method
for finding a particular value in a list, that consists in
checking
every one of its elements, one at a time and in sequence,
until the
desired one is found.
This is a very straightforward loop comparing every element
in the
array with the key. As soon as an equal value is found, it
returns.
If the loop finishes without finding a match, the search
failed and
-1 is returned.
For small arrays, a linear search is a good solution because
it's so
straightforward. In an array of a million elements, a linear
search
will take,on average, 500,000 comparisons to find the key. For
a much
faster search, take a look at binary search.
DATABLOCK

##############################################################
#######
#

Define Functions Here

#
##############################################################
#######
lsearch()
{
status=-1
for((i=0;i<count;i++))
do
Temp=$1
if [ $Temp -eq ${ARRAY[i]} ]
then
status=0
searches=$((i+1))
return
#

return $((i+1))

# Bash function can return value between 0-255, That's why I


assigned
# result to a global variable. This is one of the method to
capture
# return value of a function.
fi
done
}
##############################################################

#######
#

Variable Declaration

#
##############################################################
#######
clear
echo "Enter Array Elements : "
read -a ARRAY
count=${#ARRAY[@]}
search=y
##############################################################
#######
#

Main Script Starts Here

#
##############################################################
#######
while [ "$search" == "y" -o "$search" == "Y" ]
do
echo -n "Enter element to be searched : "
read num
lsearch $num
if [ $status -eq 0 ]
then
echo "$num found after $searches searches"
else
echo "$num not found"
fi

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


read search
done
OUTPUT:
$ sh linearsearch.sh
Enter Array Elements :
12 34 56 78 90 23 45 56 67 321 66 88 92
Enter element to be searched : 56
56 found after 3 searches
Do you want another search (y/n): y
Enter element to be searched : 321
321 found after 10 searches
Do you want another search (y/n): y
Enter element to be searched : 100
100 not found
Do you want another search (y/n): n

You might also like