Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /bin/bash
- echo "Enter an array of elements, each separated by a space"
- read -a a
- n=${#a[@]} #n denotes number of elements in the array
- echo "Enter the number to search for: "
- read key #key is the value that will be searched in the array
- for(( i=0; i<$n; i++ )) #loop for searching key in an array
- do
- if [ $key == ${a[$i]} ]; then #if key and element no. i are same, element has been found
- break
- fi
- done
- if [ $i -lt $n ]; then #if i < n, it means break statement had been encountered
- echo "Found at position "$i
- else echo "Not Found!"
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement