Tuesday, January 12, 2016

write a shell script to check whether given name exists in that file or not.(Assume there is a file “Names” with some strings.)

# search.sh
# Program :         Assume there is a file “Names” with some strings. write a shell
                             script to check whether given name exists in that file or not
# Author :           141740
# Date :              17/03/2015

echo "Enter the name to search"
read name

flg=0
for s in `cat Names`
do
        if [ $s == $name ]
        then
                flg=1
                break
        fi
done

echo
if [ $flg -eq 1 ]
then
        echo "Name is present in the file"
else
        echo "Name is not present in the file"
fi
Output:
 [141740@localhost ~]$ cat Names
chaithra
Supritha
Neenu
Melody

[141740@localhost ~]$ sh search.sh
Enter the name to search
chaithra

Name is present in the file

[141740@localhost ~]$ sh search.sh
Enter the name to search
dsfcx

Name is not present in the file


[141740@localhost ~]$

1 comment: